/plushcap/analysis/cloudflare/recycling-memory-buffers-in-go

Recycling memory buffers in Go

What's this blog post about?

The blog post discusses the challenges of memory management in long-running network services written in garbage-collected languages like Go. It presents a simple program that creates a lot of garbage and uses the runtime.ReadMemStats function to get information about the size of the heap. The author explains how garbage collection runs frequently in a Go program, causing HeapAlloc and HeapIdle to change over time. A separate scavenger releases memory unused for more than 5 minutes, but this doesn't happen often. The post then introduces a manual memory management technique using channels to keep a separate pool of buffers that are no longer used. This recycling mechanism can be shared across goroutines and dynamically cope with bursts of demand for buffers. The author also presents an example of a recycler in Go, which keeps a linked list of returned buffers and periodically throws away old ones unlikely to be reused. These techniques can significantly reduce the amount of memory a program needs by recycling memory that the programmer knows is likely to be reused without asking the garbage collector to do the work. They can be used for more than just byte slices, as any arbitrary Go type (user-defined or not) could be recycled in a similar manner.

Company
Cloudflare

Date published
Aug. 24, 2013

Author(s)
John Graham-Cumming

Word count
1488

Hacker News points
None found.

Language
English


By Matt Makai. 2021-2024.