[Crash]: FATAL ERROR: Ineffective mark-compacts near heap limit - JavaScript heap out of memory after long-running session (~182.4M tokens, 20h uptime)

Aperta
#30,352 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@GooseKIller ci sta già lavorando.

Dal 19/11/2026.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
25/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
node.js, typescript
Ambito
backend, cli

Direzione di ricerca

Start by reading packages/cli/src/ui/hooks/useMemoryMonitor.ts and the history retention paths named in the report: packages/core/src/core/agentChatHistory.ts, packages/cli/src/ui/hooks/useHistoryManager.ts, and MainContent.tsx. Compare the reported RSS warning threshold with V8 heap usage and the session persistence flow. The report proposes dynamic warnings, bounded history, and emergency snapshots; done means the chosen behavior is bounded and users receive a warning or recoverable checkpoint before heap exhaustion.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

status/need-triage
What happened?

During an extended interactive Gemini CLI session running for 72,130,808 ms (~20.03 hours) with 182.4 million cumulative tokens processed, the process crashed with a fatal V8 out-of-memory error:

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
Aborted (core dumped)

At the time of the crash:

  • The status bar indicated:
    • Memory (RSS): 3.32 GB
    • LLM Context Utilization: 32% used (the wire context window was healthy and well within model bounds due to context pruning/summarization)
    • Quota Utilization: 28% used
    • Cumulative Tokens: 182.4m tokens
    • Status: ⠹ Thinking... (esc to cancel, 2m 47s)
  • The V8 garbage collector attempted consecutive scavenge and mark-compact cycles near the 4,099.7 MB heap limit. The mark-compact collection reclaimed only ~23.6 MB (4062.0 -> 4038.4 MB), demonstrating that >99% of heap objects were retained live references that could not be freed.
  • Because the built-in MEMORY_WARNING_THRESHOLD in packages/cli/src/ui/hooks/useMemoryMonitor.ts is hardcoded to a static 7 GB (7 * 1024 * 1024 * 1024), the warning was never triggered and no alert/heap snapshot was captured before the fatal crash.
  • The process aborted abruptly with a core dump, resulting in loss of active session state.

What did you expect to happen?
  1. Bounded Heap Retention: Memory usage in long-running interactive sessions should remain bounded or grow sub-linearly. Historical turns and cold tool outputs should be evicted from the active in-memory heap or paged out to disk session logs (~/.gemini/history/).
  2. Adaptive Memory Warning: useMemoryMonitor should monitor memory usage relative to the active V8 heap limit (v8.getHeapStatistics().heap_size_limit), warning users at ~75–80% of the active heap limit (e.g. ~3.2 GB on a 4 GB heap) rather than relying on a static 7 GB threshold that can never be reached on default 4 GB heap environments.
  3. Emergency Checkpoint / Heap Snapshot: When approaching critical heap limits (>85%), the CLI should automatically flush a session checkpoint to disk and capture a diagnostic heap snapshot via v8.getHeapSnapshot() before an unrecoverable process abort occurs.

Client information
Client Information
  • Gemini CLI version: 0.68.0
  • Node.js version: v24.18.0 (/home/<user>/.nvm/versions/node/v24.18.0/bin/node)
  • Operating system: Linux x86_64 (workstation-linux-x64)
  • Model: gemini-3.8-flash
  • Active Modes: YOLO mode enabled (Ctrl+Y), No sandbox
  • Session Duration: 72,130,808 ms (~20 hours 2 minutes)
  • Cumulative Tokens: 182.4m tokens
  • Heap Limit at Crash: 4,099.7 MB (~4.09 GB default 64-bit V8 space)
  • RSS at Crash: 3.32 GB

Login information

Logged in via Google Account / OAuth credentials.


Anything else we need to know?
1. Terminal Output and Status Line Prior to Crash
YOLO Ctrl+Y 2 GEMINI.md files · 1 MCP server · 66 skills
* Type your message or @path/to/file
workspace (/directory) branch sandbox /model quota context memory session machine /auth diff tokens
~/workspace-app fix/history-canvas-ftp-1772-rows no sandbox gemini-3.8-flash 28% used 32% used 3.32 GB 853b90b0 workstation-linux-x64 <user>@<org>.com +1696 -731 182.4m tokens 3 errors (F12 for details)
⠹ Thinking... (esc to cancel, 2m 47s) ? for shortcuts
2. V8 GC Logs and Native Stack Trace
<--- Last few GCs --->
[1900208:0x4415c000] 72130243 ms: Scavenge (interleaved) 4056.2 (4081.0) -> 4054.9 (4099.5) MB, pooled: 0 MB, 33.36 / 0.00 ms (average mu = 0.318, current mu = 0.322) allocation failure;
[1900208:0x4415c000] 72130808 ms: Mark-Compact (reduce) 4062.0 (4099.7) -> 4038.4 (4060.4) MB, pooled: 0 MB, 21.48 / 0.00 ms (+ 492.2 ms in 98 steps since start of marking, biggest step 5.4 ms, walltime since start of marking 557 ms) (average mu = 0.284,
FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory

----- Native stack trace -----
1: 0x74eae8 node::OOMErrorHandler(char const*, v8::OOMDetails const&) [/home/<user>/.nvm/versions/node/v24.18.0/bin/node]
2: 0xc42680 [/home/<user>/.nvm/versions/node/v24.18.0/bin/node]
3: 0xc4276f [/home/<user>/.nvm/versions/node/v24.18.0/bin/node]
4: 0xee5fc5 [/home/<user>/.nvm/versions/node/v24.18.0/bin/node]
5: 0xee5ff2 [/home/<user>/.nvm/versions/node/v24.18.0/bin/node]
6: 0xee62ea [/home/<user>/.nvm/versions/node/v24.18.0/bin/node]
7: 0xef6fea [/home/<user>/.nvm/versions/node/v24.18.0/bin/node]
8: 0xefb390 [/home/<user>/.nvm/versions/node/v24.18.0/bin/node]
9: 0x198d931 [/home/<user>/.nvm/versions/node/v24.18.0/bin/node]
Aborted (core dumped)

3. Root Cause Analysis & Codebase Investigation

Investigation of the codebase reveals three intersecting factors:

A. Unbounded In-Memory Retention in AgentChatHistory and useHistory

While AgentHistoryProvider (packages/core/src/context/agentHistoryProvider.ts) successfully summarizes and truncates context payloads sent over the wire to the Gemini API (which is why context usage was only 32%), the underlying in-memory data structures retain all historical turns indefinitely:

  • AgentChatHistory (packages/core/src/core/agentChatHistory.ts:23-43):
    this.history is an unbounded array of HistoryTurn. Every user prompt, model reply, and massive tool output (such as large file reads, terminal diffs, and data rows) is appended to this.history and never evicted.
  • useHistory (packages/cli/src/ui/hooks/useHistoryManager.ts:44):
    The UI hook stores all turns in React state (useState<HistoryItem[]>), causing the Ink renderer (MainContent.tsx:453-462) to retain an ever-expanding tree of component objects.
B. Inoperative Memory Warning Threshold on Default 4 GB Heaps

In packages/cli/src/ui/hooks/useMemoryMonitor.ts:

export const MEMORY_WARNING_THRESHOLD = 7 * 1024 * 1024 * 1024; // 7GB in bytes
export const MEMORY_CHECK_INTERVAL = 60 * 1000; // one minute
...
const usage = process.memoryUsage().rss;
if (usage > MEMORY_WARNING_THRESHOLD) {
  addItem({
    type: MessageType.WARNING,
    text: `High memory usage detected: ${(usage / (1024 * 1024 * 1024)).toFixed(2)} GB. If you experience a crash, please file a bug report by running /bug`,
  });
}

64-bit Node.js has a default V8 max heap limit of ~4,096 MB (~4.09 GB). An application running under default memory flags crashes at ~4 GB long before usage can ever reach 7 GB. As a result, the built-in warning and the /bug auto-snapshot mechanism (MEMORY_SNAPSHOT_AUTO_THRESHOLD_BYTES = 2 GB in bugCommand.ts) are never brought to the user's attention.

C. Memory Auto-Configuration Boundary Condition

In packages/cli/index.ts:

const totalMemoryMB = os.totalmem() / (1024 * 1024);
const heapStats = v8.getHeapStatistics();
const currentMaxOldSpaceSizeMb = Math.floor(heapStats.heap_size_limit / 1024 / 1024);
const targetMaxOldSpaceSizeInMB = Math.floor(totalMemoryMB * 0.5);

if (targetMaxOldSpaceSizeInMB > currentMaxOldSpaceSizeMb) {
  return [`--max-old-space-size=${targetMaxOldSpaceSizeInMB}`];
}

On machines with 8 GB RAM (or when relaunch is skipped / unconfigured), 8192 * 0.5 = 4096 MB, so targetMaxOldSpaceSizeInMB <= currentMaxOldSpaceSizeMb and no --max-old-space-size flag is added, leaving the heap limit capped at ~4 GB.


4. Suggested Mitigations
  1. Make useMemoryMonitor Dynamic & Heap-Aware:
    Instead of a hardcoded 7 GB threshold, compare against v8.getHeapStatistics().heap_size_limit:
    const heapStats = v8.getHeapStatistics();
    const heapWarningThreshold = heapStats.heap_size_limit * 0.80; // 80% of max heap
    if (heapStats.used_heap_size > heapWarningThreshold) {
      // Warn user to run /compress, /clear, or /bug
    }
    
  2. Implement Bounded In-Memory Window for Core & UI:
    Introduce a maximum in-memory history turn window (e.g., retain the last 50–100 turns in active memory, offloading older turns to the session JSONL file on disk).
  3. Emergency Auto-Snapshot / Checkpoint:
    When heap usage exceeds 85% of heap_size_limit, trigger an automatic session flush and dump a diagnostic snapshot via captureHeapSnapshot().
Lingua principale
TypeScript
Stelle
109k
Fork
15k
Merge medio
1g 24m
PR unite (30g)
91

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di google-gemini/gemini-cli

Tutte le issue di google-gemini/gemini-cli

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.