Skip to content

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

Closed as not planned

Description

@jiangliang79

Description

Environment

  • Platform: macOS ARM64
  • opencode version: latest dev
  • DB size: ~1.1 GB

Problem

On long-running sessions, the opencode process footprint grows to several GB. Profiling with footprint on macOS shows ~1.8 GB attributed to IOAccelerator — this is SQLite's memory-mapped pages, not actual application data.

The root cause: Bun's bundled SQLite sets a large mmap_size by default, unlike upstream SQLite which defaults to mmap_size = 0. As a result, the entire DB file is mapped into the process address space. With a 1.1 GB opencode.db, this alone accounts for ~1.8 GB of process footprint regardless of how much data is actually accessed.

Fix

Adding PRAGMA mmap_size = 0 in db.ts restores the standard SQLite default. SQLite then uses its internal page cache (already configured at ~64 MB via cache_size = -64000) instead of mmap. This caps SQLite's memory contribution at ~64 MB regardless of DB file size.

The performance impact is negligible — mmap vs read() is a sub-millisecond difference on cache misses, completely dwarfed by LLM API call latency.

I have a PR ready for this.

Plugins

No response

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
    coreAnything pertaining to core functionality of the application (opencode server stuff)
    perfIndicates a performance issue or need for optimization
    on Apr 14, 2026
  2. github-actions commented on Apr 14, 2026

    @github-actions
    Contributor

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

    If your issue is distinct (e.g., specifically about SQLite mmap being the root cause), please add a comment to the Memory Megathread (#20695) linking back here, as maintainers are consolidating memory reports there.

  3. assigned and unassigned on Apr 26, 2026
  4. removed
    bugSomething isn't working
    perfIndicates a performance issue or need for optimization
    coreAnything pertaining to core functionality of the application (opencode server stuff)
    on May 3, 2026
  5. github-actions commented on Jul 3, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions