adk-python icon indicating copy to clipboard operation
adk-python copied to clipboard

InMemoryMemoryService is always created in `adk api_server`, causing potential OOM risk and inconsistency with Runner implementation

Open Tomoki-Iino opened this issue 1 year ago • 3 comments

Description

When running adk api_server without the --memory_service_uri flag, the CLI automatically creates an InMemoryMemoryService instance by default. This behavior is inconsistent with the Runner class implementation, where memory service can be None, and can lead to memory accumulation and potential Out Of Memory (OOM) errors in production environments.

Inconsistency Between Implementations

Runner Class (Correct Behavior)

In src/google/adk/runners.py (lines 97, 115, 134), the memory service is optional:

class Runner:
    memory_service: Optional[BaseMemoryService] = None  # Line 97

    def __init__(
        self,
        *,
        memory_service: Optional[BaseMemoryService] = None,  # Line 115
        ...
    ):
        """
        Args:
            memory_service: The memory service for the runner.  # Line 134
        """
        self.memory_service = memory_service  # Line 151

When creating a Runner directly, you can pass memory_service=None and no service is created.

CLI Implementation (Problematic Behavior)

In src/google/adk/cli/fast_api.py (lines 88-98), the implementation always creates an InMemoryMemoryService:

# Build the Memory service
if memory_service_uri:
    memory_service = service_registry.create_memory_service(
        memory_service_uri, agents_dir=agents_dir
    )
    if not memory_service:
        raise click.ClickException(
            "Unsupported memory service URI: %s" % memory_service_uri
        )
else:
    memory_service = InMemoryMemoryService()  # Always created!

The CLI forces creation of InMemoryMemoryService even when not needed.

Problem

  1. Implementation inconsistency:

    • Runner class: Memory service can be None (optional)
    • CLI api_server: Always creates InMemoryMemoryService (forced)
  2. Memory accumulation: InMemoryMemoryService stores all session events in a dictionary (self._session_events) that grows indefinitely

Current Behavior

adk api_server ${AGENT_NAME} \
  --port ${PORT} \
  --session_service_uri {DB_URL} \
  --artifact_service_uri {GCS_URL}
# No --memory_service_uri specified

Result: InMemoryMemoryService() is automatically created, even though the user may not need memory features.

Expected Behavior

The CLI should behave consistently with the Runner class:

# When memory_service_uri is not specified:
memory_service = None  # Not InMemoryMemoryService()

Proposed Solution

Option 1: Set to None by default

Modify src/google/adk/cli/fast_api.py:

# Build the Memory service
if memory_service_uri:
    memory_service = service_registry.create_memory_service(
        memory_service_uri, agents_dir=agents_dir
    )
    if not memory_service:
        raise click.ClickException(
            "Unsupported memory service URI: %s" % memory_service_uri
        )
else:
    memory_service = None  # Consistent with Runner implementation

Option 2: Add explicit disable option

Allow users to explicitly specify no memory service:

adk api_server --memory_service_uri none ...

Environment

  • ADK Python version: 1.16.0
  • Python version: 3.11+

Code References

  • Runner implementation: src/google/adk/runners.py:97, 115, 134, 151
  • CLI implementation: src/google/adk/cli/fast_api.py:88-98
  • InMemoryMemoryService: src/google/adk/memory/in_memory_memory_service.py:50-101

Tomoki-Iino avatar Oct 22 '25 12:10 Tomoki-Iino

Hi @Tomoki-Iino ,

Re the issue

Memory accumulation: InMemoryMemoryService stores all session events in a dictionary (self._session_events) that grows indefinitely

The memory won't grow if you don't have explict logic to add session to memory, right?

DeanChensj avatar Oct 23 '25 03:10 DeanChensj

@DeanChensj

Thank you for your feedback.

After reviewing the code more carefully, I realized my understanding was incorrect. The memory service doesn't accumulate data unless add_session_to_memory() is explicitly called.

I apologize for the confusion and appreciate you pointing this out.

Tomoki-Iino avatar Oct 23 '25 10:10 Tomoki-Iino

Thanks @Tomoki-Iino , I'll close this issue and the PR then.

DeanChensj avatar Oct 23 '25 16:10 DeanChensj