Skip to content

Memory leak #9385

Closed
Closed

Description

@yantianqi1

Description

Logically speaking, a 16GB Mac shouldn't have its memory randomly wiped out. It shouldn't be happening all the time.
Error: Unknown error
ud@tauri://localhost/assets/index-D9T6R7qf.js:2:11977
@tauri://localhost/assets/index-D9T6R7qf.js:2:4664

────────────────────────────────────────
Caused by:
{}

Plugins

oo-my-opencode

OpenCode version

No response

Steps to reproduce

No response

Screenshot and/or share link

No response

Operating System

No response

Terminal

No response

Activity

  1. added
    perfIndicates a performance issue or need for optimization
    on Jan 19, 2026
  2. taxilian commented on Jan 19, 2026

    @taxilian

    This ticket is very light on details, but I can confirm I've been seeing issues with memory growing out of bounds quite a bit myself.

    I often run opencode in a coder workspace running on my kubernetes cluster; if the pod allocates more memory than is available it will be killed. I currently have that pod set to 32GB of ram because 16GB always got killed and it still eventually gets there.

    I'm not 100% sure where all the issues are, but here are my thoughts / observations:

    • Since node.js itself has a maximum amount of memory in a single process, it's probably related to some multi process things; not sure what those would be
    • It seems to grow faster when subagents are used. If I had to guess, it seems like there are cases where subagents are no longer being used / have been essentially discarded but they aren't being cleaned up, so they just stay around forever or until you restart the process
    • It seems like there are multiple things that don't get cleaned up because if I leave any opencode instance open for long enough it will eventually use enough memory to be a major issue.
  3. kov commented on Jan 20, 2026

    @kov

    Could you give it a try with #9693 applied?

  4. rekram1-node commented on Jan 20, 2026

    @rekram1-node
    Contributor

    Latest release also shipped some memory fixes

  5. censey commented on Jan 21, 2026

    @censey

    testing with 1.1.27 and still massive memory consumption as it runs high models. I am actually noticing an increase in consumption rate using Opus high vs 1.1.25

  6. rekram1-node commented on Jan 21, 2026

    @rekram1-node
    Contributor

    Probably just the same memory issue from before, no significant changes were made that could be increasing it and a dumb bug was fixed that definitely was causing at least some of it prior

  7. relsunkaev commented on Jan 29, 2026

    @relsunkaev

    Can confirm.
    Image

  8. leeweisern commented on Feb 6, 2026

    @leeweisern

    using opencode overtime spins up multiple opencode processes that i have to kill occasionally

    Image Image
  9. cloudwaddie-agent commented on Feb 22, 2026

    @cloudwaddie-agent

    Investigation Complete - Memory Leak Fix

    I've completed a comprehensive search for memory leaks and created a PR with fixes:

    PR Created

    #14635

    Memory Leaks Found & Fixed

    The primary cause of memory leaks was event listeners not being cleaned up when TUI components unmount:

    1. app.tsx - 6 SDK event listeners fixed:

      • TuiEvent.CommandExecute
      • TuiEvent.ToastShow
      • TuiEvent.SessionSelect
      • SessionApi.Event.Deleted
      • SessionApi.Event.Error
      • Installation.Event.UpdateAvailable
    2. session/index.tsx - 1 event listener fixed:

      • message.part.updated
    3. prompt/index.tsx - 1 event listener fixed:

      • TuiEvent.PromptAppend

    Why This Causes Memory Issues

    Previously, these listeners were registered in component render but never unregistered. When components remount (switching sessions, navigating), duplicate listeners accumulate without being cleaned up, causing memory to grow continuously, leading to the 98% memory usage you reported.

    How to Test

    1. Build: cd packages/opencode && bun run build
    2. Run opencode normally
    3. Monitor memory with htop or Activity Monitor
    4. Create/switch between multiple sessions, read files
    5. Before fix: memory grows continuously
    6. After fix: memory stays stable

    Or use heap snapshots via opencode.status command.

  10. kryptobaseddev commented on Feb 22, 2026

    @kryptobaseddev

    I've been investigating this and put together a fix in #14650 that addresses the subagent deallocation problem specifically called out here, plus several other leak sources.

    The biggest finding: when the task tool creates a subagent session via Session.create() in tool/task.ts, that session is never cleaned up after the task completes. Each subagent retains its full message history, tool output buffers, and event listeners in memory indefinitely. The more subagents you use, the faster memory grows — which matches what @leeweisern showed with multiple opencode processes accumulating.

    The fix calls Session.remove() after extracting the task result. The event-reducer already had cleanupSessionCaches() wired up to session.deleted events — it just was never triggered for subagent sessions. So the cleanup infrastructure existed, it just needed to be invoked.

    The PR also includes the same TUI listener cleanup that #14635 addressed (which was closed), plus fixes for 8 other leak sources across the codebase — SSE buffer overflow, MCP OAuth transport leaks, LSP diagnostics growth, tool output surviving compaction, pending permissions not cleared on session delete, and unbounded delta string accumulation in the event-reducer.

    Tested by building a standalone binary and running it for 2.5 hours with continuous subagent tasks and session switching. RSS stabilized at ~2,500 MB after initial loading and held flat — previous behavior would climb toward 25+ GB under the same workload. Full monitoring data is in the PR description.

  11. n0one42 commented on Feb 23, 2026

    @n0one42
    Image

    Okay... It seems like I need a farm of my MacBook Pro M4 Max to run OpenCode ;)

    OpenCode GUI

  12. thdxr commented on Apr 2, 2026

    @thdxr
    Member

    Closing in favor of #20695 where we're collecting all memory-related problems. Please follow that issue for updates.

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

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingperfIndicates 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