This is a simple go/Gin project that explores the practical use of GOMEMLIMIT
and visualizes the changes to heap memory in realtime.
Table of Contents
- Go >= v1.19
- Docker
- WSL / cygwin / git bash (to run shell scripts)
The estimated memory required to run the http server is around 5-10 MiB
, hence the ideal initial memory limit should be set to be more than 20 MiB
to prevent GC from constantly running, leaving no space for new heap allocation.
Default configuration
GOMEMLIMIT=30MiB # soft memory limit for the runtime in human readable bytes
GOGC=100 # initial garbage collection target percentage, 100 => 100% => 2x
GODEBUG=gctrace=1 # enables debug mode with garbage collection tracing
Run the docker container.
# builds image and run container
$ ./scripts/run.sh
Run tests.
$ ./scripts/test.sh
By default the gin app will listen on port 8080 on localhost. The base_url
mentioned from here should be http://localhost:8080 unless explicitly modified.
Refer to API Docs for more detailed documentation.
Heap: Subset of memory that’s managed by GC.
- Memory requested by the application that the GO Compiler couldn’t find a place for in compile time.
- Non-heap memory includes memory for GO Routine stacks, GC metadata, and other various GO Runtime data structures.
Live: Memory reachable by program.
- Memory that the GC discovers is actively used by the program.
- GC is basically a bunch of code that needs to be executed to make this discovery.
New: New Memory that may or may not be live.
- Memory that the application has asked runtime to allocate for it since the last time the GC ran. Hence, the liveness of it has not yet been determined.
Total Heap Memory:
Total heap memory = Live heap + New heap memory
Total GC CPU Cost: derived from GC Cost Model
Total GC CPU cost = (GC CPU cost per cycle) * (GC frequency) * Time Period, T