memsize

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 30, 2023 License: MIT Imports: 8 Imported by: 2

README

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)

Documentation

Overview

Package memsize computes the size of your object graph.

So you made a spiffy algorithm and it works really well, but geez it's using way too much memory. Where did it all go? memsize to the rescue!

To get started, find a value that references all your objects and scan it. This traverses the graph, counting sizes per type.

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

memsize can handle cycles just fine and tracks both private and public struct fields. Unfortunately function closures cannot be inspected in any way.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HumanSize

func HumanSize(bytes uintptr) string

HumanSize formats the given number of bytes as a readable string.

Types

type Sizes

type Sizes struct {
	Total  uintptr
	ByType map[reflect.Type]*TypeSize
	// Internal stats (for debugging)
	BitmapSize        uintptr
	BitmapUtilization float32
}

Sizes is the result of a scan.

func Scan

func Scan(v interface{}) Sizes

Scan traverses all objects reachable from v and counts how much memory is used per type. The value must be a non-nil pointer to any value.

func (Sizes) Report

func (s Sizes) Report() string

Report returns a human-readable report.

type TypeSize

type TypeSize struct {
	Total uintptr
	Count uintptr
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL