IRONSOFTWAREHOME

Monitor .NET Process Memory in Linux/WSL

Curtis Chau
Curtis Chau
Updated: June 24, 2026

Resource-heavy .NET work, such as OCR, PDF generation, or image processing, can be hard to size up in constrained environments like WSL (Windows Subsystem for Linux) or Linux containers. You can log a process's memory consumption in real time directly from a console app, or watch it externally with standard Linux tools.

Prerequisites

  • .NET SDK installed, version 5.0 or later.
  • A Linux environment, either native or via WSL.
  • A basic .NET console app.

Solution

1. Log memory usage to the console

Read the process's private memory size and print it in megabytes:

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");
}
}
C#

PrivateMemorySize64 returns the amount of memory, in bytes, that the process has allocated exclusively and does not share with other processes. It's a solid indicator of how heavy your application is running.

2. Track memory continuously

For a long-running process, wrap the read in a loop to sample usage over time:

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

The Thread.Sleep(5000) call paces the logging to one reading every five seconds. Pipe the output to a file or another process, or use it to fire alerts when usage crosses a threshold.

Console Output

Memory Usage: 214 MB
Text

Option: Monitor externally with Linux tools

To watch a process from outside the app, use one of the standard utilities. <PID> is the Process ID, the unique number the operating system assigns to each running process.

top or htop: watch a single process live.

top -p <PID>
SHELL

ps: print a one-shot snapshot of memory fields.

ps -o pid,vsz,rss,comm -p <PID>
SHELL

Here VSZ is the virtual memory size in KB and RSS is the resident set size, the physical memory in use.

smem: install it for a more detailed per-process breakdown.

sudo apt install smem
smem -p <PID>
SHELL

Best Practices

  • Run memory tracking in test environments before deploying to production.
  • For long-running services or APIs, integrate a metrics stack such as Prometheus or Grafana.
  • Reserve GC.Collect() for debugging only.
Warning: Do not call GC.Collect() in production code; use it only while debugging.
Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...
Read More

Ready to Get Started?

Nuget Downloads 21,105,021Version:2026.9just released

Get your free 30-day Trial Key instantly.
No credit card or account creation required
C# NuGet Library for PDF
Install with NuGet

Version: 2026.9

PM > Install-Package IronPdf
nuget.org/packages/IronPdf/
  1. In Solution Explorer, right-click References, Manage NuGet Packages
  2. Select Browse and search "IronPdf"
  3. Select the package and install
C# PDF DLL
Download DLL

Version: 2026.9

or download Windows Installer here.

  1. Download and unzip IronPDF to a location such as ~/Libs within your Solution directory
  2. In Visual Studio Solution Explorer, right click References. Select Browse, "IronPdf.dll"

Licenses from $999

Key in blue circle

Get your free 30-day Trial Key instantly.

Your trial license will be sent to your email address

No limitations. 100% unlocked. No credit card.

OR
bullet_checkedNo credit card or account creation requiredNo limitations. 100% unlocked. No credit card.
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried Iron Suite
Book your free Live Demo
Booking Badge

Trusted by Millions of Engineers Worldwide

Iron Software's customer logos
Get Your No-Obligation Consult
Complete the form below or email sales@ironsoftware.com
Your details will always be kept confidential.
Trusted by Millions of Engineers Worldwide
Iron Software's customer logos
Get your free 30-day Trial Key instantly.
No credit card or account creation required
C# NuGet Library for PDF
Install with NuGet

Version: 2026.9

PM > Install-Package IronPdf
nuget.org/packages/IronPdf/
  1. In Solution Explorer, right-click References, Manage NuGet Packages
  2. Select Browse and search "IronPdf"
  3. Select the package and install
C# PDF DLL
Download DLL

Version: 2026.9

or download Windows Installer here.

  1. Download and unzip IronPDF to a location such as ~/Libs within your Solution directory
  2. In Visual Studio Solution Explorer, right click References. Select Browse, "IronPdf.dll"

Licenses from $999