Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.JS garbage collection event? or --trace-gc to stderr?

As a general practice, when I write scripts, log messages always go to stderr and data (status messages, results from an algorithm, whatever) goes to stdout.

I would like to get this sent to stderr. Can that be done?

I would be fine to use my own handler to some sort of process.on('gc') event if there is one. Is there one?

I am writing some code now where stdout is reserved for data and I have no choice but to turn off --trace-gc. I am using memoryUsage() as a second best thing, but it does not tell the usage right before and right after GC, it just tells you the usage whenever memoryUsage() happened to be called.

like image 928
700 Software Avatar asked May 05 '11 20:05

700 Software


People also ask

Does Nodejs do garbage collection?

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

How does node garbage collection work?

Garbage collection (GC) can have a big impact on the performance of your apps. GC is a process that the Node. js runtime regularly runs to clean up any objects that were created and are not used anymore. If you create a lot of objects in your code (or a dependency does) this can slow down your app.

What is scavenge GC?

Scavenge operations occur only with the gencon garbage collection policy. A scavenge operation runs when the allocate space within the nursery area is filled. During a scavenge, reachable objects are copied either into the survivor space within the nursery, or into the tenure space if they have reached the tenure age.

How often does node GC run?

This small GC is triggered by a number of things (including some heuristics) that can make it difficult to determine precisely what triggered it, but every couple seconds is nothing to be concerned about in practice since it's only blocking for 1-10ms. Under high load applications it will actually run much more often.


1 Answers

A quick check of the v8 source code (where the garbage collector lives - [node src dir]/deps/v8/src/heap.cc and platform-posix.cc) shows that garbage collection messages are printed to the console using the standard linux vprint() (from printf(3)) so I'm pretty sure you cannot trap them without patching the source code.

like image 182
Rob Raisch Avatar answered Sep 17 '22 02:09

Rob Raisch