Memory Management

cat-memory-management

Deal with allocation, memory mapping, garbage collection, reference counting, or interfaces to foreign memory managers.

Rust's memory management is a core strength: you won't often need to manually manage memory like in C/C++. For most common Rust development, relying on language features like ownership, borrowing, and lifetimes, and using smart pointers like Box, Rc, and Arc will be sufficient. Avoid unsafe code and raw pointers unless absolutely necessary.

TopicRust Crates or Features
Smart PointersUse std::boxed::Box for heap allocation; std::rc::Rc for reference-counted shared ownership; std::sync::Arc for atomically reference-counted shared ownership (thread-safe); std::cell::RefCell for interior mutability; and std::sync::Mutex for safe mutable access from multiple threads.
Global Statics and Lazy InitializationFIXME
Core Allocation (Rarely Used Directly)alloc (Standard library) provides the fundamental allocation APIs. Most other memory management tools are built on top of it.
Specialized Allocatorswee_alloc is a small and efficient allocator, often used in embedded systems or WebAssembly.
Memory Profilingvalgrind (with massif or memcheck): External tool. Powerful memory profiler. heaptrack: External tool. Heap profiler.

Smart Pointers (for Managing Memory beyond Basic ownership)

  • Box: For allocating data on the heap.
  • Rc (Reference Counting): For shared ownership of data.
  • Arc (Atomic Reference Counting): For shared ownership across threads.
  • RefCell: For interior mutability (allowing you to mutate data even when there are immutable references to it).
  • Mutex: For safe mutable access to data from multiple threads.

Global Statics and Lazy Initialization

Unsafe Code and Raw Pointers

Use unsafe code and raw pointers (*const T, *mut T) only when absolutely necessary for interacting with external code or hardware. They bypass Rust's safety guarantees and require very careful manual memory management.

Custom Memory Allocation, Garbage Collection

The core alloc crate provides the core allocation APIs. You'll rarely use this directly, but it's what the other memory management tools are built on. Custom allocators are rarely needed in typical Rust development.

  • wee_alloc is a small and efficient allocator often used in embedded systems or WebAssembly.

Rust does not have a garbage collector in the traditional sense. It uses ownership and borrowing to manage memory automatically and deterministically. If you need garbage collection for specific reasons, you'd have to look for specialized crates, but this is rare in Rust.

Memory Safety Tools

Memory profiling tools like Valgrind are useful for optimizing memory usage.

  • valgrind (with massif or memcheck): While not Rust-specific, Valgrind is a very common and powerful memory profiler. You'd run your Rust program under Valgrind.
  • heaptrack: A heap profiler that can track memory allocations.

See Development Tools: Profiling and Memory Usage Analysis.