On this page
Memory Management Models
Prev: Pointers, References & Indirection | Up: memory_and_execution | Next: paradigms
Rel: Python Memory Management, Call Stack & Heap, C Dynamic Memory & Allocators, Rust Ownership, Borrowing & Lifetimes, JVM Garbage Collection (G1 & ZGC), C++ RAII & Smart Pointers
Ref: Wikipedia — Garbage Collection (Computer Science)
Tags: #public #prog #programming #memory #gc #ownership
Contents
Comparing the Four Major Models
| Model | Examples | How Memory Is Reclaimed | Key Trade-off |
|---|---|---|---|
| Manual | C, Zig | Programmer calls malloc/free (or arena allocators) |
Zero runtime overhead, full control; risk of leaks & use-after-free |
| Reference Counting | CPython, Swift | Each object counts incoming references; freed immediately at 0 | Deterministic cleanup; cannot free cycles alone + atomic count cost |
| Tracing GC | JVM, Go, V8 (JS) | Periodically traces reachable graph from roots (mark-sweep / generational) | Handles cycles & simplifies code; higher RAM + stop-the-world pauses |
| Ownership / RAII | Rust, modern C++ | Single owner per value; compiler inserts drop/destructor at scope exit |
Memory safety + zero GC pauses; stricter compile-time borrow rules |
Cycles, Pauses & Deterministic Cleanup
- Reference cycles (
A -> B -> A) keep refcounts > 0 forever; break them with weak references or a backup cyclic collector (CPythongc). - Deterministic resource cleanup (files, sockets, locks) should never rely on tracing GC timing — use RAII (
Drop),withcontext managers,try-with-resources, ordefer.
Linked from
- tuts/c
- tuts/c/pointers_and_memory
- tuts/c/pointers_and_memory/dynamic_memory_and_allocators
- tuts/c/tutorials/arena_allocator_pattern
- tuts/cpp
- tuts/cpp/language_and_raii/raii_smart_pointers_and_lifetime
- tuts/cpp/stdlib/memory/weak_ptr
- tuts/java/jvm_and_openjdk
- tuts/java/jvm_and_openjdk/garbage_collection_g1_and_zgc
- tuts/programming/memory_and_execution
- tuts/programming/memory_and_execution/call_stack_and_heap
- tuts/programming/memory_and_execution/memory_management_models
- tuts/programming/memory_and_execution/pointers_references_and_indirection
- tuts/rust/ownership_and_internals
- tuts/rust/ownership_and_internals/ownership_borrowing_and_lifetimes
Related notes
- tuts/programming/memory_and_execution Linked here; links to this page; shared tags: #memory, #prog, #programming
- tuts/programming/memory_and_execution/call_stack_and_heap Linked here; links to this page; shared tags: #memory, #prog, #programming
- tuts/programming/memory_and_execution/pointers_references_and_indirection Linked here; links to this page; shared tags: #memory, #prog, #programming
- tuts/rust/ownership_and_internals/ownership_borrowing_and_lifetimes Linked here; links to this page; shared tags: #memory, #ownership, #prog
- tuts/c/pointers_and_memory/dynamic_memory_and_allocators Linked here; links to this page; shared tags: #memory, #prog
- tuts/cpp/language_and_raii/raii_smart_pointers_and_lifetime Linked here; links to this page; shared tags: #memory, #prog
- tuts/java/jvm_and_openjdk/garbage_collection_g1_and_zgc Linked here; links to this page; shared tags: #gc
- tuts/c/pointers_and_memory links to this page; shared tags: #memory, #prog
- tuts/c/tutorials/arena_allocator_pattern links to this page; shared tags: #memory, #prog
- tuts/cpp/stdlib/memory/weak_ptr links to this page; shared tags: #memory, #prog