memsize

memsize computes the size of your object graph

MIT License

Stars
28
Committers
2

NOTE: As of Go 1.23, memsize no longer works because of a restriction added by the Go toolchain. The Go 1.23 compiler no longer allows access to runtime symbols via go:linkname, which prevents memsize from accessing the Stop-the-World functionality of the Go runtime.

If your program depends on memsize, you can disable the restriction when building your program:

go build -ldflags=-checklinkname=0

For Go API documentation, go to https://pkg.go.dev/github.com/fjl/memsize


Package memsize computes the size of your object graph.

For any Go object, it can compute the amount of memory referenced by the object. Almost all Go types are supported, except for function pointers.

To scan a value and print the amount of memory it uses, run

sizes := memsize.Scan(myValue)
fmt.Println(sizes.Total)

If your program provides an HTTP server for debugging (e.g. using net/http/pprof), you can also add an interactive memsize tool there and use it from a web browser. To do this, add

import "github.com/fjl/memsize/memsizeui"

var memsizeH memsizeui.Handler

and then hook the handler up to your debugging HTTP server. The web interface will display buttons for added 'roots', which you must register on the handler:

memsizeH.Add("myObject", &myObject)