Memory Management in Rust: Stack vs. Heap
Tags: rust, tutorial, beginners, programming
Published: 2024-03-17 18:30:00

In programming, memory management plays a crucial role in ensuring safety and efficiency in code execution. Two primary areas where memory is allocated and utilized are the stack and the heap. Understanding the differences between these memory areas is fundamental for writing efficient and memory-safe Rust code. In this article, we'll delve into the concepts of stack and heap memory in Rust, exploring how they work, their differences, and when to use each.

## Stack Memory

The stack operates on a **Last In, First Out (LIFO)** principle, akin to stacking plates. It's used for storing fixed-size data with predictable lifetimes. In Rust, primitive types and local variables of functions are allocated on the stack by default. Memory allocation and deallocation on the stack are automatic and managed by the compiler. Let's visualize stack memory allocation with an example:

```rust
fn main() {
    let x = 111;
    foo();
}

fn foo() {
    let y = 999;
    let z = 333;
}
```
When `main()` function executes, we allocate a single 32-bit integer (`x`) to the stack frame.

| **Address** | **Name** | **Value** |
| ----------- | -------- | --------- |
| 0           | x        | 111       |


In the table, the "Address" column refers to the memory address of the RAM. The "Name" column refers to the variable, and the "Value" column refers to the variable's value.

When `foo()` is called a new stack frame is allocated. The `foo()` function has two variable, `y` and `z`.

| **Address** | **Name** | **Value** |
| ----------- | -------- | --------- |
| 2           | z        | 333       |
| 1           | y        | 999       |
| 0           | x        | 111       |


> **Note:** The numbers 0, 1, and 2 are not actual memory addresses.

After `foo()` is completed, its stack frame is deallocated.

| **Address** | **Name** | **Value** |
| ----------- | -------- | --------- |
| 0           | x        | 111       |

Finally, `main()` is completed, and everything goes away. Rust automatically does allocation and deallocation of memory in and out of the stack.

## Heap Memory

Unlike the stack, heap memory allows for dynamic allocation of data with arbitrary sizes and lifetimes. In Rust, heap memory is managed explicitly using constructs like `Box<T>`. Complex data types like Strings, Vectors, and Boxes are allocated on the heap. Heap memory allocation and deallocation require more management and can be less predictable compared to the stack. Let's see an example of heap memory allocation:

```rust
fn main() {
    let x = Box::new(100);
    let y = 222;
    
    println!("x = {x}, y = {y}");
}
```
Here, we allocate two variables, `x` and `y`, on the stack. However, the value of `x` is allocated on the heap when `Box::new()` is called. Thus, the actual value of `x` is a `pointer` to the heap.

**Stack memory:**

| **Address** | **Name** | **Value** |
| ----------- | -------- | --------- |
| 1           | y        | 222       |
| 0           | x        | -->5476   |

**Heap memory:**

| **Address** | **Name** | **Value** |
| ----------- | -------- | --------- |
| 5476        |          | 100       |

Here, the variable `x` holds a pointer to the address `→5678` (an arbitrary address used for demonstration). Heap can be allocated and freed in any order. Thus it can end up with different addresses and create holes between addresses.

So when `x` goes away, it first frees the memory allocated on the heap. Once the `main()` is completed, we free the stack frame, and everything goes away, freeing all the memory.

We can make the memory live longer by transferring ownership where the heap can stay alive longer than the function which allocates the `Box`.

## Differences between Stack and Heap

- Accessing data in the stack is faster than in the heap due to its predictable nature.
- Stack memory management is straightforward and predictable, while heap management is more complex.
- Rust allocates primitive types and local variables on the stack by default, while heap allocation requires using `Box<T>` or similar constructs.
- Stack is suitable for fixed-size data with predictable lifetimes, while the heap is ideal for dynamic data with uncertain lifetimes.

## Conclusion

Understanding the nuances of stack and heap memory management in Rust is crucial for writing efficient and safe code. By leveraging stack memory for fixed-size data and heap memory for dynamic allocations, Rust developers can optimize their programs for performance and memory usage. Additionally, Rust's ownership and borrowing system ensures memory safety by enforcing strict rules on how data is accessed and manipulated, further enhancing the reliability of Rust code.