Memory Management Basics
Vaibhav • September 11, 2025
Every program you write in C# uses memory. Whether you're storing numbers, text, or more complex data, memory is where your program keeps track of everything it needs while running. But not all memory is the same. Some values are stored in one part of memory, while others go somewhere else. And when your program is done with a value, something needs to clean it up. That’s where memory management comes in.
In this article, we’ll explore how memory works in C# at a beginner-friendly level. We’ll introduce the idea of the stack and the heap, explain the difference between value types and reference types, and give you a first look at garbage collection. You don’t need to understand advanced programming to follow along - we’ll keep things simple and conceptual.
What is memory?
When your program runs, it needs a place to store information. This place is called memory. Think of memory like a big table with lots of drawers. Each drawer can hold a value - like a number, a piece of text, or something more complex. When you declare a variable in C#, you’re asking the program to reserve a drawer for that value.
But not all drawers are the same. Some are very fast and small. Others are bigger and more flexible. In C#, these two areas are called the stack and the heap.
The stack - fast and simple
The stack is a part of memory used for simple, short-lived values. When you declare a variable like int age = 25;
, it goes on the stack. The stack is very fast because it works like
a stack of plates - the last thing added is the first thing removed.
int x = 10;
int y = 20;
int z = x + y;
In this example, x
, y
, and z
are stored on the stack. They are simple numbers, and they disappear
automatically when your program finishes using them. You don’t need to clean them up - C# handles that for you.
The stack is great for values that are small and don’t need to change much. It’s fast, but limited in size and flexibility.
The heap - flexible and long-lived
The heap is another part of memory used for more complex or long-lived data. If you create something that needs to stay around for a while - like a string or a list - it usually goes on the heap.
While we haven’t introduced classes yet, you’ve already seen strings:
string name = "Vaibhav";
In this example, the variable name
is stored on the stack, but the actual text
"Vaibhav"
is stored on the heap. The stack holds a reference - like a pointer -
to the heap location where the string lives.
The heap is slower than the stack, but it’s much more flexible. It can store large amounts of data and keep it around as long as needed. But because it’s more complex, C# needs a way to manage it carefully.
The stack stores actual values for simple types. The heap stores objects and complex data, and the stack holds a reference to them.
Value types vs reference types
In C#, every type is either a value type or a reference type. This affects how the type is stored in memory and how it behaves when you assign or copy it.
Value types include types like int
, bool
, char
, and double
. When you assign a value type to a variable, the actual value is copied.
int a = 5;
int b = a;
b = 10;
Console.WriteLine(a); // Output: 5
Console.WriteLine(b); // Output: 10
In this example, a
and b
are separate copies.
Changing b
does not affect a
. This is how
value types behave - each variable holds its own copy.
Reference types include types like string
and other objects
(which we’ll learn about later). When you assign a reference type, you’re copying the reference - not the actual
data.
string first = "Hello";
string second = first;
second = "Hi";
Console.WriteLine(first); // Output: Hello
Console.WriteLine(second); // Output: Hi
In this case, first
and second
originally
pointed to the same string. But when we changed second
, it pointed to a new
string. Strings are special - they behave like value types in some ways - but most reference types will share
the same data unless you create a new copy.
Value types are stored directly on the stack. Reference types are stored on the heap, and the stack holds a reference to them.
Why does this matter?
Understanding stack vs heap and value vs reference types helps you write better programs. It explains why some variables behave independently and others seem to “share” data. It also helps you avoid mistakes - like accidentally changing something you didn’t mean to.
As you learn more about classes, arrays, and collections, this knowledge will become even more important. But even now, it helps you understand how your variables live and behave in memory.
Garbage collection - automatic cleanup
When your program creates data on the heap, it needs to be cleaned up eventually. You don’t want old data sitting around forever, taking up space. In some languages, you have to clean it up yourself. But in C#, there’s a system called garbage collection that does it for you.
Garbage collection is like a smart janitor. It watches your program and looks for data that’s no longer being used. When it finds something that’s no longer needed, it removes it from memory. This keeps your program efficient and prevents memory leaks.
You don’t need to do anything special to use garbage collection. It runs automatically in the background. But it’s good to know it exists - especially when working with reference types and heap memory.
Garbage collection doesn’t run constantly. It runs when needed - like when memory is low or when your program creates a lot of new data. It’s designed to be efficient and invisible.
Memory in simple programs
Let’s look at a simple program and think about how memory is used:
int score = 100;
string player = "Vaibhav";
Console.WriteLine($"Player: {player}, Score: {score}");
In this example:
score
is a value type stored on the stack.player
is a reference type. The variable is on the stack, but the actual text"Vaibhav"
is on the heap.
When the program ends, score
disappears automatically. The string "Vaibhav"
is cleaned up by garbage collection if it’s no longer needed.
Common mistakes and how to avoid them
One common mistake is assuming that all variables behave the same. For example, you might expect two reference variables to be independent - but they might point to the same data.
Another mistake is worrying too much about memory cleanup. In C#, you don’t need to delete variables manually. The garbage collector handles it for you. Just focus on writing clear code, and C# will take care of the rest.
Use value types for simple data like numbers and booleans. Use reference types for more complex data like strings and objects. Don’t worry about memory cleanup - C# handles it automatically.
Summary
Memory management is how your program stores and cleans up data. In C#, memory is divided into the stack and the
heap. The stack is fast and simple, used for value types like int
and bool
. The heap is flexible and used for reference types like string
. C# uses garbage collection to clean up unused data on the heap, so you
don’t have to do it manually.
You learned how value types are copied, how reference types point to shared data, and how memory behaves in simple programs. These concepts will help you write better code and understand what’s happening behind the scenes. As you continue learning C#, you’ll build on this foundation to work with classes, collections, and more advanced memory techniques.