Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

timing for node.js Garbage Collection

Recently, I have installed https://github.com/lloyd/node-memwatch for development, to investigate how GC interacts with my program.

I have binded the event "stat", the arthor states that the event is triggered when GC is performed.

I've found that when the script with high load. The "stat" events are not triggered. I am not sure whether it implies GC is not performed, but it is a sign that GC may not have triggered.

In my production server, the loading is even a lot higher throughout the day. I am quite sure that GC has no chance to perform. The memory usage has no chance to decrease. It is just like memory leak.

  1. Is my observation correct? Is GC not able to perform when there are high load continuously?
  2. If so, should I use the exposed GC interface to force GC?
  3. Is GC blocking? Should I perform GC more frequently so that GC will not block for a long time for each GC?

I know manual GC is not a good idea (There is someone opposing the idea of manual GC in node.js, but I cannot find the link for reference), but I am seeing that the memory usage is increasing continuously. It really needs to be solved.

like image 741
Harold Chan Avatar asked Jul 12 '12 08:07

Harold Chan


1 Answers

There are 3 types of GC events in V8

  • kGCTypeMarkSweepCompact
  • kGCTypeScavenge
  • kGCTypeAll

V8 runs the scavenge event quite often, but only on newly created objects. During a heavy load the other types of GC may occur infrequently.

You can try running the NodeFly agent which uses nodefly-gcinfo module to track ongoing memory usage.

You can also call nodefly-gcinfo directly which has a callback that runs each time a GC event occurs:

require('nodefly-gcinfo').onGC(function(usage, type, flags){
    console.log("GC Event Occurred");
    console.log("Heap After GC:",usage, type, flags);
});
like image 96
Jacob Groundwater Avatar answered Oct 11 '22 15:10

Jacob Groundwater