Skip to content

fix: disable SQLite mmap to prevent memory growing with DB size#22428

Open
jiangliang79 wants to merge 1 commit intoanomalyco:devfrom
jiangliang79:fix/sqlite-mmap-memory
Open

fix: disable SQLite mmap to prevent memory growing with DB size#22428
jiangliang79 wants to merge 1 commit intoanomalyco:devfrom
jiangliang79:fix/sqlite-mmap-memory

Conversation

@jiangliang79
Copy link
Copy Markdown

@jiangliang79 jiangliang79 commented Apr 14, 2026

Issue for this PR

Closes #22429

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Bun's bundled SQLite enables memory-mapped I/O by default, which maps the entire DB file into the process address space. This causes the process footprint to grow proportionally with the DB file size regardless of actual data access patterns.
Observed on macOS ARM64: a 1.1 GB opencode.db results in ~1.8 GB of IOAccelerator memory in the process footprint snapshot.
Setting PRAGMA mmap_size = 0 restores SQLite's official default behavior, bounding SQLite's memory contribution to the cache_size limit (~64 MB). The performance impact is negligible — sub-millisecond extra latency on cache misses, dwarfed by LLM API call latency.

How did you verify your code works?

Took a footprint snapshot of the opencode process on macOS ARM64 before and after the change with a 1.1 GB DB. The IOAccelerator region (SQLite mmap pages) dropped from ~1.8 GB to negligible. Functional behavior is unchanged — mmap only affects how pages are loaded from disk, not what data SQLite returns.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Bun's bundled SQLite enables memory-mapped I/O by default, which maps the entire DB file into the process address space. This causes the process footprint to grow proportionally with the DB file size regardless of actual data access patterns.
Observed on macOS ARM64: a 1.1 GB opencode.db results in ~1.8 GB of IOAccelerator memory in the process footprint snapshot.
Setting PRAGMA mmap_size = 0 restores SQLite's official default behavior, bounding SQLite's memory contribution to the cache_size limit (~64 MB). The performance impact is negligible — sub-millisecond extra latency on cache misses, dwarfed by LLM API call latency.
@github-actions
Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions github-actions bot added needs:compliance This means the issue will auto-close after 2 hours. and removed needs:issue labels Apr 14, 2026
zhumengzhu pushed a commit to zhumengzhu/opencode that referenced this pull request Apr 14, 2026
Two independent fixes for memory/stability issues:

1. SQLite mmap_size = 0: Bun enables mmap by default, causing the DB file
   size to be mapped directly into the process address space. A 1.1 GB DB
   results in ~1.8 GB of additional memory footprint. Disabling mmap keeps
   memory usage proportional to active cache, not file size. (ref anomalyco#22428)

2. GlobalBus.setMaxListeners(100): each SSE connection registers one listener
   on GlobalBus and removes it on disconnect. The default Node.js limit of 10
   triggers MaxListenersExceededWarning during normal usage with multiple
   concurrent connections. 100 is a safe upper bound without hiding real leaks.
   (ref anomalyco#22422)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@github-actions github-actions bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Apr 14, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

High memory usage: Bun's SQLite maps entire DB file into process address space via mmap

1 participant