A.9.2 Data Structures and Memory Management
A Mathematica expression internally consists of a contiguous array of pointers, the first to the head, and the rest to its successive elements.
Each expression contains a special form of hash code which is used both in pattern matching and evaluation.
For every symbol there is a central symbol table entry which stores all information about the symbol.
Most raw objects such as strings and numbers are allocated separately; unique copies are however maintained of small integers and of certain approximate numbers generated in computations.
Every piece of memory used by Mathematica maintains a count of how many times it is referenced. Memory is automatically freed when this count reaches zero.
The contiguous storage of elements in expressions reduces memory fragmentation and swapping. However, it can lead to the copying of a complete array of pointers when a single element in a long expression is modified. Many optimizations based on reference counts and pre-allocation are used to avoid such copying.