codegraph icon indicating copy to clipboard operation
codegraph copied to clipboard

[bug]: Javascript heap out of memory: FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory

Open mosjin opened this issue 6 months ago • 7 comments

1. env: Window 10, Windows Terminal Version: 1.24.11321.0

codegraph --version 0.9.8

2. Installed codegraph by below cmd:

irm https://raw.githubusercontent.com/colbymchenry/codegraph/main/install.ps1 | iex

3. Enter into local dir and run codegraph init ., then encounter below error:

codegraph init . ┌ Initializing CodeGraph │ ◆ Initialized in D:\works\csp_s_teach_article | | . Scanning files... 3,160 found <--- Last few GCs --->

[19172:0000022E9303F000] 558962 ms: Mark-Compact 4025.1 (4159.9) -> 3993.0 (4160.2) MB, pooled: 0 MB, 92.50 / 0.00 ms (average mu = 0.545, current mu = 0.618) allocation failure; scavenge might not succeed [19172:0000022E9303F000] 559300 ms: Mark-Compact 4025.2 (4160.2) -> 3996.4 (4163.4) MB, pooled: 1 MB, 127.07 / 0.00 ms (average mu = 0.596, current mu = 0.624) allocation failure; scavenge might not succeed

FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory ----- Native stack trace -----

| . Scanning files... 3,160 found 1: 00007FF735EC0CCF node::OnFatalError+1343 2: 00007FF736B0F697 v8::Function::NewInstance+423 3: 00007FF7369100C7 v8::base::AddressSpaceReservation::AddressSpaceReservation+322071 4: 00007FF73690DD66 v8::base::AddressSpaceReservation::AddressSpaceReservation+313014 5: 00007FF7369283DA X509_STORE_set_cleanup+5098 6: 00007FF736934238 uv_timer_set_repeat+19096 7: 00007FF73694F595 uv_timer_set_repeat+130549 8: 00007FF73694F4FC uv_timer_set_repeat+130396 9: 00007FF736072A03 v8::internal::Version::GetVersion+102915 10: 00007FF736B3CBE8 v8::String::ValueView::ValueView+456 11: 00007FF735CD219B uv_backend_fd+316523 12: 00007FF735CD24E9 uv_backend_fd+317369 13: 00007FF735EA3E0F EVP_MD_meth_get_input_blocksize+72559 14: 0000022ECAECF77E

mosjin avatar Jun 01 '26 05:06 mosjin

I looked into this and reproduced the OOM mechanism. Sharing findings + a question on fix direction.

Reproduction

A project of symbol-dense files (each producing tens of thousands of nodes) reproduces it. Synthetic repro — 15 files, each ~566 KB / 14,000 trivial functions:

for (let f = 0; f < 15; f++) {
  const parts = [];
  for (let i = 0; i < 14000; i++) parts.push(`function fn${f}_${i}(a,b){return a+b+${i};}`);
  fs.writeFileSync(`big${f}.js`, parts.join(''));
}

codegraph init on this → 210,015 nodes, 9m 9s, heap peak 535 MB (a normal 2,360-file TS project: ~19 s, ~30 MB). More such files → hits the 4 GB V8 limit → the FATAL OOM here.

Two distinct problems (from --trace-gc, heap vs. time)

Phase Wall time Heap
scanning + parsing + storing 549 s flat ~13 MB
resolving last 14 s 13 MB → 535 MB spike

1. OOM — resolution materializes the whole graph in JS heap. The batched ref-resolution loop (resolveAndPersistBatched) stays flat. The spike is after it, in synthesizeCallbackEdges (src/resolution/callback-synthesizer.ts:1182): several passes do [...queries.getNodesByKind('method'), ...queries.getNodesByKind('function')] — e.g. fieldChannelEdges (L102), closureCollectionEdges (L174) — materializing every function/method node at once, then merging all passes' edges into one array (L1198). On a symbol-dense graph this is what blows the 4 GB heap.

2. Slowness — parsing/storing symbol-dense files is very slow (549 s for 210k nodes, ~2.6 ms/node; heap flat, so not a leak). Likely why the progress bar looks frozen at "Scanning files… 3,160" — resolution runs synchronously and the shimmer can't flush.

For @mosjin — what kind of files?

Could you confirm what csp_s_teach_article contains — any minified JS bundles, generated code, or large data-as-code files (big const/enum tables)? codegraph init --verbose would also show which file it stalls on. (Files just under the 1 MB MAX_FILE_SIZE but symbol-dense slip past the existing guard.)

For maintainer — fix direction?

  • (A) Skip symbol-dense / minified files at the source — extend the MAX_FILE_SIZE guard with a minified/symbol-density heuristic (max line length, or a per-file node-count cap). Fixes both OOM and slowness; matches the existing "don't index files with no useful symbols" intent (the MAX_FILE_SIZE comment already names minified bundles).
  • (B) Stream the synthesis passes instead of materializing all nodes — more general (helps genuinely large normal projects too) but a bigger change to the resolution layer.

Happy to send a PR once the direction's clear.

maxmilian avatar Jun 01 '26 09:06 maxmilian

Fixed in #653 — you were both right that it's real; I reproduced and root-caused it.

It's not the parsing (that's already bounded). It's the dynamic-edge synthesis phase: the callback/observer synthesizers loaded every function and method node into memory at once just to scan-and-filter them down to a tiny subset — gigabytes on a symbol-dense project.

Tracing peak memory over an index of 80 files × 14k functions (~1.1M nodes):

  • parsing stays flat at ~650 MB,
  • then synthesis spikes to 3.7 GB — the materialization. On a larger project that blows past the ~4 GB heap → "JavaScript heap out of memory."

#653 streams those scans instead. Same repro now peaks at 1.3 GB (was 3.7 GB), no OOM, and the synthesized edges are unchanged (full suite green). @maxmilian — your 15-files-×-14k-functions repro was exactly the right shape to find this; thank you.

Closes on merge of #653.

colbymchenry avatar Jun 02 '26 19:06 colbymchenry

Update: #653 is now merged to main and will ship in the upcoming 1.0 release (it's on main today if you want it sooner). Thanks again for the repro — it's what pinned this down.

colbymchenry avatar Jun 02 '26 19:06 colbymchenry

I’m still running into this issue in version 1.0.1.

My project is a secondary development based on ClickHouse. When I run codegraph init and codegraph index , I still encounter the same OOM issue.

Maybe you need to reopen this issue.

I’ve attached my log report below.

Image
bash-5.3$ codegraph --version
1.0.1
bash-5.3$ codegraph index
┌  Indexing project
│
│  ◆ Scanning files — 401,007 found
│  ◆ Parsing code — done
│  ✽ Resolving refs  ░░░░░░░░░░░░░░░░░░░░░░░░░  0%
<--- Last few GCs --->
 c[41012:0xb7740c000]  1160591 ms: Mark-Compact (reduce) 4005.1 (4063.2) -> 4005.0 (4055.5) MB, pooled: 0 MB, 437.67 / 0.00 ms  (+ 17.8 ms in 3 steps since start of marking, biggest step 7.1 ms, walltime since start of marking 478 ms) (average mu = 0.872, c[41012:0xb7740c000]  1161496 ms: Mark-Compact (reduce) 4047.6 (4097.2) -> 4047.5 (4097.5) MB, pooled: 0 MB, 493.62 / 0.00 ms  (+ 5.0 ms in 1 steps since start of marking, biggest step 5.0 ms, walltime since start of marking 516 ms) (average mu = 0.801, cu
FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
----- Native stack trace -----

│  ✽ Resolving refs  ░░░░░░░░░░░░░░░░░░░░░░░░░  0% 1: 0x100c7a0fc node::OOMErrorHandler(char const*, v8::OOMDetails const&) [/Users/chengjisheng/.nvm/versions/node/v22.18.0/lib/node_modules/@colbymchenry/codegraph/node_modules/@colbymchenry/codegraph-darwin-arm64/node]
 2: 0x100ed64b8 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, v8::OOMDetails const&) [/Users/chengjisheng/.nvm/versions/node/v22.18.0/lib/node_modules/@colbymchenry/codegraph/node_modules/@colbymchenry/codegraph-darwin-arm64/node]
 3: 0x1011358a4 v8::internal::Heap::stack() [/Users/chengjisheng/.nvm/versions/node/v22.18.0/lib/node_modules/@colbymchenry/codegraph/node_modules/@colbymchenry/codegraph-darwin-arm64/node]
 4: 0x10113378c v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [/Users/chengjisheng/.nvm/versions/node/v22.18.0/lib/node_modules/@colbymchenry/codegraph/node_modules/@colbymchenry/codegraph-darwin-arm64/node]
 5: 0x1011282b4 v8::internal::HeapAllocator::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [/Users/chengjisheng/.nvm/versions/node/v22.18.0/lib/node_modules/@colbymchenry/codegraph/node_modules/@colbymchenry/codegraph-darwin-arm64/node]
 6: 0x101109994 v8::internal::Factory::NewFillerObject(int, v8::internal::AllocationAlignment, v8::internal::AllocationType, v8::internal::AllocationOrigin) [/Users/chengjisheng/.nvm/versions/node/v22.18.0/lib/node_modules/@colbymchenry/codegraph/node_modules/@colbymchenry/codegraph-darwin-arm64/node]
│  ✽ Resolving refs  ░░░░░░░░░░░░░░░░░░░░░░░░░  0% 7: 0x1015996d8 v8::internal::Runtime_AllocateInOldGeneration(int, unsigned long*, v8::internal::Isolate*) [/Users/chengjisheng/.nvm/versions/node/v22.18.0/lib/node_modules/@colbymchenry/codegraph/node_modules/@colbymchenry/codegraph-darwin-arm64/node]
 8: 0x101bee234 Builtins_CEntry_Return1_ArgvOnStack_NoBuiltinExit [/Users/chengjisheng/.nvm/versions/node/v22.18.0/lib/node_modules/@colbymchenry/codegraph/node_modules/@colbymchenry/codegraph-darwin-arm64/node]
 9: 0x124c3b19c
10: 0x101c1de60 Builtins_ArrayMap [/Users/chengjisheng/.nvm/versions/node/v22.18.0/lib/node_modules/@colbymchenry/codegraph/node_modules/@colbymchenry/codegraph-darwin-arm64/node]
11: 0x101b50bec Builtins_InterpreterEntryTrampoline [/Users/chengjisheng/.nvm/versions/node/v22.18.0/lib/node_modules/@colbymchenry/codegraph/node_modules/@colbymchenry/codegraph-darwin-arm64/node]
12: 0x101b50bec Builtins_InterpreterEntryTrampoline [/Users/chengjisheng/.nvm/versions/node/v22.18.0/lib/node_modules/@colbymchenry/codegraph/node_modules/@colbymchenry/codegraph-darwin-arm64/node]
13: 0x101b50bec Builtins_InterpreterEntryTrampoline [/Users/chengjisheng/.nvm/versions/node/v22.18.0/lib/node_modules/@colbymchenry/codegraph/node_modules/@colbymchenry/codegraph-darwin-arm64/node]
14: 0x101b50bec Builtins_InterpreterEntryTrampoline [/Users/chengjisheng/.nvm/versions/node/v22.18.0/lib/node_modules/@colbymchenry/codegraph/node_modules/@colbymchenry/codegraph-darwin-arm64/node]
15: 0x101b516e0 Builtins_InterpreterEnterAtBytecode [/Users/chengjisheng/.nvm/versions/node/v22.18.0/lib/node_modules/@colbymchenry/codegraph/node_modules/@colbymchenry/codegraph-darwin-arm64/node]
16: 0x124c28a74
17: 0x124ccd380
18: 0x101b50bec Builtins_InterpreterEntryTrampoline [/Users/chengjisheng/.nvm/versions/node/v22.18.0/lib/node_modules/@colbymchenry/codegraph/node_modules/@colbymchenry/codegraph-darwin-arm64/node]
19: 0x101b50bec Builtins_InterpreterEntryTrampoline [/Users/chengjisheng/.nvm/versions/node/v22.18.0/lib/node_modules/@colbymchenry/codegraph/node_modules/@colbymchenry/codegraph-darwin-arm64/node]
20: 0x101b50bec Builtins_InterpreterEntryTrampoline [/Users/chengjisheng/.nvm/versions/node/v22.18.0/lib/node_modules/@colbymchenry/codegraph/node_modules/@colbymchenry/codegraph-darwin-arm64/node]
21: 0x101b8f7e8 Builtins_AsyncFunctionAwaitResolveClosure [/Users/chengjisheng/.nvm/versions/node/v22.18.0/lib/node_modules/@colbymchenry/codegraph/node_modules/@colbymchenry/codegraph-darwin-arm64/node]
22: 0x101c64e58 Builtins_PromiseFulfillReactionJob [/Users/chengjisheng/.nvm/versions/node/v22.18.0/lib/node_modules/@colbymchenry/codegraph/node_modules/@colbymchenry/codegraph-darwin-arm64/node]
23: 0x101b7ebd0 Builtins_RunMicrotasks [/Users/chengjisheng/.nvm/versions/node/v22.18.0/lib/node_modules/@colbymchenry/codegraph/node_modules/@colbymchenry/codegraph-darwin-arm64/node]
24: 0x101b4e7b0 Builtins_JSRunMicrotasksEntry [/Users/chengjisheng/.nvm/versions/node/v22.18.0/lib/node_modules/@colbymchenry/codegraph/node_modules/@colbymchenry/codegraph-darwin-arm64/node]
25: 0x101079a4c v8::internal::(anonymous namespace)::Invoke(v8::internal::Isolate*, v8::internal::(anonymous namespace)::InvokeParams const&) [/Users/chengjisheng/.nvm/versions/node/v22.18.0/lib/node_modules/@colbymchenry/codegraph/node_modules/@colbymchenry/codegraph-darwin-arm64/node]
26: 0x10107a2c0 v8::internal::(anonymous namespace)::InvokeWithTryCatch(v8::internal::Isolate*, v8::internal::(anonymous namespace)::InvokeParams const&) [/Users/chengjisheng/.nvm/versions/node/v22.18.0/lib/node_modules/@colbymchenry/codegraph/node_modules/@colbymchenry/codegraph-darwin-arm64/node]
27: 0x10107a3fc v8::internal::Execution::TryRunMicrotasks(v8::internal::Isolate*, v8::internal::MicrotaskQueue*) [/Users/chengjisheng/.nvm/versions/node/v22.18.0/lib/node_modules/@colbymchenry/codegraph/node_modules/@colbymchenry/codegraph-darwin-arm64/node]
28: 0x1010a91d0 v8::internal::MicrotaskQueue::RunMicrotasks(v8::internal::Isolate*) [/Users/chengjisheng/.nvm/versions/node/v22.18.0/lib/node_modules/@colbymchenry/codegraph/node_modules/@colbymchenry/codegraph-darwin-arm64/node]
29: 0x1010a9b30 v8::internal::MicrotaskQueue::PerformCheckpoint(v8::Isolate*) [/Users/chengjisheng/.nvm/versions/node/v22.18.0/lib/node_modules/@colbymchenry/codegraph/node_modules/@colbymchenry/codegraph-darwin-arm64/node]
30: 0x100b5b0b8 node::InternalCallbackScope::Close() [/Users/chengjisheng/.nvm/versions/node/v22.18.0/lib/node_modules/@colbymchenry/codegraph/node_modules/@colbymchenry/codegraph-darwin-arm64/node]
31: 0x100b5b3b0 node::InternalMakeCallback(node::Environment*, v8::Local<v8::Object>, v8::Local<v8::Object>, v8::Local<v8::Function>, int, v8::Local<v8::Value>*, node::async_context, v8::Local<v8::Value>) [/Users/chengjisheng/.nvm/versions/node/v22.18.0/lib/node_modules/@colbymchenry/codegraph/node_modules/@colbymchenry/codegraph-darwin-arm64/node]
32: 0x100b5b6b8 node::InternalMakeCallback(v8::Isolate*, v8::Local<v8::Object>, v8::Local<v8::Function>, int, v8::Local<v8::Value>*, node::async_context, v8::Local<v8::Value>) [/Users/chengjisheng/.nvm/versions/node/v22.18.0/lib/node_modules/@colbymchenry/codegraph/node_modules/@colbymchenry/codegraph-darwin-arm64/node]
33: 0x100bf217c node::Environment::CheckImmediate(uv_check_s*) [/Users/chengjisheng/.nvm/versions/node/v22.18.0/lib/node_modules/@colbymchenry/codegraph/node_modules/@colbymchenry/codegraph-darwin-arm64/node]
34: 0x101b3548c uv__run_check [/Users/chengjisheng/.nvm/versions/node/v22.18.0/lib/node_modules/@colbymchenry/codegraph/node_modules/@colbymchenry/codegraph-darwin-arm64/node]
35: 0x101b2e2d4 uv_run [/Users/chengjisheng/.nvm/versions/node/v22.18.0/lib/node_modules/@colbymchenry/codegraph/node_modules/@colbymchenry/codegraph-darwin-arm64/node]
36: 0x100b5c6a8 node::SpinEventLoopInternal(node::Environment*) [/Users/chengjisheng/.nvm/versions/node/v22.18.0/lib/node_modules/@colbymchenry/codegraph/node_modules/@colbymchenry/codegraph-darwin-arm64/node]
37: 0x100cd300c node::NodeMainInstance::Run() [/Users/chengjisheng/.nvm/versions/node/v22.18.0/lib/node_modules/@colbymchenry/codegraph/node_modules/@colbymchenry/codegraph-darwin-arm64/node]
38: 0x100c26d14 node::Start(int, char**) [/Users/chengjisheng/.nvm/versions/node/v22.18.0/lib/node_modules/@colbymchenry/codegraph/node_modules/@colbymchenry/codegraph-darwin-arm64/node]
39: 0x18cbc3e00 start [/usr/lib/dyld]

@colbymchenry

jischeng avatar Jun 15 '26 08:06 jischeng

I’d be happy to submit an MR to help fix this issue if that would be useful.

jischeng avatar Jun 15 '26 08:06 jischeng

Hi, I’ve opened a PR that attempts to address this issue. Please feel free to take a look when you have time. I’d be happy to make any changes if needed. @colbymchenry

jischeng avatar Jun 15 '26 12:06 jischeng

I also have this issue on codegraph 1.3.0 with my project containing a large SDK (>100,000 files are indexed). After increasing the heap limit to 8 Gb the project was indexed, there were 1.3M nodes and 2.9M edges.

me21 avatar Jul 09 '26 11:07 me21