Skip to content
English
  • There are no suggestions because the search field is empty.

[Public] How to Monitor Memory Usage of a .NET Process in Linux / WSL Console

This article shows you how to log your app’s memory consumption in real-time, right from your console application.

 

When developing or debugging .NET applications — especially resource-intensive ones like those using IronOCR, IronPDF, or heavy image processing — it's important to track memory usage. This is particularly true in constrained environments like WSL (Windows Subsystem for Linux) or Linux containers.


Prerequisites

  • .NET SDK installed (version 5.0 or later)

  • Linux environment (native or via WSL)

  • Basic .NET Console app


Sample Code: Log Memory Usage to Console

 

Add the following snippet in your application to report private memory size used by your process: 

using System;
using System.Diagnostics;

class Program
{
static void Main(string[] args)
{
// Trigger your memory-consuming logic here (e.g., OCR, PDF generation, etc.)

// Log memory usage
Process currentProcess = Process.GetCurrentProcess();
long memorySize = currentProcess.PrivateMemorySize64; // In bytes

Console.WriteLine($"Memory Usage: {memorySize / (1024 * 1024)} MB");
}
}

 

What Does PrivateMemorySize64 Mean?

  • It returns the amount of memory (in bytes) that the process has allocated exclusively and is not shared with other processes.

  • This is a good indicator of how "heavy" your application is running.


Bonus: Continuous Memory Tracking

If you're running a long-running process and want to monitor memory usage over time, wrap it in a loop:

while (true)
{
var memory = Process.GetCurrentProcess().PrivateMemorySize64;
Console.WriteLine($"Memory Usage: {memory / (1024 * 1024)} MB");
Thread.Sleep(5000); // Log every 5 seconds
}

 

Alternative: Use Linux Tools

If you'd like to monitor memory externally (outside the app), here are a few Linux-based tools you can use:

 

top or htop

 
top -p <PID>
 
Note: <PID> stands for Process ID — a unique number assigned by the operating system to each running process.
 
ps
 
ps -o pid,vsz,rss,comm -p <PID>
  • VSZ = virtual memory size (in KB)

  • RSS = resident set size (physical memory used)

smem (more detailed)

 
sudo apt install smem
smem -p <PID>

Example Output

Memory Usage: 214 MB

 

You can write this to a log file, pipe it to another process, or use it to trigger alerts if usage exceeds thresholds.

 


Best Practices

  • Run memory tracking in test environments before deploying to production.

  • For long-running services or APIs, consider integrating metrics collection (e.g., Prometheus, Grafana).

  • Use GC.Collect() only for debugging, not in production.


Summary

Tracking memory usage in a Linux/WSL .NET app is simple and essential for:

  • Troubleshooting memory leaks

  • Debugging out-of-memory crashes

  • Benchmarking performance

With just a few lines of code, you can get clear visibility into how your app behaves under load.