Memory Management in High-Level Languages: C++ vs. Go vs. Rust
C++, Go, and Rust are three high-level languages with three distinctly different styles of memory management. Their styles also indirectly trace the evolution of high-level languages, and one of the most important threads in that evolution is how memory management has changed.
1. Introduction: Why Memory Management Matters
Memory management is a cornerstone of high-level programming languages. It affects a program's performance, security, and stability.
- Performance: A poorly written program requests large amounts of memory but uses little of it, so actual memory utilization is low. Programs like this cause performance problems.
- Security: Security problems mainly stem from allocated memory that is never properly freed. The pointer that referenced it gets overwritten with another value, so the memory it pointed to can never be accessed again. This is a memory leak, and leaked memory regions can be used to plant malicious code for attacks, which leads to security problems.
- Stability: High-level languages customarily use variables (pointer variables) to reference a block of memory. If that memory has already been freed but the variable keeps referencing and using it, a memory-management fault is triggered. This is the use of a dangling pointer.
These three problems have all come up over the course of high-level language development.
C++, Go, and Rust use different memory management strategies:
- Manual management: C++ uses manual management, which demands more skill from developers.
- Garbage collection: In Go, memory allocation and reclamation are scheduled by its runtime. Developers do not have to deal with memory management, but the runtime scheduler spends some resources doing the reclamation.
- Ownership system: Rust takes a unique approach. It uses compile-time checks to eliminate memory problems at the code level, but it introduces higher-level abstractions and has a steeper learning curve.
2. C++: The Art of Manual Management and Smart Pointers
C++ is an old language that remains critically important today. But C++ keeps evolving with new versions, so its memory management can be viewed as two approaches: traditional manual management and modern management.
Traditional Manual Memory Management
This is essentially the C style, except that C++ usually uses the new and delete keywords to allocate and free memory (similar to C calling memory-management functions directly).
In this approach, for every object obtained via new, you must know where it is released with delete, make sure it does get released, and make sure it is never released twice, so programming requires special care. Programs tend to be complex, though, and this manual approach is very error-prone and imposes a heavy cognitive load.
For example:
int* alloc_int() {
Int* i = new Int(); // allocate memory directly on the heap
assert(i != NULL);
return &(i->integer); // return the address of a member; i becomes a dangling pointer
}
int n = *alloc_int();
// Here, the Int object allocated inside alloc_int() can never be freed: the function returns a pointer, but only to a member of the object, which is only good for reading its value
int* p = alloc_int();
// If we take the member pointer directly
delete p; // the program crashes
// p points only to the member's address, which cannot be freed; what could be freed is the object pointer, but it was never returned
// Now define another version of the function
Int* alloc_int() {
Int* i = new Int(); // allocate memory directly on the heap
assert(i != NULL);
return i; // return the object pointer
}
Int* p = alloc_int();
// Allocating p and not freeing it after use is a problem
delete p; // freeing it this way is fine
This forces programmers to pair every new with exactly one delete themselves. If the control flow is complex, they have to analyze where each path goes and check that every branch still satisfies the one-to-one pairing.
Modern Memory Management
Modern C++ memory management starts with the C++0x standard, and much of modern C++ is based on it; of course, more modern features require more recent standards. Modern C++ introduced an important concept: smart pointers, which are really an application of RAII (Resource Acquisition Is Initialization).
The three main smart pointers are:
unique_ptr: a smart pointer with exclusive ownership.shared_ptr: a smart pointer with shared ownership; the object internally uses reference counting to manage its lifetime.weak_ptr: an observer smart pointer, mainly used to solve the problem of reference cycles.
C++ smart pointers introduce the abstraction of an owner and manage memory through that owner, which avoids memory leaks and improper frees and greatly eases the pain of manual memory management. This approach gives a high degree of language-level flexibility, but it requires developers with deep experience and a rigorous attitude.
The following examples show the three smart pointers in use, starting with unique_ptr and shared_ptr:
class Test {
public:
void doSomething() {}
};
// Demonstrates unique_ptr
void testUnique() {
// Create a unique_ptr
std::unique_ptr<Test> uPtr1 = std::make_unique<Test>();
// A smart pointer can be used just like a raw pointer
uPtr1->doSomething();
// unique_ptr has exclusive ownership, so ownership can only be transferred
std::unique_ptr<Test> uPtr2 = std::move(uPtr1);
// uPtr1 is no longer valid at this point
assert(uPtr1 == nullptr);
// Same call as before
uPtr2->doSomething();
}
// Demonstrates shared_ptr
void testShared() {
// Create a shared_ptr
std::shared_ptr<Test> sPtr1 = std::make_shared<Test>();
// Use it like a raw pointer
sPtr1->doSomething();
// Copy the pointer; the reference count goes up by 1
std::shared_ptr<Test> sPtr2 = sPtr1;
// Call the method again; any state change made here is visible through sPtr1 too, since the object is shared
sPtr2->doSomething();
// Scope example
{
// Copy once more
std::shared_ptr<Test> sPtr3 = sPtr1;
// Use the shared object
sPtr3->doSomething();
// On leaving the scope, the reference count of the object sPtr3 points to drops by 1
}
// On leaving the scope, sPtr2 and sPtr1 also decrement the count; when it reaches 0, the object is freed automatically
}
That was a simple demonstration of shared_ptr. Next, let's look at shared_ptr in a more complex situation, which is where weak_ptr comes into play.
The main purpose of weak_ptr is to solve problems that arise with shared_ptr, as in this example:
class T2;
class T1 {
public:
std::shared_ptr<T2> sPtr;
};
class T2 {
public:
std::shared_ptr<T1> sPtr;
};
// Demonstrates weak_ptr
void testWeak1() {
// weak_ptr exists to solve a problem with shared_ptr
// Create two shared pointers
std::shared_ptr<T1> sPtr1 = std::make_shared<T1>(); // count 1
std::shared_ptr<T2> sPtr2 = std::make_shared<T2>(); // count 1
// Make them reference each other
sPtr1->sPtr = sPtr2; // count 2
sPtr2->sPtr = sPtr1; // count 2
// Returning from the function here leaks memory: both sPtr1 and sPtr2 still have a reference count of 1, so the objects are considered in use
// The overall release sequence:
// sPtr1 created on the stack (1) -> referenced by sPtr2 (2) -> stack sPtr1 destroyed (1)
// sPtr2 created on the stack (1) -> referenced by sPtr1 (2) -> stack sPtr2 destroyed (1)
}
// A fixed version demonstrating weak_ptr
class T3 {
public:
std::shared_ptr<T3> sPtr;
};
class T4 {
public:
std::weak_ptr<T3> sPtr; // references without counting
};
void testWeak2() {
// Create two shared pointers
std::shared_ptr<T3> sPtr1 = std::make_shared<T3>();
std::shared_ptr<T4> sPtr2 = std::make_shared<T4>();
// Make them reference each other
sPtr1->sPtr = sPtr2; // counted normally
sPtr2->sPtr = sPtr1; // weak_ptr, does not increase the count
// Returning from the function here frees all the memory
// The overall release sequence:
// sPtr1 created on the stack (1) -> observed by sPtr2, not referenced (1) -> stack sPtr1 destroyed (0)
// sPtr2 created on the stack (1) -> referenced by sPtr1 (2) -> sPtr1 destroyed (1) -> stack sPtr2 destroyed (0)
}
Accessing the object behind a weak_ptr
A
weak_ptrcannot guarantee that its object is still accessible (i.e., not yet freed), so it is usually used together withlock():cppif (auto sp = sPtr1->sPtr.lock()) { // sp is a shared_ptr<T2> // safe to use } else { // T2 has already been destroyed }
So although C++ smart pointers ease the problems of manual memory management, the owner-based lifetime management they introduce still seems rather cumbersome in complex scenarios such as reference cycles, and requires some "ingenious" design and coding at the code level.
3. Go: GC, a Blessing for Programmers?
The core of Go's memory management is automatic garbage collection (GC): the runtime periodically scans memory and automatically reclaims blocks that are no longer referenced.
Similar GC mechanisms exist in other high-level languages such as Java and Python. Languages that run on a stateful runtime virtual machine basically all manage memory through GC.
With GC-based memory management, developers carry little cognitive load for memory. Almost all memory is handled well by the GC, and developers can focus on business logic. But GC has its downsides. First is the performance overhead: every GC cycle consumes CPU, and the more objects there are, the larger that cost grows. GC scheduling is also unpredictable, so if a program is sensitive to real-time behavior, Go is a poor fit (for example, Go is not suited to developing game engines).
The following example illustrates these problems:
// This function prints the game engine's status periodically
func logEngineEveryPeriod() {
for {
fmt.Printf("[%v] engine status: %v", time.Now(), engine.GetStatus())
time.Sleep(time.Millisecond * 100)
}
}
// This function allocates a lot of memory
func allocImage() *Image {
// ...
}
// Demo function
func testGo() {
// Run the goroutine asynchronously in the background
go logEngineEverySecond()
for sp := range sprites {
img := allocImage()
sp.FitImage(img)
}
}
The code above has one obvious problem: logEngineEveryPeriod() cannot do its job. The memory allocation makes GC take longer, which delays the log output, so there is no guarantee that it prints exactly every 100 milliseconds.
Recent Go versions have heavily optimized GC-induced latency, and in practice the results are now excellent. Unless a task is extremely latency-sensitive, Go can basically handle it.
Apart from GC latency, is Go's memory management otherwise perfect? Not quite.
Go still has memory leaks; they just take a different form. There are two main kinds:
-
Memory leaks caused by global objects holding references
This is the most common kind. In essence, a long-lived object (sometimes one that lives for the whole program) unintentionally references an object that should have been short-lived. When the GC scans, it sees the short-lived object is still referenced by the long-lived one, considers it "alive," and cannot reclaim it.
For example:
go// A global cache that simulates a memory leak var cache = make(map[int][]byte) // Allocate a block of memory func getData(id int) []byte { // Each call adds a large byte slice to the cache data := make([]byte, 1024*1024) // 1MB cache[id] = data return data } // Demo function func demoGlobalRef() { // Iterate, allocating and using memory for item := range GetAllItems() { // Allocate a block of memory mem := getData(item.GetIndex()) // Use the memory and print the result item.UseMemory(mem) item.PrintResult() } }In
demoGlobalRefabove, asitemkeeps iterating, more and more data is referenced by the sharedcache. Memory keeps climbing until it is exhausted and the service crashes. -
Memory retention caused by misusing
sliceThis is another subtle but common leak. When you slice a small
sliceout of a largeslice, then as long as the original largesliceis still in memory, the underlying array it depends on will not be reclaimed by the GC, even after the smallsliceis no longer referenced.Sample code:
go// Return a large slice func createBigSlice() []byte { return make([]byte, 1024*1024*10) // 10MB } // Wrong: slice directly from the large slice func getFirstNBytesWrong() []byte { bigSlice := createBigSlice() // Returns a small slice, but its underlying array is still bigSlice's large array return bigSlice[:1024] } // Right: copy the data func getFirstNBytesCorrect() []byte { bigSlice := createBigSlice() // Create a new, small slice smallSlice := make([]byte, 1024) // Copy the data over so bigSlice is no longer referenced copy(smallSlice, bigSlice) return smallSlice } // Demo function func demoSlice() { // This slice leaks memory leakedSlice := getFirstNBytesWrong() // This slice is fine correctSlice := getFirstNBytesCorrect() }As shown above, if you return a sub-slice taken directly from a large array, the large array is still referenced and will not be reclaimed by the GC. To use memory more efficiently and reduce memory overhead, create a new small slice,
copythe needed content into it, and return that instead. The large array is then no longer referenced and can be reclaimed by the GC.
4. Rust: The Ownership System, the Ultimate Answer to Memory Safety
As we have seen, C++ smart pointers require developers to be extremely careful, which brings a lot of mental burden. Go removes that burden at a stroke, but it depends on GC and inherits GC's drawbacks; you also still have to watch variable references, or memory may never be freed. It seems there is no perfect solution.
In fact there is one: Rust's approach to memory management. Rust's core ideas are compile-time checking and zero runtime overhead, built on three rules:
- Ownership: Every value has an owner.
- Move: When the owner goes out of scope, the value is dropped.
- Borrowing: Data can be accessed through a mutable borrow or immutable borrows, but not both at the same time.
This approach borrows the core design ideas of C++ smart pointers but pushes them to the extreme. C++ smart pointers are an after-the-fact remedy (for historical reasons), whereas Rust is preventive: it makes ownership mandatory and extends it to every type, eliminating the problems at their root.
The following code demonstrates this memory management mechanism:
// A simple function showing ownership transfer
fn take_ownership(some_string: String) { // some_string takes ownership
println!("I own this string: {}", some_string);
} // some_string goes out of scope and its memory is freed automatically
// A function showing borrowing
fn borrow_and_print(some_string: &String) { // some_string is an immutable borrow
println!("I'm borrowing this string: {}", some_string);
} // some_string goes out of scope; the borrow ends
// A function showing mutable borrowing
fn borrow_and_change(some_string: &mut String) { // some_string is a mutable borrow
some_string.push_str(" and I was changed!");
} // some_string goes out of scope; the borrow ends
// Demo function
fn demo_rust_mem() {
// --- 1. Ownership example ---
let s1 = String::from("hello world"); // s1 owns "hello world"
let s2 = s1; // ownership moves from s1 to s2
// From this point on, s1 is no longer valid! The compiler reports an error
// println!("s1 is: {}", s1); // compile error! value borrowed here after move
println!("s2 is: {}", s2);
// Calling the function transfers ownership
let s3 = String::from("take me away");
take_ownership(s3);
// println!("s3 is: {}", s3); // compile error! ownership of s3 has been moved
// --- 2. Borrowing example ---
println!("\n--- Borrowing example ---");
let s4 = String::from("this is a borrowed string");
borrow_and_print(&s4); // a borrow of s4 is passed
println!("s4 is still valid after borrowing: {}", s4); // s4 is still valid after the borrow ends
// --- 3. Mutable borrowing example ---
println!("\n--- Mutable borrowing example ---");
let mut s5 = String::from("I am mutable");
borrow_and_change(&mut s5);
println!("s5 has been changed: {}", s5);
// --- 4. Borrow conflict example ---
println!("\n--- Borrow conflict example ---");
let mut s6 = String::from("I have multiple references");
// Multiple immutable borrows are allowed
let r1 = &s6;
let r2 = &s6;
println!("{} and {}", r1, r2);
// But a mutable borrow is not allowed after immutable borrows
// let r3 = &mut s6; // compile error! cannot borrow as mutable...
// --- 5. Dangling pointers (prevented by the compiler) ---
// The function below does not compile because it would return a dangling reference
// fn dangle() -> &String {
// let s = String::from("dangling"); // s is created here
// &s // return a reference to s
// } // s goes out of scope here and is dropped; its reference would dangle!
// The compiler reports: `s` does not live long enough
}
As you can see, Rust manages memory through ownership and rules out logic that could cause memory problems at compile time. This guarantees memory safety, and since it does not depend on GC, it is also more efficient.
5. Comparison and Selection Guide
Comparing the memory management mechanisms of the three languages: C++ suits workloads with extreme performance requirements, and it is essential when you need control over low-level hardware, but choosing C++ also brings the complexity of systems programming. Go suits workloads that are not latency-critical, such as high-concurrency network services, backend development, and fast-iterating business systems; compared with C++ and Rust, its syntax is easier to pick up and relatively simple. If the system you are building has strict memory-safety requirements and must also deliver high performance, Rust is a strong option, for example in securities trading systems, embedded systems, and operating systems.
Overall, different languages take different approaches to memory management. There is no single best approach, only the one best suited to a specific project. As the industry evolves, though, memory safety is an important direction for programming languages, and Rust's ownership model offers a fresh way of thinking about it.