I. What Is Memory Management?
Memory management refers to a set of mechanisms by which a program requests, uses, tracks, reclaims, and releases memory resources during execution.
When a program runs, it needs memory to store:
- Program instructions and code data.
- Local variables and function call information.
- Objects, arrays, and collections.
- Strings and other dynamic data.
- Runtime metadata.
- Caches and temporary computation results.
The core goals of memory management include:
- Correctness: Ensure that the program accesses valid memory regions.
- Safety: Avoid dangling pointers, double frees, and illegal access.
- Efficiency: Reduce the overhead of allocation, deallocation, and reclamation.
- Resource control: Avoid memory leaks and unnecessary memory usage.
- Predictability: Control memory usage and release timing as much as possible.
1. The Basic Lifecycle of Memory Management
A dynamic object typically goes through the following stages:
flowchart TD
A["Program needs memory"] --> B["Allocate memory"]
B --> C["Initialize object"]
C --> D["Read and modify object"]
D --> E{"Is the object still in use?"}
E -->|Yes| D
E -->|No| F["Free or reclaim memory"]
F --> G["Memory can be reused"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style D fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style E fill:#FFF5DE,stroke:#E8D29A,color:#334155
style F fill:#E8EDF2,stroke:#B8C3CE,color:#334155
style G fill:#E5F4EC,stroke:#A3CBB5,color:#334155
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
Freeing memory here does not necessarily mean that the operating system immediately reclaims the corresponding physical memory.
Many memory allocators keep freed memory inside the process so that later allocations can reuse it.
2. Which Areas Does Memory Management Involve?
Taking a common process memory layout as an example, it can be abstracted as follows:
| Memory area | Primary use | Typical contents |
|---|---|---|
| Code segment | Store program instructions | Executable code |
| Static data area | Store data with static storage duration | Global variables, static variables |
| Heap | Support dynamic memory allocation | Dynamic objects, containers |
| Stack | Support function calls | Call frames, local data |
| Memory-mapped area | Map files and other resources | Shared libraries, mapped files |
Note:
- Memory layouts differ across operating systems and runtimes.
- The stack and heap are common logical memory management concepts; they do not mean that all objects must reside in the corresponding areas.
- Compilers may perform escape analysis, register allocation, and object elimination.
- The heap managed by a garbage collector is also not equivalent to all process memory directly managed by the operating system.
II. Main Modes of Memory Management
Different programming languages adopt different memory management strategies, but these can be summarized into several main modes.
1. Manual Memory Management
Manual memory management requires programmers to explicitly control the allocation and release of dynamic memory.
Typical languages:
- C
- C++
- Some low-level system interfaces
For example, in C:
#include <stdlib.h>
#include <stdio.h>
int main() {
int *p = malloc(sizeof(int));
if (p == NULL) {
return 1;
}
*p = 100;
printf("%d\n", *p);
free(p);
p = NULL;
return 0;
}
Core operations:
malloc(): Allocate memory of the specified size.calloc(): Allocate memory and initialize it to all zero bytes.realloc(): Adjust the size of an allocated memory block.free(): Release dynamically allocated memory.
The basic process of manual management:
flowchart TD
A["Determine memory requirements"] --> B["Call allocation function"]
B --> C{"Allocation successful"}
C -->|No| D["Handle allocation failure"]
C -->|Yes| E["Initialize and use"]
E --> F["Ensure it is no longer used"]
F --> G["Call deallocation function"]
G --> H["Memory can be reused"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style C fill:#FFF5DE,stroke:#E8D29A,color:#334155
style D fill:#FBE9E9,stroke:#D9AAAA,color:#334155
style E fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style F fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style G fill:#E8EDF2,stroke:#B8C3CE,color:#334155
style H fill:#E5F4EC,stroke:#A3CBB5,color:#334155
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
Advantages:
- The timing of memory allocation and deallocation can be controlled by the program.
- Memory layout can be optimized for specific scenarios.
- Runtime management costs are relatively controllable.
- Suitable for operating systems, embedded systems, and low-level infrastructure.
Disadvantages:
- Prone to memory leaks.
- Prone to double-free errors.
- Prone to dangling pointers.
- Complex object relationships are difficult to maintain.
- In multithreaded environments, concurrent access and deallocation issues require additional handling.
Pay special attention: free() is only responsible for freeing memory and does not automatically clear all other pointers that point to that memory.
For example:
int *p = malloc(sizeof(int));
int *q = p;
free(p);
// q still stores the original address, but that address can no longer be accessed through q
At this point, q becomes a dangling pointer.
2. Garbage Collection Mechanism
Garbage Collection (GC) automatically identifies objects that are no longer needed at runtime and reclaims the memory they occupy.
Typical languages:
- Java
- C#
- Go
- JavaScript
- Python (CPython mainly uses reference counting and cyclic garbage collection)
Programs usually do not need to explicitly release ordinary managed objects.
For example, in Java:
public class Main {
public static void main(String[] args) {
User user = new User();
user.name = "Alice";
user = null;
}
}
When an object is no longer referenced by the program, it may become a garbage collection candidate.
However, this does not mean the object is released immediately after user = null is executed.
The garbage collector decides when to reclaim based on its own strategy.
Basic process:
flowchart TD
A["Create object"] --> B["Object enters managed heap"]
B --> C["Program holds object reference"]
C --> D["Reference relationships change"]
D --> E["GC determines object reachability"]
E --> F{"Is the object reachable?"}
F -->|Yes| G["Keep object"]
F -->|No| H["Mark as collectible"]
H --> I["Perform garbage collection"]
I --> J["Reclaim or compact memory"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style D fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style E fill:#FFF5DE,stroke:#E8D29A,color:#334155
style F fill:#FFF5DE,stroke:#E8D29A,color:#334155
style G fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style H fill:#FBE9E9,stroke:#D9AAAA,color:#334155
style I fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style J fill:#E8EDF2,stroke:#B8C3CE,color:#334155
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
Advantages:
- Reduce the work of manually freeing memory.
- Reduce the probability of some memory safety errors occurring.
- Simplify reference management between objects.
- Suitable for complex object relationships and large application development.
Disadvantages:
- Garbage collection consumes CPU and memory resources.
- The timing of collection usually cannot be precisely controlled by business code.
- May cause pauses or tail latency fluctuations.
- Logical memory leaks can still occur.
In particular, note:
Using GC does not mean memory leaks will not occur.
As long as an object remains reachable from a GC root object, the garbage collector may continue to retain it even if the business no longer needs it.
For example:
import java.util.ArrayList;
import java.util.List;
public class Cache {
private static final List<byte[]> DATA = new ArrayList<>();
public static void add() {
DATA.add(new byte[1024 * 1024]);
}
}
If DATA continues to grow and there is no cleanup strategy, the objects in the collection will remain reachable, eventually potentially causing memory exhaustion.
3. Automatic Reference Counting
Reference counting records how many references currently hold an object to determine whether the object can be released.
When the reference count increases, it indicates that a new owner or reference relationship has been added; when the reference count decreases, it indicates that a reference relationship has been removed.
When the reference count reaches zero, the object can usually be released immediately.
Python's CPython implementation uses reference counting as its primary memory management mechanism, combined with cyclic garbage collection to handle reference cycles.
For example:
class User:
def __init__(self, name):
self.name = name
user = User("Alice")
another = user
del user
del another
When neither variable references the object anymore, the object's reference count may reach zero.
Simplified reference counting process:
flowchart TD
A["Create object"] --> B["Initialize reference count"]
B --> C["Add reference"]
C --> D["Increment reference count"]
D --> E["Remove reference"]
E --> F["Decrement reference count"]
F --> G{"Is the reference count zero?"}
G -->|No| C
G -->|Yes| H["Release object"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style D fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style E fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style F fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style G fill:#FFF5DE,stroke:#E8D29A,color:#334155
style H fill:#E8EDF2,stroke:#B8C3CE,color:#334155
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
Advantages:
- When an object's reference count reaches zero, it can usually be released immediately.
- No need to wait for a full heap scan cycle.
- Good timeliness for some resource release scenarios.
- The implementation approach is relatively intuitive.
Disadvantages:
- Updating reference counts incurs additional overhead.
- Management costs are higher when reference relationships are complex.
- Cyclic references may prevent objects from being released.
- In multithreaded environments, concurrency safety of count updates must be handled correctly.
Circular Reference Problem
For example:
class Node:
pass
a = Node()
b = Node()
a.other = b
b.other = a
del a
del b
The two objects reference each other; even if the external variables have been deleted, a reference relationship still exists between them.
Therefore, reference counting may not necessarily release them.
CPython uses a cyclic garbage collection mechanism to handle this type of problem as a supplement.
4. Ownership and Borrowing Mechanism
Ownership is Rust's core memory management mechanism.
It does not rely on a traditional runtime garbage collector; instead, at compile time, it uses ownership rules, borrow checking, and lifetime constraints to ensure that reference usage meets memory safety requirements.
Core rules:
- Every value has an owner.
- At any given time, a value has only one primary owner.
- When the owner goes out of scope, the value it owns is destroyed.
- Values can transfer ownership through moves.
- Borrowed references are subject to rules.
For example:
fn main() {
let user = String::from("Alice");
let another = user;
println!("{}", another);
}
Here, ownership of user is moved to another.
After the move is complete, the original string can no longer be used through user.
fn main() {
let user = String::from("Alice");
let name = &user;
println!("{}", name);
println!("{}", user);
}
Here an immutable borrow is used; name is only a reference and does not acquire ownership of the string.
The Rust borrow checker checks at compile time whether references satisfy the rules, preventing many dangling-reference and data-race problems.
Basic process:
flowchart TD
A["Create value"] --> B["Establish ownership"]
B --> C{"How is the value used?"}
C -->|Move| D["Transfer ownership"]
C -->|Borrow| E["Create reference"]
D --> F["New owner manages value"]
E --> G["Borrow checking"]
G --> H{"Is the borrow valid?"}
H -->|No| I["Compile error"]
H -->|Yes| J["Access via reference"]
F --> K["Owner leaves scope"]
J --> K
K --> L["Automatically destroy value"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style C fill:#FFF5DE,stroke:#E8D29A,color:#334155
style D fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style E fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style F fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style G fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style H fill:#FFF5DE,stroke:#E8D29A,color:#334155
style I fill:#FBE9E9,stroke:#D9AAAA,color:#334155
style J fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style K fill:#E8EDF2,stroke:#B8C3CE,color:#334155
style L fill:#E5F4EC,stroke:#A3CBB5,color:#334155
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
Advantages:
- Many memory safety issues can be found at compile time.
- Traditional GC is usually not needed.
- The timing of object destruction is highly predictable.
- Complex resources can be managed without relying on a global garbage collector.
Disadvantages:
- The ownership and borrowing rules take time to learn.
- Complex reference relationships may require adjusting data structures.
- Some scenarios require
Rc,Arc, and interior mutability mechanisms. - The compiler cannot statically express all runtime business constraints, and
unsafecode still requires developers to ensure safety.
It should be emphasized that Rust lifetime annotations constrain the valid range of a reference; they do not extend the lifetime of the referenced object itself.
5. RAII and Automatic Resource Management
RAII (Resource Acquisition Is Initialization) is an important resource management concept in C++.
It binds resource acquisition to the object lifetime:
- Acquire the resource when the object is constructed.
- Release the resource when the object is destroyed.
- Manage resources through scope and object lifetime.
For example:
#include <iostream>
#include <memory>
class User {
public:
User() {
std::cout << "Created\n";
}
~User() {
std::cout << "Destroyed\n";
}
};
int main() {
{
auto user = std::make_unique<User>();
}
return 0;
}
When user goes out of scope, unique_ptr automatically destroys the object it manages.
RAII is not equivalent to garbage collection.
It still relies on deterministic object lifetimes and destruction rules; it merely automates resource-release logic through language mechanisms.
RAII applies not only to memory but also to:
- File handles.
- Mutex locks.
- Network connections.
- Database connections.
- Operating system resources.
3. Memory Management Mechanisms in Mainstream Languages
Different languages have clearly different memory management strategies; to understand them in practice, one must consider both the language specification and the specific implementation.
1. C: Manual Memory Management
C typically manages dynamic memory through malloc(), calloc(), realloc(), and free().
Example:
#include <stdlib.h>
int main() {
int *data = malloc(10 * sizeof(int));
if (data == NULL) {
return 1;
}
data[0] = 100;
free(data);
return 0;
}
Characteristics:
- Dynamic memory is explicitly allocated and freed by the programmer.
- Pointers allow direct manipulation of memory addresses.
- No language-level automatic garbage collection is provided.
- Developers must maintain resource lifetimes themselves.
Common problems:
- Memory leaks.
- Buffer overflows.
- Out-of-bounds access.
- Double free.
- Dangling pointers.
- Access to uninitialized memory.
2. C++: RAII and Smart Pointers
C++ supports manual memory management and also provides mechanisms such as RAII, smart pointers, and containers.
Common smart pointers:
| Smart pointer | Management approach | Use case |
|---|---|---|
std::unique_ptr |
Exclusive ownership | Single object owner |
std::shared_ptr |
Shared reference counting | Object shared in multiple places |
std::weak_ptr |
Weak reference; does not increase the strong reference count | Observe shared objects, break reference cycles |
Example:
#include <memory>
#include <iostream>
int main() {
auto user = std::make_unique<int>(100);
std::cout << *user << std::endl;
return 0;
}
unique_ptr automatically releases the object when it goes out of scope.
When using shared_ptr, object lifetime is usually managed through reference counting:
#include <memory>
int main() {
auto a = std::make_shared<int>(100);
{
auto b = a;
}
return 0;
}
The managed object is released only when the last strong reference is destroyed.
Note that shared_ptr reference counting cannot automatically resolve strong reference cycles; weak_ptr usually needs to be used appropriately.
3. Rust: Ownership and Borrow Checking
Rust manages memory using its ownership system.
Example:
fn main() {
let value = String::from("Hello");
let borrowed = &value;
println!("{}", borrowed);
println!("{}", value);
}
For shared ownership, Rust provides:
Rc<T>: reference counting in single-threaded environments.Arc<T>: atomic reference counting that supports sharing between threads.Box<T>: allocates an object on the heap and manages it exclusively.RefCell<T>: checks borrowing rules at runtime.Mutex<T>: controls concurrent access through a mutex.
These mechanisms do not mean that Rust has a traditional automatic tracing GC.
4. Java: JVM Garbage Collection
Java objects are usually managed by the JVM's managed heap.
Example:
public class Main {
public static void main(String[] args) {
User user = new User();
user.name = "Alice";
user = null;
}
}
The Java garbage collector determines whether objects are still reachable.
Common GC implementations include:
- Serial GC
- Parallel GC
- G1 GC
- ZGC
- Shenandoah GC
Different collectors make different trade-offs in throughput, pause time, and memory overhead.
Java also provides try-with-resources to manage non-memory resources such as files and connections:
try (var input = new java.io.FileInputStream("data.txt")) {
System.out.println(input.read());
}
Memory reclamation for ordinary Java objects is managed by the GC, but external resources such as file handles should still be explicitly closed or managed with resource-management constructs.
5. C#: CLR Garbage Collection
C# runs on the .NET CLR runtime, and ordinary managed objects are mainly managed by the GC.
For example:
public class User
{
public string Name { get; set; }
}
class Program
{
static void Main()
{
var user = new User();
user.Name = "Alice";
user = null;
}
}
The .NET GC generally uses a generational collection approach.
Common generations:
- Gen 0: typically holds younger objects.
- Gen 1: serves as an intermediate generation.
- Gen 2: typically holds longer-lived objects.
In addition, C# manages unmanaged resources through mechanisms such as IDisposable, using, and SafeHandle.
Dispose() is responsible for deterministic resource release, rather than requiring the GC to immediately reclaim managed objects.
6. Go: Concurrent Garbage Collection
Go uses automatic garbage collection, and common implementations use a concurrent mark-and-sweep strategy.
For example:
package main
type User struct {
Name string
}
func main() {
user := &User{
Name: "Alice",
}
_ = user
}
The Go compiler performs escape analysis to determine whether a variable can be safely placed on the stack or whether it needs to be allocated on the heap.
Go's memory management involves:
- The garbage collector.
- The memory allocator.
- Escape analysis.
- The stack growth mechanism.
- Concurrent marking and sweeping.
In Go, not all objects are allocated on the heap, and not all objects are reclaimed by the GC.
7. Python: Reference Counting and Cycle GC
Taking CPython as an example, Python mainly uses reference counting to manage objects, and handles specific reference cycles through cycle garbage collection.
Example:
class User:
def __init__(self, name):
self.name = name
user = User("Alice")
another = user
del user
del another
Characteristics:
- Objects are generally managed by the Python runtime.
- Reference counting is used to track the number of references to an object.
- The cycle GC handles some unreachable reference cycles.
- After an object's memory is freed, the allocator does not necessarily return that memory to the operating system immediately.
Memory management may differ across Python implementations; CPython's mechanisms cannot be directly regarded as the Python language specification.
8. JavaScript: Automatic Memory Management
Modern JavaScript engines generally use reachability analysis and garbage collection to manage object memory.
For example:
function createUser() {
const user = {
name: "Alice"
};
return user;
}
let currentUser = createUser();
currentUser = null;
When an object is no longer reachable from GC root objects, it may become a candidate for garbage collection.
JavaScript engines also typically employ:
- Generational collection.
- Incremental marking.
- Concurrent marking.
- Object compaction.
- Memory allocation optimization.
The specific strategy is determined by the engine implementation.
9. Summary of Memory Management Across Languages
| Language | Main mechanism | Release method | Main characteristics |
|---|---|---|---|
| C | Manual management | Explicit release | Strong control |
| C++ | RAII, smart pointers | Scope and ownership management | Relatively deterministic resource release |
| Rust | Ownership and borrowing | Owner goes out of scope | Compile-time safety checks |
| Java | GC | Garbage collector | Managed heap management |
| C# | GC | Garbage collector | Separate management of managed and unmanaged resources |
| Go | GC | Garbage collector | Concurrent garbage collection |
| Python | Reference counting and cycle GC | Runtime management | Dynamic object management |
| JavaScript | Reachability analysis and GC | Automatic engine reclamation | Dynamic runtime management |
IV. Core Algorithms of Garbage Collection
To understand GC, you need to further understand how the garbage collector determines whether an object can be released.
1. Reference Counting
Reference counting determines whether an object can be released by recording the number of references to it.
Core rules:
- When a new strong reference is added, the count increases.
- When a strong reference is removed, the count decreases.
- When the count reaches zero, the object can be released.
Advantages:
- Objects are usually released in a timely manner.
- No need to traverse the entire object graph every time.
- The concept is intuitive to implement.
Disadvantages:
- Reference updates incur extra overhead.
- Circular references are hard to handle.
- Correct synchronization is required in multithreaded environments.
2. Mark-Sweep
Mark-sweep is a classic tracing garbage collection algorithm.
It mainly consists of two phases:
Mark phase
Starting from the GC Roots, traverse all reachable objects and mark them.
Sweep phase
Traverse the objects in the heap and reclaim those that were not marked.
flowchart TD
A["Start garbage collection"] --> B["Identify GC Roots"]
B --> C["Traverse object references"]
C --> D["Mark reachable objects"]
D --> E["Scan heap memory"]
E --> F{"Is the object marked?"}
F -->|Yes| G["Keep object"]
F -->|No| H["Reclaim object"]
G --> I["End this collection cycle"]
H --> I
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style D fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style E fill:#E8EDF2,stroke:#B8C3CE,color:#334155
style F fill:#FFF5DE,stroke:#E8D29A,color:#334155
style G fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style H fill:#FBE9E9,stroke:#D9AAAA,color:#334155
style I fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
Advantages:
- Can handle circular references.
- No need to maintain an accurate reference count for every object.
- Can manage complex object graphs.
Disadvantages:
- Requires traversing object references.
- May incur significant collection overhead.
- Sweeping alone may lead to memory fragmentation.