> ## Documentation Index
> Fetch the complete documentation index at: https://splinter.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Memory Limits

> Capped memory storage with auto-eviction

Agents can accumulate lots of data. Memory limits cap storage and auto-evict old entries.

## Basic Usage

```python theme={null}
from splinter.control import MemoryStore

store = MemoryStore(
    max_size_bytes=10*1024*1024,  # 10 MB
    max_entries=1000,
)

# Store data
store.set("researcher", "findings", large_data)

# Retrieve data
data = store.get("researcher", "findings")
```

## Eviction Policies

```python theme={null}
from splinter.control import MemoryStore, EvictionPolicy

# LRU: Evict least recently used
store = MemoryStore(
    max_entries=1000,
    policy=EvictionPolicy.LRU,
)

# FIFO: Evict oldest first
store = MemoryStore(
    max_entries=1000,
    policy=EvictionPolicy.FIFO,
)

# LFU: Evict least frequently used
store = MemoryStore(
    max_entries=1000,
    policy=EvictionPolicy.LFU,
)
```

## Per-Agent Limits

```python theme={null}
store = MemoryStore(
    max_size_bytes=50*1024*1024,  # 50 MB total
)

# Per-agent caps
store.set_agent_limit("researcher", max_size_bytes=20*1024*1024)
store.set_agent_limit("writer", max_size_bytes=10*1024*1024)
```

## Memory Usage

```python theme={null}
# Check usage
usage = store.get_usage()
print(f"Total: {usage.total_bytes / 1024 / 1024:.1f} MB")
print(f"Entries: {usage.total_entries}")

# Per-agent usage
agent_usage = store.get_agent_usage("researcher")
print(f"Researcher: {agent_usage.bytes / 1024 / 1024:.1f} MB")
```

## TTL (Time to Live)

Auto-expire old entries:

```python theme={null}
# Expire after 1 hour
store.set("researcher", "cache", data, ttl_seconds=3600)

# Check if expired
if store.is_expired("researcher", "cache"):
    # Refresh data
    pass
```

## Memory Warnings

```python theme={null}
from splinter.exceptions import MemoryLimitError

try:
    store.set("researcher", "huge_data", very_large_data)
except MemoryLimitError as e:
    print(f"Memory limit exceeded: {e.current_bytes} / {e.limit_bytes}")
```

## Cleanup

```python theme={null}
# Clear specific agent
store.clear_agent("researcher")

# Clear all
store.clear_all()

# Force garbage collection
store.gc()
```

## Best Practices

<AccordionGroup>
  <Accordion title="Set appropriate limits">
    10-50 MB per agent is usually enough. More = slower performance.
  </Accordion>

  <Accordion title="Use TTL for caches">
    Don't keep stale data forever.
  </Accordion>

  <Accordion title="Monitor memory usage">
    If you're hitting limits often, optimize what you're storing.
  </Accordion>

  <Accordion title="Choose the right eviction policy">
    LRU for caches, FIFO for logs, LFU for frequently accessed data.
  </Accordion>
</AccordionGroup>
