In this post we will review the usage of the Go environment variable GOMEMLIMIT, and explain how to use it.
First, let's understand the core behavior of the garbage collection on a Go process runtime. By default, the garbage collection runs whenever the process allocated memory doubles.
See for example, the following memory chart:
This chart displays 2 event of garbage collection, each occurring once the process memory doubles:
- The memory reaches 200M, grabage collection cleaning 20M, and the process memory drops to 180M
- The memory reaches 360M, grabage collection cleaning 60M, and the process memory drops to 300M
- The process memory limit is 3G RAM
- The garbage collection last run had finished with 2G RAM
- The next planned garbage collection run is at 4G RAM
- The process terminates a 3G RAM with out of memory error.
Here is where the GOMEMLIMIT environment variable comes to help. It sets up a soft limit for a process runtime. This means that the garbage collection is run more often when the process memory is above this threshold. Notice that the GOMEMLIMIT does not include some OS related memory, so we would usually set it to a smaller value than the actual memory limit.
How do we actually use this?
We should set the GOMEMLIMIT to a value close the the actual limit of memory by kubernetes. For example, if the kubernetes pod memory limit is 5G, we will set the GOMEMLIMIT to ~4G.
No comments:
Post a Comment