Search code examples
heroku-nodejs

Understanding Heroku's recommendations for Node.js --gc_interval flag


What exactly is --gc_interval, and what are the best practices for setting this value in production if the goal is to distinguish clearly between memory leaks and lazy garbage collection?

Heroku recommends setting --gc_interval=100 in their Node.js best practices documentation. However, I'm having trouble finding any official resources or documentation that explain the precise semantics of this parameter. 100 seems arbitrary.

I have even found this thread which says "--gc-interval is a debugging flag and not supposed to be used for production" but perhaps that is outdated since the thread is from 2016. This gist hosted by https://gist.github.com/listochkin says --gc_interval (garbage collect after allocations) type: int default: -1 but I have no idea who listochkin is.

What exactly is --gc_interval , and what are the best practices for setting this value in production if the goal is to distinguish clearly between memory leaks and lazy garbage collection?

I have tried searching for best practices and official documentation for --gc_interval but have been unable to find any.


Solution

  • It seems to call the GC after that amount of (new) allocations. I think it's left vague enough so people don't use it much and to keep it internal.

    Depending on your problem 100 might be short, or too long if you mostly handle big buffers.

    It's one of the options node passes through to the V8 engine. Calling node --v8-options:

      --gc_interval (garbage collect after <n> allocations)
            type: int  default: -1
    

    I suggest you instead use the --expose-gc flag and control when the GC happens calling global.gc() in your code. There is plenty more documentation for that. You can check memory usage using memoryUsage() before and after your call to check if it's a leak or just lazy GC'ing.

    Besides these, there are more GC related options you can tune, listed when calling node --v8-options. Here's a great article on how V8's garbage collector works that could bring some clarity on how to choose from those options.

    If you really want to find out what it does you might find it in V8's source code, it's called FLAG_gc_interval.