Standard library / runtime

runtime.MemProfile()

Copy sampled allocation records; data can lag garbage collection. Prefer runtime/pprof for analysis. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func MemProfile(p []MemProfileRecord, inuseZero bool) (n int, ok bool)

Parameters & Attributes

NameTypeDescription
p[]MemProfileRecordDestination slice of profile records; if too small, allocate using the returned count and retry.
inuseZeroboolInclude previously allocated sites whose allocated and freed byte counts are equal.

Return Value

(n int, ok bool)

Number of required records and whether the supplied slice was large enough to copy them.

Working Example

Example Snippetgo
package main
import (  "fmt"  "runtime")
func main() {  n, _ := runtime.MemProfile(nil, true)  records := make([]runtime.MemProfileRecord, n+64)  n, ok := runtime.MemProfile(records, true)  fmt.Println(ok && n >= 0 && n <= len(records))}
Go Reference