Skip to content

A simple Gin application to show the Golang Garbage Collection in action.

Notifications You must be signed in to change notification settings

davidlhw/golang-garbage-collection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GO Garbage Collection 1.19 Demo

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
  1. Setup
  2. Usage
  3. Technical Essay
  4. References

Setup

Prerequisites

General

  • Go >= v1.19
  • Docker

Windows

  • WSL / cygwin / git bash (to run shell scripts)

Configuring

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

(back to top)

Usage

Run the docker container.

# builds image and run container
$ ./scripts/run.sh

Run tests.

$ ./scripts/test.sh

API Endpoints

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.

(back to top)

Glossary

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

(back to top)

Technical Essay

(back to top)

References

(back to top)

About

A simple Gin application to show the Golang Garbage Collection in action.

Topics

Resources

Stars

Watchers

Forks