Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reduce memory to minimum with global.gc() in nodejs?

I found the related problem here

But still don't got the real answer for this. :( So, How to reduce to minimum memory with global.gc() in nodejs? should I spam global.gc() function to reduce?

like image 274
Keitaro Urashima Avatar asked Oct 27 '25 06:10

Keitaro Urashima


1 Answers

Instead of forcing the garbage collector to run, you should first identify if you actually have a memory leak by using various tools (e.g. node's built-in inspector, heapdump module, etc.) available for node that allow you to detect such leaks.

It's entirely possible that it appears there is a leak when there isn't, due to how V8's garbage collector works (it is generally lazy because GC is not exactly a cheap operation CPU usage-wise).

Also, you can limit the amount of memory used by V8 via the --max-old-space-size=xxxx command-line argument (where xxxx is the amount of memory in megabytes). This can also be helpful in more quickly determining whether you have a legitimate memory leak.

like image 58
mscdex Avatar answered Oct 28 '25 19:10

mscdex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!