Memory Management
How your program handles memory (Stack vs. Heap).
Memory Management
Analogy: concept
Memory is like a Restaurant:
- Stack: The waiter's notepad. Fast, temporary, organized (function calls).
- Heap: The kitchen storage. Large, messy, needs cleaning (objects).
- Garbage Collector: The busboy who clears tables when customers leave.
The Garbage Collector
Visualize how objects are stored in the Heap and cleaned up when no longer needed.
Memory Management (Garbage Collector)
HEAP MEMORY
Heap is empty. Create objects!
1. Click + New Object to allocate memory.
2. Click an object to remove its reference (simulate variable going out of scope).
3. Click Run GC to clean up unreferenced objects.
Key Concepts
-
Stack Memory:
- Stores primitive variables (
int,boolean) and references to objects. - Automatically cleared when a function ends.
- Very fast access.
- Stores primitive variables (
-
Heap Memory:
- Stores actual objects (
new Car(),new ArrayList()). - Objects live here until they are "collected".
- Slower access than Stack.
- Stores actual objects (
-
Garbage Collection (GC):
- A background process that looks for "unreferenced" objects.
- If no variable points to an object, it's considered "garbage" and deleted to free up memory.
The Code
void createObject() {
// "c" is a reference on the Stack
// "new Car()" creates an object on the Heap
Car c = new Car();
// ... function ends ...
// "c" is popped off the Stack.
// The Car object on the Heap is now unreferenced.
// GC will eventually delete it.
}
Up Next
Concurrency