Optimize Memory usage of GO Applications by setting Memory Limit
Pallavi Priya Patharlagadda · Journal of Artificial Intelligence Machine Learning and Data Science · 2024
Go is one of the most efficient programming languages, and it mainly contains two components: the Go compiler, which transforms the code into an executable binary, and the runtime, which is piece of software that performs functions like the garbage collector, the scheduler, goroutines, maps, channels, etc.Golang runtime is quite lean on compute resource usage.This is perfect on its own, as it allows the majority of developers to stay concentrated on coding tasks.But when the Golang application is deployed as a containerized service, It needs to share available compute resources with many other containers.Go binaries are not inherently container-aware; specifically, they do not account for memory and CPU limits.In the high-load scenario, When the Golang application needs more memory, the application would request the memory from the Operating System.When the Operating system (Linux) runs out of the available physical memory, the OOM Killer will intervene and kill the process that is taking more memory.To prevent this from happening, Go provided the SetMemoryLimit option in Golang 1.19.This helps in making the application aware of the maximum memory limit.When the application reaches its memory limit, the Go Runtime will trigger the Garbage collector to sweep up the unused memory so the application can start reusing it instead of requesting the Operating system for more memory.This Paper describes the usage and the performance impact of setting this memory limit.