Skip to content

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

@Tomoki-Iino

Description

@Tomoki-Iino

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

Metadata

Metadata

Assignees

Labels

services[Component] This issue is related to runtime services, e.g. sessions, memory, artifacts, etc

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions