Skip to content

Commit

Permalink
fix(go): memory profiling
Browse files Browse the repository at this point in the history
  • Loading branch information
rwxd committed Aug 28, 2023
1 parent 63f862a commit b945824
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions Programming-Languages/Go/profiling.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Profiling in Go

## CPU Profiling

### Generate data

```go
import (
"runtime/pprof"
)

func main {
f, err := os.Create("my-tool.prof")
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()

// CPU Intensive code
}
```

### View data

```bash
go tool pprof my-tool.prof
```

```bash
# view top 10 functions
(pprof) top

# view top 20 functions
(pprof) top20

# view top 10 functions in a graph
(pprof) top --cum

# Visualize graph through web browser
(pprof) web

# Output graph as a svg
(pprof) svg
```

## Memory Profiling

Go comes with a built-in profiling tool called pprof that can provide detailed information about your application's runtime memory usage.

### Generate data

```go
import _ "net/http/pprof"
```

Then, add the following code to start a new HTTP server that will serve the pprof endpoints:

```go
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
```

With the above setup, you can access various profiling data by navigating to http://localhost:6060/debug/pprof/ while your application is running. For memory-related insights, http://localhost:6060/debug/pprof/heap is of particular interest.

### Capture Heap Dump

Once you have pprof set up and your application is running:

Allow your application to run until you suspect a memory leak.
Capture a heap profile by executing:

```bash
curl -s http://localhost:6060/debug/pprof/heap -o mem.pprof
```

### Analyze data

```bash
go tool pprof mem.pprof
```

* Use the top command to get an overview of the functions consuming the most memory.
* To see detailed memory allocations for a specific function, use the list command followed by the function name.
* For a visual representation, web or svg commands can be used to generate graphs showcasing memory allocations.

0 comments on commit b945824

Please sign in to comment.