I notice the same problem that garbage collection does not occur. Re-testing my server for the same content (Rendered by react) to the test memory of the script will be exhausted in an hour or so. This AWS instance has only 1 GB of RAM.
After forced garbage collection, whenever heapUsed exceeded 256 MB, everything works fine for several hours in a row. heapUsed ranges from approximately 150 MB after the GC to just over 256 MB.
Meanwhile, heapTotal will eventually stabilize at approximately 340 MB.
I came to the conclusion that there is no memory leak on my server, garbage collection does not happen as expected.
That's what I'm doing:
setInterval (function() { let mu = process.memoryUsage(); console.log('heapTotal:', mu.heapTotal, 'heapUsed:', mu.heapUsed); if (mu.heapUsed > 256 * 1024 * 1024) { console.log('Taking out the garbage'); global.gc(); } }, 1000 * 60);
It might be better to check the memory after each request.
source share