DEV Community

Cover image for Profiling Memory In Go
dikac
dikac

Posted on

Profiling Memory In Go

#go

Efficient memory management is critical in Golang applications, particularly in high-concurrency environments, long-running services, or data-intensive tasks. Profiling memory usage helps diagnose issues, optimize performance, and prevent out-of-memory (OOM) errors. This guide provides a comprehensive approach to profiling memory usage from a Go endpoint.

Why Memory Profiling Matters

Memory profiling identifies inefficient memory usage, memory leaks, and excessive allocations in your application. Without proper profiling, memory issues can lead to performance degradation, higher costs, and service downtime.

Common Causes of High Memory Usage

  1. Memory leaks: Unintended memory retention due to data structures not being cleared up.
  2. Excessive allocations: Large slices, maps, or other data structures consuming significant memory.

Setting Up Memory Profiling in Go

To profile memory usage in a Go application, you can use tools like pprof for runtime profiling and parca for continuous profiling. Here’s how to set up and use these tools effectively.

Profiling Tools

  1. pprof

    A built-in Go tool that provides profiling for memory, CPU, goroutines, and more.

  2. Parca

    A continuous profiling tool that provides real-time insights by collecting data from pprof.

  3. Stress Testing

    Generate load to simulate real-world usage and observe memory behavior under stress. For our case we use SoapUI.

Using pprof

Since pprof is built-in tool, installation is not required, include the following snippet to enable pprof in your application:

import (
    _ "net/http/pprof"
)

func main() {
    go func () {
    log.Print(http.ListenAndServe(":1234", nil))
    }()
}
Enter fullscreen mode Exit fullscreen mode

This exposes pprof on port 1234. Access profiling data by visiting http://localhost:1234/debug/pprof/ or using tools like go tool pprof.

Using parca for Continuous Profiling

To install parca see https://github.com/parca-dev/parca, after successfully installing parca, configure parca.yaml job_name.static_configs.targets set the same port number as pprof (in this example 1234)

then you can run command

parca --config-path="parca.yaml"
Enter fullscreen mode Exit fullscreen mode

if successful you will see message similar to

level=info name=parca ts=2024-10-30T06:19:44.5149184Z caller=factory.go:53 msg="loading bucket configuration"
level=info name=parca ts=2024-10-30T06:19:44.5159183Z caller=badger.go:54 msg="Set nextTxnTs to 0"
level=info name=parca ts=2024-10-30T06:19:44.517917Z caller=server.go:90 msg="starting server" addr=:7070
Enter fullscreen mode Exit fullscreen mode

addr=:7070 is where you can access parca web interface, port number might be different depend on configuration

if all setup successful, you can access parca on web browser

Image description

There is a multiple profiling type, for Memory usage you can use

Image description

if you encounter any issue, you should consult documentation since different environment might require different solution


Identifying Memory Usage

Stress Testing

Before profiling, simulate high traffic using stress-testing tools in our case we use SoapUI. Stress tests help replicate conditions leading to memory issues.

Analyzing Memory Usage

Image description
After completing a stress test, monitor memory usage over time using the parca dashboard.

Image description
Click on the graphs to access detailed profiles.

Image description
Using the icicle graph, examine the stack and corresponding memory usage. Wider lines indicate higher memory consumption. This visualization helps pinpoint processes consuming significant memory.

In our application, a process with substantial memory usage was identified:

Image description

Memory Optimization

Memory optimization is a complex topic that varies depending on the application and its environment. Here are some practical techniques:

  • Selective Data Loading: Load only the necessary data to significantly reduce memory allocation.
  • Avoiding Pointers: Use value types instead of pointers to minimize heap allocations.
  • Predefining Data Lengths: Specify lengths for known-size data structures to improve memory efficiency.

Upon further investigation, we discovered that the data retrieved from the cache was excessively large. We needed to validate whether such a large dataset was truly necessary for our logic flow.

In our case, it turned out that this large dataset was not required. Therefore, we optimized the process by selectively removing unnecessary data. After re-running the tests, memory usage was reduced by approximately 50%.

Previous Implementation

Image description

After selectively removing unneeded data

Image description

With help of this method we can easily narrow down and correct memory usage, in our case Selective Data Loading is the correct method for reducing memory usage.


Conclusion

Memory profiling is a critical practice for maintaining the performance and stability of Go applications. By leveraging tools like pprof and parca, you can identify memory issues, optimize resource usage, and ensure your application performs reliably under various loads. Regular profiling and proactive optimization help address memory-related challenges effectively.

Top comments (0)