jamalkaksouri / memory_leak_golang

Memory leak in Go by example

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Memory Leak checking in golang service

Sometimes, we find pods restarting several times a day without any error. The memory consumption keeps going up, until it reaches the memory limit.
We have a process to diagnose memory leak for Go services. Tools such as pprof and minikube can help us find the root cause.
In this repo, we used the pprof tool to detect the memory leaks

Dependencies

  • Gin framework
  • Graphviz

Installation Graphviz in windows

  1. Visit the download location
  2. Download and run the 32-bit or 64-bit exe file.
  3. Ignore any security warnings you might get.
  4. During the installation, make sure you select Add Graphviz top the system PATH for current user.
  5. When the installation is finished, start CMD as an administrator
  6. Run command dot

Note Make sure that during installation checked "Add Graphviz to the system PATH for current user"

Usage

  1. One time install
go install github.com/google/pprof@latest
  1. Dump heap to a file(ensure that application is run!-using new terminal)
curl http://<HOSTNAME>:<PORT>/debug/pprof/heap > heap.out
  1. Use pprof to interact with heap
go tool pprof heap.out
  1. Inside the new command prompt
png

💡 Everytime for reporting new result you should repeat levels 2,3,4
💡 After run go run main.go go to localhost on browser then repeat previous level

routes

🔗 http://localhost:8080/append-slice
🔗 http://localhost:8080/hanging
🔗 http://localhost:8080/streams

Tips

In the diagram, each box is a function, and each arrow means a function call. The bigger the box, the higher memory usage. From the graph above, the blame goes to runtime function “allocm” (the largest box near the bottom). Once we found which function is causing the problem, we can check how the memory is leaked.

About

Memory leak in Go by example


Languages

Language:Go 100.0%