New guy for Garbage Collection

Asutosh PandaAsutosh Panda
3 min read

How would you feel when your efficient Go code uses less memory than allocated but still managed to crash. Well this was the issue in the previous version, with the latest version(1.19). Go has introduced a new feature called GOMEMLIMIT. GOMEMLIMIT is kind of solution that can remove garbage collection and related out of memory issues. Here is a nugget about that :-

Go's Garbage Collection

The garbage collector of go is quite concurrent and efficient. Go's syntax is straightforward which also keeps the memory leak problems away. In fact the garbage collection process barely takes 1% of CPU. On the other hand, the memory allocation in stack is quite cheap, doesn't require garbage collection for function or memory, have a small life span too. In case of memory allocation in heap is long-lived, costly, garbage is must required. e.g. - allocating few variables for a calculator code is handled by stack and creating a cache needs heap.

Why Out Of Memory Error

The questions is why you can face out of memory issues even when you have allocated memory. Sometimes the short-lived elements also get allocated inside the heap but for those garbage collector doesn't work. So those allocations never get free. In the end, these allocations will lead to Out of Memory error.

So to avoid this, we can delay the cycle of garbage collection(giving very little scope to short-lived elements to stay). We can go as close as to the limit, then only run the cycle. In this way we can make the code fast and stay safe from OOM.

Before GOMEMLIMIT

Prior to Go 1.19, there used be GOGC environment variable which accepts a target limit relative to the current heap size we have. Per say, GOGC's default value is mostly 100mb. To run the cycle of garbage collection, the heap size must double itself. For small permanent heaps, this shouldn't be problem. e.g. - if you have 6gb, then you can double the heap size many times, starting from 100mb. But what happens in case of long-lived heap which may need 5gb in first case, you won't be able to double it up to 6gb. Earlier, GOGC used to be set very low(such as 25) to stay safe.

After GOMEMLIMIT

It allows to keep a soft memory limit, kind of solution that works along with the GOGC. Soft limit in the sense, Go runtime doesn't ensure that the memory usage will cross the limit. GOGC can be set when memory is still available. Whereas GOMEMLIMIT will automatically make the garbage collector more aggressive when we'll be near to the limit and this what we needed overall. e.g. - heap limit is 100mb, it can double up to 200mb and when go near to 4gb the garbage collector will run.

That's it for now. Thanks😊

0
Subscribe to my newsletter

Read articles from Asutosh Panda directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Asutosh Panda
Asutosh Panda

Curious about Outages, Distributed Systems, DevOps, Backend, SRE related stuffs.