Skip to content

High memory usage and database bloat in long-running OpenCode instances #16777

Closed

Description

@BYK

Description

A long-running OpenCode server instance (2+ days) consumes 1.76 GB (602 MB RSS + 1.15 GB swap) on an 8 GB system. The SQLite database grows to 1.99 GB with no automatic cleanup — 1,706 sessions spanning 53 days, 274K parts (1.49 GB) never pruned.

Root causes identified:

  • No data retention — sessions are only soft-archived, never deleted
  • auto_vacuum = OFF — deleted data never reclaims disk space
  • No periodic WAL checkpoint — WAL file grows unbounded during long sessions
  • Compaction marks parts but keeps full data — dead weight in SQLite
  • Bash tool output accumulates unboundedly — verbose commands can grow to hundreds of MB in the output string
  • Edit tool Levenshtein uses O(n×m) matrix — can spike to ~400 MB for 10K char strings
  • FileTime per-session tracking never pruned — slow memory leak over instance lifetime
  • RPC pending map has no timeout — orphaned promises leak if workers die
  • LSP diagnostics Map grows monotonically — empty entries never deleted

Measured metrics:

Metric Value
Main process RSS 602 MB
Main process swap 1.15 GB
Peak RSS (VmHWM) 1.64 GB
SQLite DB size 1.99 GB
DB parts table 1.49 GB
Sessions 1,706 (never auto-deleted)
auto_vacuum OFF

Supersedes #16729 which was auto-closed due to template formatting.

Plugins

No plugins

OpenCode version

0.1.44 (dev build)

Steps to reproduce

  1. Run opencode serve on a machine with 8 GB RAM
  2. Use it actively for 2+ days with multiple worktrees
  3. Monitor RSS with ps aux or /proc/<pid>/status — memory grows monotonically
  4. Check opencode.db file size — grows without bound since archived sessions are never deleted

Screenshot and/or share link

N/A

Operating System

Ubuntu 22.04 (Linux x86_64, 8 GB RAM, 16 GB swap)

Terminal

N/A (headless server mode via opencode serve)

Activity

  1. added
    coreAnything pertaining to core functionality of the application (opencode server stuff)
    perfIndicates a performance issue or need for optimization
    on Mar 9, 2026
  2. github-actions commented on Mar 9, 2026

    @github-actions
    Contributor

    This issue might be a duplicate of existing issues. Please check:

    If the fixes you're tracking (e.g., PR #16730) are already captured under #16697, it may be worth coordinating there rather than maintaining a separate issue.

  3. 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.

  4. chncaesar commented on Jul 22, 2026

    @chncaesar

    I hit the same problem — my DB was 1.3 GB with 56% freelist pages (only 525 MB of live data). I built a small tool to handle the cleanup safely:

    • pip install ocdbc
    • ocdbc analyze # read-only health report
    • ocdbc vacuum # safe cleanup with backup + integrity checks

    It checkpoints WAL, verifies integrity before and after, creates a timestamped backup, enables auto_vacuum = INCREMENTAL, runs VACUUM with progress, and re-enables WAL. Refuses to run while OpenCode has the DB open (fuser check). Took mine from 1.3 GB to ~578 MB.

    Might help others while the upstream fix lands: https://github.com/chncaesar/opencode-db-clean

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

Metadata

Metadata

Assignees

Labels

coreAnything pertaining to core functionality of the application (opencode server stuff)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