Skip to content

Memory leak: Unbounded caches grow without limit #9140

Closed

Description

@sauerdaniel

Problem

Several internal caches in OpenCode use standard Map objects without size limits. Over long-running sessions, these caches can grow without bound, contributing to memory issues like those reported in #5363.

Affected Caches

  1. Instance cache (project/instance.ts) - Caches project instances by directory
  2. Provider SDK cache (provider/provider.ts) - Caches loaded SDK instances
  3. LSP spawning cache (lsp/index.ts) - Tracks LSP server spawn promises

Code Example

// Current: unbounded Map that grows forever
const cache = new Map<string, Context>()

// No eviction when cache gets large
cache.set(directory, context)

Impact

  • Memory usage grows continuously over time
  • Long-running sessions can consume excessive RAM (70GB+ reported in opencode eating 70gb of memory? #5363)
  • No mechanism to evict stale entries
  • Opening many directories causes unbounded growth

Relates to #5363

Expected Behavior

Caches should have bounded size with LRU eviction:

const cache = createLruCache<string, Context>({
  maxEntries: 20,
  onEvict: (key, value) => {
    // Clean up evicted entry
    value.dispose()
  }
})

Proposed Solution

Create a bounded LRU (Least Recently Used) cache utility that:

  • Limits maximum entries with configurable maxEntries
  • Automatically evicts least-recently-used entries when limit is reached
  • Provides onEvict callback for cleanup when entries are removed
  • Maintains Map-like interface for drop-in replacement

Activity

  1. added
    perfIndicates a performance issue or need for optimization
    on Jan 17, 2026
  2. github-actions commented on Jan 17, 2026

    @github-actions
    Contributor

    This issue might be a duplicate of or closely related to existing issues. Please check:

    The unbounded cache pattern described in this issue is a root cause of the memory issues mentioned in #5363 and #8947, and similar patterns appear in the snapshot and test infrastructure.

  3. changed the title [-]Unbounded caches cause memory growth over time[/-] [+]Memory leak: Unbounded caches grow without limit[/+] on Jan 18, 2026
  4. github-actions commented on Mar 27, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 90 days of no activity. If the issue is still relevant please open a new one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

perfIndicates a performance issue or need for optimization

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions