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.
When appropriate, large lists and nested lists of numbers are automatically stored as packed arrays of machine-sized integers or real numbers. The Mathematica compiler is automatically used to compile complicated functions that will be repeatedly applied to such packed arrays. MathLink, DumpSave, Display, as well as various Import and Export formats, make external use of packed arrays.