Using "Process" Class to Show Memory Usages

This section provides a tutorial example on how to retrieve memory usage statistics of your application using methods and properties of the System.Diagnostics.Process class.

If you want to know memory usage statistics of your C# application, you can use these methods and properties from System.Diagnostics.Process class provided in .NET 4:

Here is my tutorial example on using these memory related methods and properties:

// MemoryUsage.cs
// Copyright (c) 2010 HerongYang.com. All Rights Reserved.

using System;
public class MemoryUsage {
   public static void Main() {
      System.Diagnostics.Process proc
         = System.Diagnostics.Process.GetCurrentProcess();
      Console.WriteLine("Current process: {0}", proc.ToString());

      Console.WriteLine("Private Memory: {0}",
         proc.PrivateMemorySize64);
      Console.WriteLine("Virtual Memory: {0}",
         proc.VirtualMemorySize64);
      Console.WriteLine("Working Set: {0}",
         proc.WorkingSet64);
      Console.WriteLine("Paged Memory: {0}",
         proc.PagedMemorySize64);
      Console.WriteLine("Paged System Memory: {0}",
         proc.PagedSystemMemorySize64);
      Console.WriteLine("Non-paged System Memory: {0}",
         proc.NonpagedSystemMemorySize64);
   }
}

I compiled it with .NET 4 C# compiler and got this output:

C:\herong>\Windows\Microsoft.NET\Framework\v4.0.30319\csc
   MemoryUsage.cs

C:\herong>MemoryUsage.exe
Current process: System.Diagnostics.Process (MemoryUsage)
Private Memory: 8744960
Virtual Memory: 89157632
Working Set: 5599232
Paged Memory: 8744960
Paged System Memory: 100228
Non-paged System Memory: 3988

See next section for explanations of those memory numbers.

Table of Contents

 About This Book

 Introduction of C# (C Sharp)

 Data Type and Variables

 Logical Expressions and Conditional Statements

 Arrays and Loop Statements

 Data Type Features

 Floating-Point Data Types

 Passing Parameters to Methods

 Execution Environment Class

 Visual C# 2010 Express Edition

 Class Features

 C# Compiler and Intermediate Language

 Compiling C# Source Code Files

 MSBuild - Microsoft Build Engine

Memory Usages of Processes

Using "Process" Class to Show Memory Usages

 Private/Virtual Memory and Working Set

 Footprints of Private and Shared Memories

 Virtual Memory Upper Limit

 Memory Report from Windows Task Manager

 Memory Report from Performance Console

 Multithreading in C#

 Async Feature from C# 5

 System.IO.FileInfo Class

 System.Diagnostics.FileVersionInfo Class

 WPF - Windows Presentation Foundation

 Partial Classes and Partial Methods

 Archived Tutorials

 References

 Full Version in PDF/ePUB