memsize

package
v1.7.2 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2025 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Overview

Package memsize provides utilities to estimate the memory footprint of Go values without relying on the reflect or unsafe packages for direct memory inspection.

It leverages the gob encoding mechanism to serialize values into memory buffers and calculate their approximate in-memory size in bytes.

This approach prioritizes safety and portability over raw performance, making it suitable for debugging, profiling, or logging approximate memory usage of application data structures.

Features:

  • Estimate the size of any Go value in bytes using gob encoding.
  • Human-readable byte formatting with SI units (e.g., "1.2 kB").
  • Graceful handling of nil pointers, maps, slices, arrays, and channels.

Example:

size, err := memsize.SizeOf(myStruct)
if err != nil {
    log.Fatal(err)
}
fmt.Println("Memory size:", memsize.ToByteString(size))

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InterfaceIsNullPointer

func InterfaceIsNullPointer(i interface{}) bool

InterfaceIsNullPointer returns true if the interface is nil or if the interface is a pointer, map, chan, or slice and is nil

func SizeOf

func SizeOf(value interface{}) (int64, error)

SizeOf returns the number of bytes that 'value' takes up in memory, without using reflect or unsafe this functions has minor overhead for the encoding process.

func ToByteString

func ToByteString(b int64) string

ToByteString returns the number of bytes as a string with a unit

Types

This section is empty.

Jump to

Keyboard shortcuts

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