There are a few different “levels” of memory you can check in C#, depending on what you actually mean:
• memory used by your managed objects (GC heap)
• memory used by the entire process
• system-wide memory
Here are the most useful approaches.
1. Managed memory (GC heap)
This tells you how much memory is currently allocated by the .NET runtime:
long bytes = GC.GetTotalMemory(false);
Console.WriteLine($"Managed memory: {bytes / 1024.0 / 1024.0:F2} MB");
Notes
• false → don’t force garbage collection (recommended for real measurement)
• true → forces a GC (can distort real usage)
• Only includes managed objects, not total process memory
2. Total process memory (most practical)
Use the Process class to see what your app is actually consuming:
using System.Diagnostics;
var process = Process.GetCurrentProcess();
Console.WriteLine($"Working Set: {process.WorkingSet64 / 1024 / 1024} MB");
Console.WriteLine($"Private Memory: {process.PrivateMemorySize64 / 1024 / 1024} MB");
Console.WriteLine($"Virtual Memory: {process.VirtualMemorySize64 / 1024 / 1024} MB");
Key fields
• WorkingSet64 → RAM currently in use
• PrivateMemorySize64 → memory exclusively used by your process
• VirtualMemorySize64 → total virtual address space
If you want “how much memory my app uses”, use WorkingSet64.
3. Using performance counters (advanced)
using System.Diagnostics;
var counter = new PerformanceCounter("Process", "Working Set", Process.GetCurrentProcess().ProcessName);
Console.WriteLine(counter.NextValue());
Useful for monitoring tools, but overkill for most apps.
4. Real-time monitoring (recommended during debugging)
Use built-in tools:
Visual Studio → Diagnostic Tools → Memory Usage
Task Manager → “Details” tab
dotnet-counters (CLI tool)
5. Forcing GC before measuring (sometimes useful)
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
long memory = GC.GetTotalMemory(true);
Use this only when you want a clean baseline, not during normal runtime.
Another Option: ManagementObjectSearcher
An operating system is software/firmware that makes a computer system's hardware usable, and implements and/or manages the resources, file systems, processes, user interfaces, services, ... available on the computer system. The CIM_OperatingSystem class represents a computer operating system, which is made up of software and firmware that make a computer system's hardware usable.
Here is how you can learn the total physical memory installed, current memory consumption, virtual memory usage, etc.
// Add System.Management to references
var winQuery = new ObjectQuery("SELECT * FROM CIM_OperatingSystem");
var searcher = new ManagementObjectSearcher(winQuery);
foreach (var item in searcher.Get())
{
Console.WriteLine("Free Physical Memory: {0}", item["FreePhysicalMemory"]);
Console.WriteLine("Total Physical Memory: {0}", item["TotalVisibleMemorySize"]);
Console.WriteLine("Free Virtual Memory: {0}", item["FreeVirtualMemory"]);
Console.WriteLine("Total Virtual Memory: {0}", item["TotalVirtualMemorySize"]);
}