Skip to content

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

@jiangliang79

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

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingcoreAnything pertaining to core functionality of the application (opencode server stuff)perfIndicates a performance issue or need for optimization

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions