Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js v8 garbage collection doesn't seem to trigger

I am confused as my application is leaking memory. It is a tcp server that process hundreds of thousands of packets per minute. I have checked the code, improved it and profiled the memory.

Everything seems okay, testing locally with low traffic actually shows that the gc releases the memory properly. But when on the live heavy traffic server it doesn't.

So I tried using the expose-gc option and added the forced gc to every disconnection and now I found that the memory is not leaking anymore or was it every even leaking?

So, my conclusion is the gc didn't activate. My server has 3GB of memory and the application in just a few hours gets to eat 2.8GB of that.

Now with the forced gc the application is not leaking anymore. It is maintaining around 200MB of memory.

So, my question, why wasn't the gc getting triggered?

like image 879
majidarif Avatar asked Feb 04 '15 12:02

majidarif


People also ask

How do I call a garbage collector in node JS?

If you launch the node process with the --expose-gc flag, you can then call global. gc() to force node to run garbage collection. Keep in mind that all other execution within your node app is paused until GC completes, so don't use it too often or it will affect performance.

Does node js do garbage collection?

Luckily for you, Node. js comes with a garbage collector, and you don't need to manually manage memory allocation.

How often does node garbage collect?

Depends on your code and how much garbage is created, but garbage collection calls each couple of seconds is pretty normal as default JS code usually creates objects constantly. If you have a problem with GC, try to reduce object allocations and just modify their properties, when needed.

What triggers garbage collection in C #?

Garbage Collection occurs if at least one of multiple conditions is satisfied. These conditions are given as follows: If the system has low physical memory, then garbage collection is necessary. If the memory allocated to various objects in the heap memory exceeds a pre-set threshold, then garbage collection occurs.


1 Answers

Source: StrongLoop Blog

The fundamental problem garbage collection solves is to identify dead memory regions (unreachable objects/garbage) which are not reachable through some chain of pointers from an object which is live. Once identified, these regions can be re-used for new allocations or released back to the operating system

The leaking of memory when using event listeners is quite common and is caused by a situation where the object that is listening is prevented from being garbage collected because the event emitter, also an object, is keeping a reference to it.

So in your code onSuccess method will be referenced by your request object. However, that onSuccess is only one function that is being reused as a listener for all request objects so that should not lead to memory buildup.

In order to find the real cause of objects being left alive in your code, I'd check around the connection ending and make sure no pointers are left alive. In addition, not in some cases V8 will create an instance of a function for every time it is used, and this may be the case in hand. If both are combined, your allocated memory will keep piling instances of the callback.

Objects which have survived two minor garbage collections are promoted to “old-space.” Old-space is garbage collected in full GC (major garbage collection cycle), which is much less frequent. I assume this is far fetched, but the option of you allocating faster than sweeping cycles might be considered, and thus when you trigger garbage collection manually, a full GC cycle is triggered.

To ensure fast object allocation, short garbage collection pauses, and the “no memory fragmentation V8” employs a stop-the-world, generational, accurate, garbage collector. V8 essentially stops program execution when performing a full garbage collection cycle.

This can explain why the memory does not leak when V8 is forced into garbage cleaning.

like image 182
Selfish Avatar answered Sep 28 '22 18:09

Selfish