tezvyn:

How does Rust ownership avoid Go GC's non-deterministic pauses?

AI-drafted, machine-checkedSource: doc.rust-lang.orgintermediate

Tests if you know Rust's compile-time ownership eliminates GC pauses by making deallocation deterministic at scope boundaries. A strong answer contrasts Go's STW with Rust's immediate Drop and zero-cost compile-time checks.

WHAT THIS TESTS: The interviewer wants to know if you understand the trade-off between automatic garbage collection and compile-time memory management, specifically how Rust's ownership model shifts memory reclamation work from runtime to compile time. They are looking for awareness of latency predictability, the zero-cost nature of ownership checks, and the distinction between stack and heap behavior in both languages.

A GOOD ANSWER COVERS: First, acknowledge that Go uses a concurrent garbage collector which, while highly optimized, still introduces non-deterministic stop-the-world pauses and CPU overhead as it scans the heap to find unreachable objects. Second, explain that Rust has no garbage collector; instead, memory is managed through a system of ownership rules enforced by the compiler. Third, state that because ownership is checked at compile time, none of these checks slow the program at runtime, meaning deallocation is deterministic and happens immediately when a value goes out of scope via the Drop trait. Fourth, note that Rust still uses the heap for data with unknown or dynamic size, but the compiler tracks exactly how long that data lives, so there is no surprise pause to reclaim it. Fifth, contrast the stack where function calls push and pop fixed-size values in LIFO order, making allocation and deallocation trivial, with the heap where Rust's explicit lifetimes prevent the need for a runtime sweeper.

COMMON WRONG ANSWERS: Claiming that Rust never allocates on the heap. Confusing Rust's ownership model with reference counting like Rc or Arc, which do introduce runtime overhead but are not the default. Asserting that Rust has zero memory management cost rather than zero runtime cost; the cost is paid in developer ergonomics and compile times. Saying Go's GC is bad or naive; Go's collector is sophisticated, but any tracing GC trades latency for convenience. Failing to mention that ownership rules are what enable the predictability, not just the absence of a GC.

LIKELY FOLLOW-UPS: How does Rust handle shared ownership or cycles without a GC? When would you use Box, Rc, or Arc, and what runtime costs do they introduce? How does escape analysis in Go compare to Rust's borrow checker for stack allocation? What happens when Rust programs need dynamic memory lifetimes, such as in complex graph structures?

ONE CONCRETE EXAMPLE: In a latency-sensitive network proxy handling thousands of requests per second, Go might see occasional 100-microsecond to millisecond-scale STW pauses during heap growth, causing tail latency spikes. In Rust, a request buffer allocated as a Vec of u8 is owned by the request handler; when the handler function returns, the Vec is dropped immediately and its heap memory is freed right then, with no background thread involved and no unpredictable pause.

Read the original → doc.rust-lang.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.