liveBook · Manning

Garbage collection

As explained in Go in Practice, Second Edition

A garbage collection system automatically identifies and reclaims memory that a program can no longer access.

Garbage collection automatically reclaims memory a program can no longer access. Go includes it in the runtime, reducing the need for manual memory management in applications. section 1.3.1 section 1.1

Go’s garbage collection shapes practical language and platform choices:

  • It trades some control over memory and runtime interruptions for fewer memory leaks and buffer-overflow risks associated with manual management. section 1.3.1
  • It generally has little noticeable effect on runtime performance, while still allowing targeted optimization when necessary. section 1.3.3
  • It makes Go a practical fit for services and system-level processes, but limits its suitability for resource-constrained embedded systems. section 1.3

Disabling collection for an experiment

Set the environment variable GOGC to "off" to disable garbage collection. This is an exceptional tuning choice: use caution because most applications do not need it. section 1.3.3

Figure 1.5 Java and Go running in an operating system
figure

Compare how Java and Go execute with runtime support inside an operating-system environment. section 1.3.3

How Go in Practice, Second Edition covers it

How deep it goes
Defines the runtime role and practical trade-offs, briefly mentions tuning, and stops short of explaining collector internals.
Examples
Compares Go with C, Java, Rust, and Zig, and demonstrates the GOGC environment variable as a tuning control.
What it assumes
Assumes basic familiarity with programming languages and runtime execution while introducing Go’s automatic memory-management model.
What you can do after
Operate/configure Go programs with realistic expectations about collection overhead and the risks of disabling it.
How it connects
Connects garbage collection with Go runtime, memory management, runtime performance, embedded systems, and manual allocation in C.

Also explained in

Compare how 9 books explain garbage collection →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Practice, Second Edition. The section links point to the parts of the book it draws on.