Go manages memory for you: the runtime allocates objects and its garbage collector frees them once nothing references them. Go does not manage other resources such as files, sockets, database connections, locks and goroutines for you. You release those yourself, usually with defer, and you bound them with context.
The distinction matters because the garbage collector only knows about reachable memory. A file handle that is no longer referenced is not guaranteed to be closed at any useful moment, and a leaked goroutine keeps its memory reachable forever.
Every value you create with a literal, make, new or append is allocated by the runtime. The compiler's escape analysis decides whether a value lives on the stack or the heap, and the concurrent, tracing garbage collector reclaims unreachable heap objects.
There is no free. You still influence memory use through design choices:
make([]T, 0, n).sync.Pool on hot paths.Two settings control GC behaviour. GOGC (default 100) sets how much the heap may grow before the next cycle. GOMEMLIMIT, available since Go 1.19, sets a soft total memory limit, which is useful in containers.
The same values can be set from code with debug.SetGCPercent and debug.SetMemoryLimit. Measure with pprof before changing them.
ReadMemStats briefly stops the world, so use it for diagnostics, not in a hot loop. For production monitoring prefer the runtime/metrics package or net/http/pprof.
A resource is anything the operating system or a remote service holds on your behalf: file descriptors, network connections, database rows and transactions, mutexes, timers, child processes and goroutines. Go gives you tools, but the responsibility to release is yours.
defer runs a call when the surrounding function returns, including on early returns and panics.
Place defer immediately after the successful acquire, never before the error check. When closing can fail in a way that matters, such as after writing a file, check the error from Close instead of discarding it.
Deferred calls run at function exit, not at the end of each iteration. Opening thousands of files in a loop with defer keeps them all open. Move the body into a helper function so each iteration releases its resource.
A context.Context carries deadlines and cancellation so that slow operations and goroutines stop.
Goroutines have no handle to kill from outside. Give each one a way to stop, such as a context or a closed channel, or it leaks together with everything it references.
runtime.SetFinalizer and, since Go 1.24, runtime.AddCleanup run code after the garbage collector finds an object unreachable. The standard library uses them as a safety net, for example os.File closes its descriptor this way.
Do not rely on them for correctness. They run at an unspecified time, possibly never before the program exits, so a descriptor or lock held until then can exhaust limits. Prefer explicit Close and defer. AddCleanup is the newer, safer API and is preferred over SetFinalizer in new code.
| Aspect | Memory | Other resources |
|---|---|---|
| Allocated by | Runtime, implicitly | You, by calling Open, Dial, Lock and similar |
| Released by | Garbage collector | You, with Close, Unlock, cancel |
| Typical tool | GOGC, GOMEMLIMIT, pprof | defer, context, pools with limits |
| Leak symptom | Growing heap | Too many open files, exhausted connections |
| Safe to forget? | Yes, if unreachable | No |
No. You cannot free memory directly. You can only make objects unreachable and let the garbage collector reclaim them, or reduce allocations by reuse.
Not reliably. Some types attach a finalizer as a last resort, but timing is not guaranteed. Always close files and connections explicitly, normally with defer.
Capture a heap profile with net/http/pprof or runtime/pprof and compare profiles over time using go tool pprof. Most leaks in Go are retained references or goroutines that never exit.
database/sql pools connections for you, but you still must close Rows and transactions, otherwise connections are never returned to the pool.
Go automates memory through allocation by the runtime and collection by the garbage collector, and you tune it with GOGC, GOMEMLIMIT and profiling. Everything else is a resource you must release yourself: close it with defer, bound it with context, and give every goroutine an exit. Treat finalizers and cleanups as a safety net, not a plan.