🧹 When Does Memory Get Cleaned Up in the Stack and Heap? (For Beginners)
If you’re just stepping into the world of .NET, C#, or memory management, you’ve likely heard these two big terms: Stack and Heap. These are not just buzzwords — they define how and when your app uses and frees memory.
Get Donie Sweeton’s stories in your inbox
Join Medium for free to get updates from this writer.
Let’s walk through what they are, how they work, and when memory gets cleaned up in each.
Stack vs Heap: The Basics
- A stack is like a vertical pile of dishes. It’s organized, fast, and cleared automatically.
- A heap is more like a big, messy drawer. You can store a lot there, but finding and cleaning things up takes more work.
Stack Memory: Automatically Cleaned Up
Whenever you call a method, the system uses stack memory to store:
- Local value-type variables like
int,bool, anddouble - References to heap objects
Once the method ends, everything that was pushed onto the stack is immediately and automatically removed. This process is called stack unwinding.
Example:
void SayHello() {
int x = 10; // Value stored in stack
string name = "John"; // Reference stored in stack, object in heap
}Here:
x = 10lives fully in the stack.name = "John"is a reference type, so:name(the pointer) is on the stack"John"(the actual string object) is on the heap
What Happens When SayHello() Ends?
- The variable
xis cleared from the stack. - The pointer
nameis also cleared from the stack. - But
"John"stays in the heap until the garbage collector decides it’s no longer needed.
Heap Memory: Cleaned by the Garbage Collector (GC)
The heap is used to store objects like:
- Strings
- Classes
- Collections (like
List<int>) - Anything you create with
new
These don’t get cleaned up automatically when a method ends. Instead, the Garbage Collector (GC) comes in and cleans them only when they’re no longer referenced anywhere in the program.
When Does GC Run?
- Memory pressure: The System is running low on memory.
- Too many allocations: Your app has created a lot of objects.
- Manual call:
GC.Collect()is called (not recommended for general use). - Background cycles: .NET runs GC periodically in the background.
A Visual Mental Model
Imagine a method like this:
void DoSomething() {
int number = 5;
Person p = new Person("Alice");
}Here’s how it maps:
STACK:
---------
| number | --> 5
| p | --> reference to heap
---------
HEAP:
---------
| Person("Alice") |
---------When DoSomething() ends:
numberandp(the pointer) are cleared from the stackPerson("Alice")stays in heap memory.- If nothing else references it, it’s eligible for GC
So What Do You, the Developer, Need to Do?
Usually nothing.
- The stack cleans itself.
- The heap is cleaned by the GC.
- But if you’re dealing with unmanaged resources (files, DB connections), then you use
Dispose()or ausingblock to help the system clean up faster.
Quick Recap
- Stack memory is freed automatically when a method ends.
- Heap memory is cleaned by the GC only when objects are no longer in use.
- Reference types store a pointer on the stack and the object on the heap.
- As a beginner, just be mindful of how long your objects live and avoid holding references unnecessarily.
👉 Follow me if you’re a dev still connecting the dots. I write to make tech simpler — because I need it too.
💬 If there’s another confusing tech topic you’d like explained, drop it in the comments. It helps me — and everyone — learn better.




