Memory Management
29 bites tagged Memory Management — interview questions with model answers, and 60-second explainers.
Why use a C++ TurboModule, and what are the risks?
C++ JSI bindings give synchronous, serialization-free calls and shared cross-platform code; challenges are manual memory ownership across the JS-C++ boundary, GC interaction, and thread safety. low-level native integration.
Rust lifetimes versus Go garbage-collected lifetimes
A lifetime is a compile-time region a reference is valid for; annotations like 'a relate input and output reference durations; Go instead uses garbage collection and escape analysis. understanding of how Rust tracks reference validity.
Describe Hilt's component hierarchy and scope injection rules
Tests whether you understand Hilt's generated component tree and scoping rules. A strong answer lists the hierarchy, explains parent-outlives-child semantics, and states that ActivityScoped-into-Singleton injection fails at compile time.
How does Debug Memory Graph find leaks and retain cycles?
This tests knowledge of Xcode's heap visualization versus allocation sampling. A strong answer notes the graph shows objects, draws retain cycles as loops, and contrasts with Leaks time-based sampling. Red flag: expecting leak flags without inspection.
Explain Copy-on-Write in Swift and implement it for custom structs
Tests value semantics with reference storage and uniqueness checks. A strong answer explains CoW delays copy until mutation via isKnownUniquelyReferenced, lists custom steps: wrap, read, check, clone. Red flag: assuming structs are automatically CoW.
What is a closure capture list? Explain [weak self] versus [unowned self].
Define capture lists, contrast [weak self] safe optionality against [unowned self] crash risk, and match each to lifetime guarantees. ARC memory safety in Swift closures.
What is a retain cycle in ARC with closures?
It tests reference counting and closure capture semantics. A retain cycle forms when a closure captures self strongly while self holds the closure; break it with weak if self can deallocate, or unowned if it will outlive the closure.
Swift struct vs class: differences and when to choose each
Tests value vs reference semantics and let mutability. Strong answers contrast copy behavior with shared references, note allocation tendencies, and choose struct for value semantics or class for identity.
Explain the primary differences between a struct and a class in Swift
Tests value versus reference semantics and their impact on memory and mutation. Strong answers note structs copy on assignment and lack inheritance, while classes share heap instances via ARC. Red flag: claiming structs are always stack-allocated.
Profiling iOS Memory with Instruments
Instruments X-rays your heap to catch objects that outstay their welcome. Profile image-heavy features or when jetsam kills your app. Never trust Simulator memory numbers; always validate on physical hardware.
Swift Closures: Functions with a Memory
A closure is a function carrying luggage: it captures surrounding variables and remembers them later. Use them in SwiftUI actions, async completions, and array transforms. Beware a strong reference cycle from capturing self without a capture list.
Use a C malloc'd char* in Go and Rust, then free it
Tests FFI allocator discipline. In Go, copy with C.GoString then C.free the *C.char. In Rust, read via CStr::from_ptr, copy to String, then libc::free. Red flag: letting Go GC or Rust Drop manage C memory, or using CString::from_raw on C malloc'd pointers.
Explain Rust Rc and Arc versus Go's tracing GC
This tests deterministic reference counting versus tracing GC. A strong answer contrasts Rc's heap reference counts with Go's root tracing, and notes Rc cannot reclaim cycles while Go's GC can. Red flag: claiming Rc has no cycle leak risk.
Explain Go escape analysis and Rust ownership for stack vs heap
Tests compiler-driven memory placement. Go escape analysis keeps non-escaping locals on stack, shrinking heap and GC work. Rust ownership lets the compiler pick stack or heap at build time with zero cost. Saying Go eliminates GC or Rust uses one.
How does Rust ownership avoid Go GC's non-deterministic pauses?
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.
Describe Go's memory management, garbage collection, and trade-offs
Explain Go's GC recycles heap memory, the compiler stack-allocates locals, and automatic collection costs runtime overhead. automatic memory management and stack versus heap division.
How do you append to a Go slice and why reassign?
Tests slice headers and append reallocation. A strong answer reassigns the result (s = append(s, 4)), explains that append may allocate a new backing array, and warns that ignoring the return value drops elements. Red flag: calling append without assignment.
Compare Go string and Rust &str/String types, mutability, UTF-8, ownership
This tests your model of immutable UTF-8 strings versus owned buffers. A strong answer contrasts Go's read-only header with Rust's &str borrow and heap-owned String, noting Go immutability is structural while Rust gates mutation via ownership.
Compare Go's GC and Rust's ownership across performance, productivity, and safety
This tests memory-model trade-offs. Contrast Rust's compile-time ownership for deterministic, zero-cost safety against Go's GC, which optimizes simplicity and onboarding but adds runtime overhead. Red flag: calling one strictly superior.
Go Escape Analysis Chooses Stack Over Heap
Go escape analysis is the compiler pass that decides whether a variable lives on the stack or heap. It avoids heap allocations when data stays local, but any pointer that outlives its function escapes. The footgun is assuming small values never allocate.
Propose a Dart FFI approach to share camera frames and its risks
Tests zero-copy frame sharing via Dart FFI plus ownership and thread hazards. Propose native allocation, pass pointer to Dart as external TypedData, use ring buffer, then free; cite use-after-free and races.
Explain StreamController, write a broadcast stream example, and why close it?
This tests Dart stream lifecycle and memory safety. A strong answer uses StreamController.broadcast(), adds data and errors, closes it, and explains unclosed controllers leak memory.
Rust's Pin: Fixing a Value's Memory Address
Pin<P> tells the Rust compiler a value must not move from its memory location. Think of it as nailing an object to a specific spot on the memory shelf. This is crucial for self-referential types, like those in async runtimes.
Go's `unsafe` Package: Breaking the Rules for Performance
Go's `unsafe` package lets you bypass type safety, treating memory like C with raw pointers for performance gains. It's used for low-level optimizations and C interoperability. The footgun: its behavior isn't guaranteed across Go versions, making code fragile.
Get Memory Management bites daily.
Five a day, five minutes, offline. With quizzes so it sticks.
Open testing — you’ll join as an early tester.