GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — upgrading from v1 to v2 made an issue in a massive wastage of RAM in the TUI. opening a single window loads entire sqlite db (6GB) into memory. · Issue #64255 · anomalyco/opencode

upgrading from v1 to v2 made an issue in a massive wastage of RAM in the TUI. opening a single window loads entire sqlite db (6GB) into memory. #64255

New issue
Copy link
New issue
Copy link
Closed as duplicate
Closed as duplicate
upgrading from v1 to v2 made an issue in a massive wastage of RAM in the TUI. opening a single window loads entire sqlite db (6GB) into memory.#64255
Copy link
Labels
area/sessions-storageSessions, history and stored dataplatform/wslWindows Subsystem for Linuxtype/bugSomething is brokenversion/v2OpenCode 2.x

Description

@beneadie
beneadie
opened on Nov 16, 2026
Issue body actions

Description

I tweeted Dax the issue on twitter earlier but didnt get a reply. I managed to half solve the issue in my opencode installation but it is not truly solved. here is the diagnosis made by my agent.

OpenCode v2: session DB accumulates huge free-page bloat after upgrade (WSL2 RAM blow-up)

TL;DR

After upgrading from OpenCode 1.19.5 → 2.x, the global session database
(~/.local/share/opencode/opencode.db) grew to 5.86 GiB, of which
~4.33 GiB (69%) was free pages (freelist_count = 1,060,053 of
page_count = 1,536,800). The DB is in auto_vacuum=0 mode and the v2
migration/prune paths never run VACUUM (SQLite's built-in file-compaction
command), so the file never shrank.

Result: starting the CLI in WSL2 jumped the VM from ~2 GB to ~7 GB (its memory
cap). Native Windows with a small DB was unaffected. VACUUM INTO reduced the
file to 1.81 GiB with no data loss.

Important: the manual VACUUM only fixes the current file. Because the
application never compacts automatically, a future migration, prune, or usage
peak can raise the high-water mark and reproduce the same problem. This needs an
application-level fix (or periodic local maintenance), not just a one-time
cleanup.

Environment

  • OpenCode CLI: log first shows 2.2.7 on 2026-11-11; later 2.2.5 / 2.3.0.
    Previously 1.19.5.
  • OS: WSL2 Ubuntu (ext4), Windows 11 host with 15.6 GB RAM.
  • WSL2 total memory during the issue: 7.5 GiB (default ~50% of host).
  • Desktop app (separate install, same Windows user): 1.14.48, DB = 9.5 MB;
    no issue.

Symptom

Opening opencode in WSL caused WSL RAM to jump from ~2 GB to ~7 GB
immediately, in every repository. The DB is global, so the repo did not matter.
With ten windows open it was previously light; the change happened around
2026-11-11.

An initial red herring: the umbrella workspace contained ~625k files
(node_modules, venvs, site-packages). That contributes file-watcher/IO
pressure but was not the 6 GB.

Root cause

The v2 SQLite session database had grown to 5.86 GiB, but only ~1.8 GiB was
live data. The rest was free pages left behind by v2 schema migrations and the
7-day session prune, with no follow-up VACUUM.

Database stats (before compaction)

size_bytes      6,294,732,800   (5.86 GiB)
page_size       4096
page_count      1,536,800
freelist_count  1,060,053       (~4.33 GiB free = 69%)
auto_vacuum     0
journal_mode    wal

Live data was approximately 1.8 GiB:

session_message  0.73 GB
part             0.69 GB
message          0.35 GB

Row counts (original and compacted DB are identical):

session           251
session_v2        299
session_message   39,715
message           33,156
part              138,830

Why this happened on upgrade

The migration table shows a v2 schema batch completed at
2026-11-11T14:49:05Z, which is exactly when the log first shows
cli starting version=2.2.7. The batch included destructive / table-rebuild
migrations such as:

  • 20261024175301_drop_session_allowance — ALTER TABLE session_v2 DROP COLUMN
  • 20261025123711_blob — new blob / blob_ref
  • 20261025154125_drop_session_pending — DROP TABLE session_pending
  • 20261029002625_session_part — new session_part
  • 20261029073502_session_time_busy
  • 20261029165540_schedule_checks
  • 20261030054400_job — creates job, migrates rows out of kv, then
    DELETE FROM kv

Earlier v2 migrations are even more destructive, e.g.:

  • DELETE FROM session_message; DELETE FROM event; DELETE FROM event_sequence;
  • DROP TABLE permission; / recreate
  • DROP TABLE credential; / recreate
  • rebuild workspace / project_directory through __new_* + DROP + RENAME

In addition, the log shows a retention job running repeatedly from Nov 11:

message=cleanup prune=7.days

Both migration drops/rebuilds and the prune delete many pages. SQLite does not
shrink the file when rows are deleted or tables are dropped when
auto_vacuum=0. The file stays at its high-water mark.

The missing maintenance step

The compiled connection setup in @opencode/storage/Database sets:

PRAGMA journal_mode = WAL
PRAGMA synchronous = NORMAL
PRAGMA busy_timeout = 5000
PRAGMA cache_size = -64000
PRAGMA wal_checkpoint(PASSIVE)

It does not set auto_vacuum, and the migration runner applies migrations
inside transactions but never runs VACUUM / PRAGMA incremental_vacuum after
them. The 7-day prune likewise deletes rows without compacting.

So the design is internally inconsistent: the app intentionally prunes data to
save space, but the file can never shrink.

Why it looked like a sudden regression

Timeline from the log:

  • 2026-10-30 — CLI on 1.19.5.
  • 2026-11-11 14:49:05 — first cli starting version=2.2.7; the v2 migration
    batch completed at the same timestamp; cleanup prune=7.days starts running.
  • 2026-11-13 — v2.3.0 released.
  • 2026-11-16 — issue diagnosed; DB compacted.

Before the v2 line, the DB was smaller and the 1.x startup path was lighter, so
many concurrent windows were fine. The v2 upgrade ran destructive migrations and
retention against a multi-GB DB and left ~4.3 GiB of free pages behind.

Why WSL showed it more than the desktop

The desktop and WSL CLI use separate data directories:

  • Desktop: C:\Users\<user>\.local\share\opencode\opencode.db — 9.5 MB.
  • WSL: ~/.local/share/opencode/opencode.db — was 5.86 GiB.

The desktop DB had only 9 sessions; the WSL DB had 251 sessions. The desktop
also runs an older version with the smaller/older schema.

WSL2 additionally counts file cache toward the VM's memory and does not return
it quickly unless autoMemoryReclaim is enabled. That amplifies the symptom,
but it is not the root cause. After compacting the DB, WSL startup dropped from
~7 GB to ~4 GB — the remaining cost is consistent with ~1.8 GiB of live session
data being loaded/decoded plus file cache/VM overhead.

Recurrence risk

This is not a one-time cleanup problem. The manual VACUUM only removes the
current 4.3 GiB of free pages; it does not change the app's behavior. The same
pattern can return because:

  • A future migration that drops/rebuilds tables can again free a large number
    of pages and raise the file's high-water mark.
  • cleanup prune=7.days deletes rows; without compaction, the file keeps the
    size of the busiest period.
  • A peak in live data (a very large tool output, many long sessions, etc.)
    permanently raises the file size, even after the data is pruned.

SQLite reuses free pages, so the file normally stabilizes at the largest
live-data size seen rather than growing every day. But that ceiling can move up
again. A long-lived installation will therefore likely regress without either an
application-level fix or periodic local maintenance.

For impacted users, a one-time user-performed VACUUM should be described as
mitigation, not as the solution.

Recovery performed (no data lost)

  • Backed up the original DB to opencode.db.bak.

  • Ran VACUUM INTO a new file:

    VACUUM INTO '/home/<user>/.local/share/opencode/opencode.compact.db'
    
  • Verified PRAGMA integrity_check = ok; identical row counts in both DBs
    (251 sessions, 39,715 session_messages, 33,156 messages, 138,830 parts).

  • Swapped the compact DB into place and removed stale -wal / -shm.

  • Result: 6.29 GB → 1.94 GB, freelist_count = 0.

  • Added WSL settings: memory=6GB and
    [experimental] autoMemoryReclaim=gradual (note: autoMemoryReclaim is an
    unknown key under [wsl2]).

  • Added watcher ignores for node_modules, venv, .venv, site-packages,
    caches, data/, output/, .git/ (secondary optimization only).

Suggested fixes

  1. Set PRAGMA auto_vacuum=INCREMENTAL when creating the DB (requires a
    one-time VACUUM), or run VACUUM after migrations that drop/rebuild
    tables.
  2. Compact after the 7-day prune. cleanup prune=7.days deletes rows;
    follow it with PRAGMA incremental_vacuum or a scheduled VACUUM.
  3. Add a DB health check. If freelist_count / page_count exceeds a
    threshold (e.g. 20–25%), compact in the background and log it.
  4. Add maintenance commands: opencode db stats and
    opencode db vacuum (and possibly opencode db prune).
  5. Log DB size / freelist ratio at startup so this is visible before it
    becomes a multi-GB problem.
  6. Bound startup loading. The new v2 session model appears to load a large
    portion of the live data. Consider profiling startup RSS and lazy-loading
    session parts; keep old (message, part) and new (session_message,
    session_part) representations from being retained indefinitely.
  7. Keep large tool outputs / blobs bounded. part was 0.69 GB across
    138k rows; a retention or size cap (plus compaction) would keep the live
    working set small.
  8. Document WSL settings. [experimental] autoMemoryReclaim=gradual plus a
    reasonable memory= cap prevents WSL from holding stale page cache.
  9. Treat compaction as ongoing maintenance. Free pages are reused, so the
    file can look stable and then jump after a migration or usage peak. Provide a
    documented periodic check/compaction path for long-lived installs (or run it
    automatically), so users do not have to discover freelist_count themselves.

Appendix: quick repro / verification

PRAGMA page_size;
PRAGMA page_count;
PRAGMA freelist_count;
PRAGMA auto_vacuum;
PRAGMA journal_mode;

SELECT name, sum(pgsize) AS bytes
FROM dbstat
GROUP BY name
ORDER BY bytes DESC
LIMIT 10;

A DB with millions of free pages and auto_vacuum=0 will not shrink on its own.
VACUUM INTO produces a compact copy; VACUUM rewrites in place (both need
temporary disk space).

Note on terminology: VACUUM is a standard SQLite command and auto_vacuum is a
standard SQLite PRAGMA — neither is an OpenCode-specific API. The OpenCode DB
layer already uses SQLite (WAL, synchronous, busy_timeout, cache_size,
wal_checkpoint), so enabling compaction does not require a new dependency.


If useful, I can provide the compacted DB stats, the original migration list,
or a small standalone maintenance script that checks freelist_count/page_count
and vacuums when needed.

Plugins

No response

OpenCode version

No response

Steps to reproduce

No response

Screenshot

No response

Operating System

No response

Terminal

No response

Activity

  1. opencodejr commented on Nov 16, 2026

    @opencodejr
    opencodejrBot
    on Nov 16, 2026
    Contributor
    More actions

    Thanks for the detailed measurements and for the steps you took to recover the database without losing anything.

    I think this is a duplicate of #47591. Your report and that issue describe the same storage problem: v2 databases have auto_vacuum off, so space freed by deleted data is never given back and opencode.db stays at its largest size. A comment there shows the same situation as yours, a data directory upgraded from V1 where most of the file was free pages until a manual VACUUM. In the code I checked, I found nothing that runs VACUUM, auto_vacuum or incremental_vacuum, which agrees with your diagnosis.

    Possible duplicates and related issues, ordered from most to least likely:

    • New and incremental core databases do not reclaim deleted pages during their lifecycle #47591: same problem, space freed by deleted data is never reclaimed. Open PRs refactor(core): enable incremental auto-vacuum before WAL initialization #56365 and fix(core): reclaim deleted SQLite pages incrementally #58730 there implement incremental auto-vacuum.
    • storage: opencode.db at ~687 MB with 77.9% of the file in a single session.revert cell, and desktop drafts.sqlite at 153 MB that is 99.5% free pages #55282: a very large opencode.db, and a desktop store that is 99.5% free pages.
    • Managed background service startup can take minutes and saturates the disk; the CLI's short health check turns it into a spawn/retry storm #54810: slow background-service startup that saturates the disk.

    I'm closing this issue now as a duplicate of #47591. Please add your numbers there (free-page share before and after, the WSL2 memory effect, and the ~4 GB still used at startup after compacting), since that's where this is tracked. If you think the remaining startup memory is a separate problem, reply here and the issue will reopen.

  2. opencodejr
    closed this as a duplicateof New and incremental core databases do not reclaim deleted pages during their lifecycle #47591 on Nov 16, 2026
  3. opencodejr
    added
    area/sessions-storageSessions, history and stored data
    platform/wslWindows Subsystem for Linux
    type/bugSomething is broken
    version/v2OpenCode 2.x
    on Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/sessions-storageSessions, history and stored dataplatform/wslWindows Subsystem for Linuxtype/bugSomething is brokenversion/v2OpenCode 2.x

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.