Memory Lab

Five simulations of what happens in memory when your code runs: what an assignment copies, where frames and objects live, why contiguous storage is friendly to caches, what a growing buffer does to pointers into it, and when memory and resources are actually released. Each one follows the rules of the language you pick, and each comes with a tested program in C#, Python, Java and C++.

Addresses are small teaching numbers and the cache is a simplified model. They explain the mechanism; they are not measurements of any machine.

  1. Simulation 1

    Values and references

    Assignment copies either the data or only a reference to it. Watch a copy stay independent while an alias sees every change.

    • values and references
    • pointers
    • copying

    Lesson: Values, references and pointers

  2. Simulation 2

    Stack frames and heap objects

    Each call pushes a frame for its parameters and locals; objects live on the heap until nothing refers to them. Overflow the teaching stack to see what each runtime does.

    • stack and heap
    • call stack
    • overflow
    • allocation

    Lesson: The call stack and the heap

  3. Simulation 3

    Contiguous array vs linked nodes

    The same values traversed as an array and as a linked list, with teaching addresses and a simplified cache-line model that counts line loads.

    • addresses
    • contiguous storage
    • cache locality
    • fragmentation

    Lesson: Contiguous storage, addresses and cache locality

  4. Simulation 4

    Reallocation and invalidated references

    A dynamic array outgrows its buffer and moves. See what happens to a pointer, span, iterator or memoryview that pointed into the old one.

    • allocation
    • copying
    • pointers
    • addresses

    Lesson: Reallocation and invalidated references

  5. Simulation 5

    Reachability, garbage collection and RAII

    Mark everything the roots can reach, sweep the rest, and compare with cleanup that happens at a known point: using, try-with-resources, with and destructors.

    • GC and RAII
    • allocation

    Lesson: Reachability, garbage collection and RAII

Where each M02 topic shows up

The Memory Lab is the interactive half of module M02, Memory Model. Fragmentation appears as the free gaps between scattered list nodes and the hole a freed buffer leaves; there is no allocator simulation.

values and references
Values and references