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.
Luckily for you, Node. js comes with a garbage collector, and you don't need to manually manage memory allocation.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With