Skip to content

Object lifetime

Category: Safety in languages · Status: stub · Lessons: chapter 01, Threads

One line: The span in which a value's storage is valid, and the question of who guarantees that every reference to it is dropped first — the compiler, the programmer, or a garbage collector.

Also called: lifetime, storage duration, extent.

How it connects

In each language

Rust Lifetimes ↗ are part of a reference's type, and 'static means the reference may outlive any caller
Go Decided by the compiler: the FAQ ↗ says a local whose address escapes is allocated on the heap, and the collector frees it
C Lifetime ↗: an automatic object's lifetime ends when its block does, and a pointer to it is then indeterminate
C++ Lifetime ↗, and storage duration ↗ for where it comes from
Java Decided by reachability: JLS 12.6 ↗ says an object is finalizable once it is no longer reachable
Python Reference counting and the cyclic collector ↗; weakref ↗ holds a reference that does not keep the object alive

Where to read more