Skip to content

Memory leaks #8570

@yiv1

Description

@yiv1

System information

Describe the current behavior
If you run training on a larger amount of data and a complex neural network model, then a memory leak causes a crash. Even if the Working Set stabilizes, Private Bytes continues to grow infinitely, reaching some limits

2025-06-26 03:02:12.840574: W tensorflow/core/framework/op_kernel.cc:1840] OP_REQUIRES failed at cwise_ops_common.h:124 : RESOURCE_EXHAUSTED: OOM when allocating tensor with shape[32,16] and type float on /job:localhost/replica:0/task:0/device:CPU:0 by allocator mklcpu
============================================================
Bun v1.2.17 (282dda62) Windows x64
Windows v.win10_fe
CPU: sse42 avx avx2
Args: "C:\Users\****\.bun\bin\bun.exe" "src/index.ts"
Features: Bun.stderr Bun.stdout(3) jsc transpiler_cache(9) tsconfig(2) process_dlopen
Builtins: "bun:main" "bun:sqlite" "node:assert" "node:buffer" "node:child_process" "node:crypto" "node:events" "node:fs" "node:os" "node:path" 
"node:stream" "node:url" "node:util"
Elapsed: 52508590ms | User: 54715984ms | Sys: 380281ms
RSS: 4.77GB | Peak: 4.77GB | Commit: 8.95GB | Faults: 80753124

panic(main thread): attempt to unwrap error: OutOfMemory
oh no: Bun has crashed. This indicates a bug in Bun, not your code.

Describe the expected behavior
Private bytes does not grow and there is no out of memory (OOM) error

Standalone code to reproduce the issue

import tf from '@tensorflow/tfjs-node';

const width = 150;
const height = 10;
const forMemoryLeaks = [];

console.time('total time');

for (let i = 0; i < 5_000_000; i++) {
    tf.tidy(() => {
        const stateTensorTemp = tf.tensor2d([Array.from({ length: width * height }, () => Math.random())]);
        const stateTensor = stateTensorTemp.reshape([1, width, height]);
    });

    if (i % 100_000 === 0) {
        forMemoryLeaks.push(process.memoryUsage());
        console.table(forMemoryLeaks);
    }
}

console.timeEnd('total time');

Other info / logs

bun 1.2.17
┌────┬───────────┬───────────┬──────────┬──────────┬──────────────┐
│    │ rss       │ heapTotal │ heapUsed │ external │ arrayBuffers │
├────┼───────────┼───────────┼──────────┼──────────┼──────────────┤
│  0 │ 335286272 │ 9522176   │ 234333   │ 40781    │ 0            │
│  1 │ 415936512 │ 25235456  │ 17000145 │ 4817217  │ 0            │
│  2 │ 386093056 │ 31961088  │ 22685473 │ 4853361  │ 0            │
│  3 │ 389959680 │ 37982208  │ 28366497 │ 4901505  │ 0            │
│  4 │ 403562496 │ 44118016  │ 34018551 │ 4925559  │ 0            │
│  5 │ 417083392 │ 50384896  │ 39662233 │ 4967721  │ 0            │
│  6 │ 426631168 │ 56651776  │ 45272825 │ 5003865  │ 0            │
│  7 │ 440102912 │ 62870528  │ 50923321 │ 5046009  │ 0            │
│  8 │ 453619712 │ 69350400  │ 56565757 │ 5076189  │ 0            │
│  9 │ 467386368 │ 75587584  │ 62185977 │ 5124441  │ 0            │
│ 10 │ 476299264 │ 80333824  │ 9441767  │ 2415351  │ 0            │
│ 11 │ 476356608 │ 80333824  │ 17333563 │ 2469675  │ 0            │
│ 12 │ 476372992 │ 80333824  │ 25366075 │ 2499819  │ 0            │
│ 13 │ 476381184 │ 80333824  │ 33327821 │ 2511837  │ 0            │
│ 14 │ 476381184 │ 80333824  │ 41325865 │ 2535945  │ 0            │
│ 15 │ 476393472 │ 80333824  │ 49374729 │ 2541945  │ 0            │
│ 16 │ 476565504 │ 80332800  │ 15177708 │ 2488108  │ 0            │
│ 17 │ 476585984 │ 80332800  │ 22753588 │ 2651044  │ 0            │
│ 18 │ 476585984 │ 80332800  │ 29741832 │ 2952808  │ 0            │
│ 19 │ 476585984 │ 80332800  │ 36739040 │ 3236464  │ 0            │
│ 20 │ 476651520 │ 80332800  │ 43679818 │ 3538138  │ 0            │
│ 21 │ 476921856 │ 80350208  │ 8513242  │ 2403786  │ 0            │
│ 22 │ 476938240 │ 80350208  │ 15338912 │ 2989152  │ 0            │
│ 23 │ 476966912 │ 80350208  │ 21742694 │ 3677094  │ 0            │
│ 24 │ 477151232 │ 80351232  │ 28637808 │ 4027056  │ 0            │
│ 25 │ 477560832 │ 80352256  │ 35541726 │ 4358910  │ 0            │
│ 26 │ 477822976 │ 80354304  │ 42451112 │ 4666728  │ 0            │
│ 27 │ 478085120 │ 80355328  │ 49594276 │ 4883988  │ 0            │
│ 28 │ 478470144 │ 80355328  │ 15196754 │ 2584738  │ 0            │
│ 29 │ 478470144 │ 80355328  │ 21831786 │ 3012682  │ 0            │
│ 30 │ 478470144 │ 80355328  │ 28328578 │ 3488914  │ 0            │
│ 31 │ 478535680 │ 80355328  │ 35007218 │ 3922930  │ 0            │
│ 32 │ 478535680 │ 80355328  │ 41238712 │ 4537288  │ 0            │
│ 33 │ 478703616 │ 80356352  │ 47458600 │ 5127880  │ 0            │
│ 34 │ 479059968 │ 80357376  │ 12294224 │ 2439856  │ 0            │
│ 35 │ 479059968 │ 80357376  │ 20219416 │ 2536360  │ 0            │
│ 36 │ 479068160 │ 80357376  │ 28099624 │ 2632936  │ 0            │
│ 37 │ 479068160 │ 80357376  │ 35324102 │ 2886358  │ 0            │
│ 38 │ 479068160 │ 80357376  │ 42561706 │ 3121690  │ 0            │
│ 39 │ 479068160 │ 80357376  │ 49670750 │ 3429454  │ 0            │
│ 40 │ 479072256 │ 80357376  │ 15236542 │ 2445838  │ 0            │
│ 41 │ 479076352 │ 80357376  │ 23327958 │ 2584630  │ 0            │
│ 42 │ 479141888 │ 80357376  │ 30321928 │ 2952808  │ 0            │
│ 43 │ 479141888 │ 80357376  │ 37340064 │ 3272608  │ 0            │
│ 44 │ 479141888 │ 80357376  │ 44238434 │ 3676786  │ 0            │
│ 45 │ 479211520 │ 80357376  │ 8989388  │ 2397676  │ 0            │
│ 46 │ 479227904 │ 80357376  │ 16379804 │ 2819980  │ 0            │
│ 47 │ 479227904 │ 80357376  │ 23996056 │ 2898376  │ 0            │
│ 48 │ 479232000 │ 80357376  │ 31619600 │ 2970736  │ 0            │
│ 49 │ 479232000 │ 80357376  │ 39494346 │ 3115402  │ 0            │
└────┴───────────┴───────────┴──────────┴──────────┴──────────────┘


node.js v22.9.0
┌─────────┬───────────┬───────────┬──────────┬──────────┬──────────────┐
│ (index) │ rss       │ heapTotal │ heapUsed │ external │ arrayBuffers │
├─────────┼───────────┼───────────┼──────────┼──────────┼──────────────┤
│ 0       │ 116215808 │ 41435136  │ 25531400 │ 8048371  │ 22695        │
│ 1       │ 141303808 │ 60833792  │ 31878784 │ 8427025  │ 395399       │
│ 2       │ 141426688 │ 60833792  │ 33129896 │ 8475121  │ 443495       │
│ 3       │ 141529088 │ 60833792  │ 36780744 │ 8625421  │ 593795       │
│ 4       │ 141611008 │ 60833792  │ 38565640 │ 8697565  │ 665939       │
│ 5       │ 141742080 │ 60833792  │ 40001504 │ 8757685  │ 726059       │
│ 6       │ 141881344 │ 60833792  │ 25050832 │ 8144461  │ 112835       │
│ 7       │ 141946880 │ 60833792  │ 25870688 │ 8180533  │ 148907       │
│ 8       │ 142110720 │ 60833792  │ 26054456 │ 8186545  │ 154919       │
│ 9       │ 142159872 │ 60833792  │ 26052448 │ 8186545  │ 154919       │
│ 10      │ 142565376 │ 60833792  │ 26089056 │ 8186545  │ 154919       │
│ 11      │ 142565376 │ 60833792  │ 26089704 │ 8186545  │ 154919       │
│ 12      │ 142553088 │ 60833792  │ 26793592 │ 8216605  │ 184979       │
│ 13      │ 142557184 │ 60833792  │ 28897192 │ 8300773  │ 269147       │
│ 14      │ 142561280 │ 60833792  │ 30332528 │ 8360893  │ 329267       │
│ 15      │ 142708736 │ 60833792  │ 34644648 │ 8535241  │ 503615       │
│ 16      │ 142708736 │ 60833792  │ 35195320 │ 8559289  │ 527663       │
│ 17      │ 143118336 │ 60833792  │ 36665448 │ 8619409  │ 587783       │
│ 18      │ 143118336 │ 60833792  │ 38581752 │ 8697565  │ 665939       │
│ 19      │ 143171584 │ 60833792  │ 40239920 │ 8763697  │ 732071       │
│ 20      │ 143171584 │ 60833792  │ 25368712 │ 8156485  │ 124859       │
│ 21      │ 143609856 │ 60833792  │ 25325408 │ 8156485  │ 124859       │
│ 22      │ 143613952 │ 60833792  │ 27319824 │ 8234641  │ 203015       │
│ 23      │ 143613952 │ 60833792  │ 29305408 │ 8318809  │ 287183       │
│ 24      │ 143613952 │ 60833792  │ 29347608 │ 8318809  │ 287183       │
│ 25      │ 143613952 │ 60833792  │ 29452416 │ 8324821  │ 293195       │
│ 26      │ 143863808 │ 60833792  │ 33142576 │ 8475121  │ 443495       │
│ 27      │ 151642112 │ 60833792  │ 29757632 │ 8336845  │ 305219       │
│ 28      │ 150396928 │ 60833792  │ 36674872 │ 8619409  │ 587783       │
│ 29      │ 149626880 │ 60833792  │ 27989096 │ 8264701  │ 233075       │
│ 30      │ 148156416 │ 60833792  │ 36273696 │ 8601373  │ 569747       │
│ 31      │ 148353024 │ 60833792  │ 25047592 │ 8144461  │ 112835       │
│ 32      │ 148353024 │ 60833792  │ 25374664 │ 8156485  │ 124859       │
│ 33      │ 148488192 │ 60833792  │ 26228168 │ 8192557  │ 160931       │
│ 34      │ 148508672 │ 60833792  │ 26306128 │ 8192557  │ 160931       │
│ 35      │ 148942848 │ 60833792  │ 26726448 │ 8210593  │ 178967       │
│ 36      │ 148946944 │ 60833792  │ 27465200 │ 8240653  │ 209027       │
│ 37      │ 149377024 │ 60833792  │ 28015872 │ 8264701  │ 233075       │
│ 38      │ 149364736 │ 60833792  │ 30886048 │ 8378929  │ 347303       │
│ 39      │ 149368832 │ 60833792  │ 32767168 │ 8457085  │ 425459       │
│ 40      │ 149393408 │ 60833792  │ 35272896 │ 8559289  │ 527663       │
│ 41      │ 149393408 │ 60833792  │ 37634600 │ 8655481  │ 623855       │
│ 42      │ 149520384 │ 60833792  │ 25521776 │ 8162497  │ 130871       │
│ 43      │ 149524480 │ 60833792  │ 30265736 │ 8354881  │ 323255       │
│ 44      │ 149725184 │ 60833792  │ 32586352 │ 8451073  │ 419447       │
│ 45      │ 149725184 │ 60833792  │ 35386936 │ 8565301  │ 533675       │
│ 46      │ 149716992 │ 60833792  │ 37929784 │ 8667505  │ 635879       │
│ 47      │ 149803008 │ 60833792  │ 40542280 │ 8775721  │ 744095       │
│ 48      │ 149807104 │ 60833792  │ 26256176 │ 8192557  │ 160931       │
│ 49      │ 149803008 │ 60833792  │ 28464616 │ 8282737  │ 251111       │
└─────────┴───────────┴───────────┴──────────┴──────────┴──────────────┘

Private Bytes growth is especially noticeable through Process Explorer
Image
Image

I don't know how else to diagnose the problem, and whether it can be controlled from the js side by changing the code. So far it seems to me that the problem is on the c++ side, and GC js does not have the ability to clean the result of c++ work. But this is just a guess.

Metadata

Metadata

Assignees

Labels

type:bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions