$linuxjunkies
>

garbage collection

also: GC, automatic memory management

Garbage collection is an automatic memory management process that identifies and frees memory that is no longer being used by a program, preventing memory leaks.

Garbage collection automatically reclaims memory occupied by objects that a program no longer references. Instead of requiring programmers to manually deallocate memory, the garbage collector periodically scans the heap, identifies unreachable objects, and frees their memory for reuse.

In languages like Java and Python running on Linux, the runtime environment handles garbage collection. For example, when a Python program creates objects that are no longer referenced, the garbage collector eventually removes them and returns their memory to the system.

C and C++ programs on Linux typically do not use garbage collection by default, requiring developers to manually manage memory with malloc()/free() or new/delete. However, some system libraries and custom implementations may use reference counting or garbage collection algorithms.

While garbage collection simplifies development and prevents certain bugs, it can introduce unpredictable pause times when collection cycles run, which is why performance-critical applications sometimes avoid it.

Related terms