Hacker News new | past | comments | ask | show | jobs | submit login

Having never dealt with issues relating to garbage collection before, how do you go about diagnosing GC issues in a language where that’s all handled for you?



In the java realm you have very fine-grained GC logging that provides insight into the overall behavior of the GC and its different subcomponents. Then there are recording/debugging facilities that allow you to trace allocations, how long objects live, analyze the entire heap (including unreachable but not yet collected objects). And higher-level monitoring APIs separate from the logging and debugging stuff. You can also choose between collectors with different characteristics, trading between overall heap size, latency, utilization of CPU cores and other factors.


In the case of NodeJS, which use the V8 Engine [0], you have access to the diagonistics API [1] that allows you profile your cpu or memory consumption. There are some tools that make that easier (see [2] or [3]) but you often left to interpret the result yourself

[0]: https://v8.dev/

[1]: http://nodejs.org/dist/latest-v12.x/docs/api/inspector.html#...

[2]: https://github.com/davidmarkclements/0x

[3]: https://github.com/vmarchaud/openprofiling-node

PS: Disclamer i wrote one of tool i mentionned above [3]


There are some general tricks that are language-agnostic, like allocating a huge "buffer" object when the app starts, the size of which is some significant portion of the memory you allow the process to use, which always has a reference, then storing references to other objects you need in that big object. In other words, circumvent the garbage collector.

Of course, this has its own issues, but I've seen it done in e.g. Go before. Its likely you'll inevitably end up with leaks, but if your service is fungible and can tolerate restarts, basically what you're doing is moving the "GC Pause" to be a "Container Restart" pause, which may be slower, but would happen less often. Some languages have ways to manually call the GC (Node is not one of them, afaik).


Node allows you to call global.gc() if you enable that functionality with a separate argument. In many cases this is an anti-pattern that would make your application behavior worse rather than better, that's why you have to opt into that.


> There are some general tricks that are language-agnostic, like allocating a huge "buffer" object when the app starts

I've only seen this done in Go :)


It's a fairly common thread in game development circles. Usually game development is one of the few places with big enough constraints that it's worth doing your own memory management in languages that have garbage collectors. Other places it often makes sense to just architect around GC pauses, since you're going to want redundancy and load balancing anyways.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: