You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Memory leak-like behavior caused by node-addon-api not doing GC synchronously #2436
Can you provide a small repro? Something that loops and calls global.gc() and logs process.memoryUsage() to show it increasing. I can't find anything wrong with the code after a quick look.
Thanks! I've spent a while on it but I'm not sure if I'm able to reproduce a leak or not. All code paths look correct. The only thing I can find is that we might not be giving accurate memory notifications to the JS engine, but that would not be new in 3.0.
It does seem to go up quickly with PNG, and not JPG, but eventually it levels out. That's if I add a small delay to allow GC. Can you try adding a delay as well and see if memory reduces?
I just got around to this. Adding the setTimeout() delay works but makes it slower than using readFile().
Using the promise without setTimeout() in the code above also increases memory usage over time.
I think it's just node. Node doesn't seem release the memory in between the loop when using readFileSync() or native api. It also doesn't seem to release in between the promise microtask. Manually doing global.gc() inside the loop doesn't help either. Maybe it just doesn't have enough time to release.
I think node just looks for time when you're waiting for async macrotask to release the memory and maybe in the case of readFile() there's another thread that runs the gc.
Node only GCing in between macro tasks is a good guess. Thanks for looking. I'll double check that our external memory tracking is accurate and then close this.
I am also experiencing a significant memory leak when moving from canvas ^2.11 to ^3.0. I do not make use of the loadImage function originally mentioned. Tracking here.
It does seem like there is something to fix. My other idea (vs blaming node) is that there is a handle to memory that we aren't releasing until after the macrotask is done. I'll try to use Valgrind or similar when I get some time.
I was also experiencing memory leaks causing nodes to become OOM.
Removing loadImage solved it, would be cool if someone could have a look at it.
Using 3.2
Ran into the same issue and tried the workaround with the macro-sleep. But this just converted the memory leak into a crash. Sometimes it just segfaults without output, sometimes it crashes with this output:
This script generates 10 PDF pages with 15 embedded photos per page (in this simplified test always the same photo). After each page a sleep is performed to give node-canvas time for garbage collection. Without this sleep the program runs into a memory leak. With this sleep it crashes at some random page (sometimes it works for all pages, then just run it again, eventually it will crash).
The image data mode in this case seems to be important. When not embedding the images, then node-canvas does not crash. With node-canvas v2 it doesn't crash with embedding enabled and even without the sleep it works fine without memory leak.
When moving the sleep inside the inner for loop then it works reliably but this isn't practical because the rendering of a page in my real application is done in a synchronous process and the implementation of this rendering is out of my scope. I can only sleep AFTER rendering a single page.
Environment:
Node.js 24.13.0
Canvas 3.2.1 and 2.11.2
Debian Linux 13 with 64 GB RAM
I think my problem with embedding images in PDF is a separate problem to the memory leak described here. So I extracted it with additional data into a new issue #2550
Activity
SystemR commentedon Apr 10, 2025
I can confirm it still happens. I believe it only happens when you pass a path to loadImage and the file descriptor not being closed.
node-canvas/src/Image.cc
Line 188 in b50b392
Passing in buffer from readFileSync also causes memory leak somehow.
My workaround is to use readFile and pass in the buffer:
chearon commentedon Apr 10, 2025
Can you provide a small repro? Something that loops and calls
global.gc()and logsprocess.memoryUsage()to show it increasing. I can't find anything wrong with the code after a quick look.SystemR commentedon Apr 10, 2025
This one will have increased memory usage over time (seeing htop):
This one doesn't:
With readFileSync it also has increased memory usage over time:
Folder has 7000+ photos. I did the expose gc as well and it didn't make a difference.
chearon commentedon Apr 11, 2025
Thanks! I've spent a while on it but I'm not sure if I'm able to reproduce a leak or not. All code paths look correct. The only thing I can find is that we might not be giving accurate memory notifications to the JS engine, but that would not be new in 3.0.
It does seem to go up quickly with PNG, and not JPG, but eventually it levels out. That's if I add a small delay to allow GC. Can you try adding a delay as well and see if memory reduces?
Here's my latest results
examples/leak.js
Output:
SystemR commentedon Apr 18, 2025
I just got around to this. Adding the
setTimeout()delay works but makes it slower than usingreadFile().Using the promise without
setTimeout()in the code above also increases memory usage over time.I think it's just node. Node doesn't seem release the memory in between the loop when using
readFileSync()or native api. It also doesn't seem to release in between the promise microtask. Manually doingglobal.gc()inside the loop doesn't help either. Maybe it just doesn't have enough time to release.I think node just looks for time when you're waiting for async macrotask to release the memory and maybe in the case of
readFile()there's another thread that runs the gc.chearon commentedon Apr 21, 2025
Node only GCing in between macro tasks is a good guess. Thanks for looking. I'll double check that our external memory tracking is accurate and then close this.
H3r3zy commentedon May 21, 2025
I didn't have the issue in 2.X (or minimal leak ?) but when I tried the 3.X my memory consumption had skyrocket
connorhsm commentedon Aug 21, 2025
I am also experiencing a significant memory leak when moving from canvas
^2.11to^3.0. I do not make use of theloadImagefunction originally mentioned. Tracking here.chearon commentedon Aug 21, 2025
It does seem like there is something to fix. My other idea (vs blaming node) is that there is a handle to memory that we aren't releasing until after the macrotask is done. I'll try to use Valgrind or similar when I get some time.
wug-ge commentedon Jan 20, 2026
I was also experiencing memory leaks causing nodes to become OOM.
Removing loadImage solved it, would be cool if someone could have a look at it.
Using 3.2
kayahr commentedon Feb 3, 2026
Ran into the same issue and tried the workaround with the macro-sleep. But this just converted the memory leak into a crash. Sometimes it just segfaults without output, sometimes it crashes with this output:
My test script is this:
The used test.jpeg is a 4032x3024 photo.
This script generates 10 PDF pages with 15 embedded photos per page (in this simplified test always the same photo). After each page a sleep is performed to give node-canvas time for garbage collection. Without this sleep the program runs into a memory leak. With this sleep it crashes at some random page (sometimes it works for all pages, then just run it again, eventually it will crash).
The image data mode in this case seems to be important. When not embedding the images, then node-canvas does not crash. With node-canvas v2 it doesn't crash with embedding enabled and even without the sleep it works fine without memory leak.
When moving the sleep inside the inner for loop then it works reliably but this isn't practical because the rendering of a page in my real application is done in a synchronous process and the implementation of this rendering is out of my scope. I can only sleep AFTER rendering a single page.
Environment:
Node.js 24.13.0
Canvas 3.2.1 and 2.11.2
Debian Linux 13 with 64 GB RAM
kayahr commentedon Feb 4, 2026
I think my problem with embedding images in PDF is a separate problem to the memory leak described here. So I extracted it with additional data into a new issue #2550
Bump `node-addon-api` to fix memory leak Automattic#2436
Bump `node-addon-api` to fix memory leak Automattic#2436
iurisilvio commentedon Apr 6, 2026
I'm being hugely affected by this issue, long running firebase function never release memory after I upgraded to canvas 3. 😢
I have some investigation and a potential fix in #2562! 🎉
Revert node-addon-api 8 + NAPI_EXPERIMENTAL; keep cairo leak fixes (m…
node-addon-api 8.9.0
node-addon-api 8.9.0
[-]Memory leak in 3.0.0-rc2[/-][+]Memory leak-like behavior caused by node-addon-api not doing GC synchronously[/+]