Skip to content

Stack & Heap

Task: Watch what survives when a function returns

STACKHEAPStudentmain()aframes leave in the reverse order they arrived

What you are watching

A call stack and a heap side by side. main starts with one frame, calls create which adds a second on top, and create allocates a Student object in the heap and holds a reference to it. When create returns, its frame is removed and everything local to it goes too, but the object stays exactly where it was because main now holds the reference. Frames leave in the reverse order they arrived.