]> git.lizzy.rs Git - rust.git/blob - src/doc/reference/src/memory-model.md
2798b0d165f7fdda4fa08d130b401987b07ac3f8
[rust.git] / src / doc / reference / src / memory-model.md
1 # Memory model
2
3 A Rust program's memory consists of a static set of *items* and a *heap*.
4 Immutable portions of the heap may be safely shared between threads, mutable
5 portions may not be safely shared, but several mechanisms for effectively-safe
6 sharing of mutable values, built on unsafe code but enforcing a safe locking
7 discipline, exist in the standard library.
8
9 Allocations in the stack consist of *variables*, and allocations in the heap
10 consist of *boxes*.
11
12 ## Memory allocation and lifetime
13
14 The _items_ of a program are those functions, modules and types that have their
15 value calculated at compile-time and stored uniquely in the memory image of the
16 rust process. Items are neither dynamically allocated nor freed.
17
18 The _heap_ is a general term that describes boxes.  The lifetime of an
19 allocation in the heap depends on the lifetime of the box values pointing to
20 it. Since box values may themselves be passed in and out of frames, or stored
21 in the heap, heap allocations may outlive the frame they are allocated within.
22 An allocation in the heap is guaranteed to reside at a single location in the
23 heap for the whole lifetime of the allocation - it will never be relocated as
24 a result of moving a box value.