https://github.com/jamalkaksouri/memory_leak_golang
Memory leak in Go by example
https://github.com/jamalkaksouri/memory_leak_golang
go golang leak memory-leak minikube pprof
Last synced: 7 months ago
JSON representation
Memory leak in Go by example
- Host: GitHub
- URL: https://github.com/jamalkaksouri/memory_leak_golang
- Owner: jamalkaksouri
- Created: 2022-10-15T13:13:51.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-10-16T09:09:19.000Z (almost 3 years ago)
- Last Synced: 2025-02-08T18:45:49.960Z (8 months ago)
- Topics: go, golang, leak, memory-leak, minikube, pprof
- Language: Go
- Homepage:
- Size: 934 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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](https://gitlab.com/graphviz/graphviz/-/releases)
2. Download and run the [32-bit](https://gitlab.com/api/v4/projects/4207231/packages/generic/graphviz-releases/6.0.2/windows_10_cmake_Release_graphviz-install-6.0.2-win32.exe) or [64-bit](https://gitlab.com/api/v4/projects/4207231/packages/generic/graphviz-releases/6.0.2/windows_10_cmake_Release_graphviz-install-6.0.2-win64.exe) 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
```bash
go install github.com/google/pprof@latest
```
2. Dump heap to a file(ensure that application is run!-using new terminal)
```bash
curl http://:/debug/pprof/heap > heap.out
```
3. Use pprof to interact with heap
```bash
go tool pprof heap.out
```
4. Inside the new command prompt
```bash
png
```π‘ Everytime for reporting new result you should repeat levels 2,3,4\
π‘ After run `go run main.go` go to [localhost](http://localhost:8080/append-slice) on browser then repeat previous level# routes
π **[http://localhost:8080/append-slice](http://localhost:8080/append-slice)**\
π **[http://localhost:8080/hanging](http://localhost:8080/hanging)**\
π **[http://localhost:8080/streams](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.