opencode
opencode copied to clipboard
Memory leak: Unbounded caches grow without limit
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
-
Instance cache (
project/instance.ts) - Caches project instances by directory -
Provider SDK cache (
provider/provider.ts) - Caches loaded SDK instances -
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 #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
onEvictcallback for cleanup when entries are removed - Maintains Map-like interface for drop-in replacement