#666 – Looking at .NET Memory Usage Using Performance Counters

As you create new objects in .NET, the garbage collector allocates memory for the objects on the heap.  As memory is allocated, the process consumes some of its available virtual memory.  (In practice, a 32-bit process can allocate at most around 1.5GB of its 2GB maximum).

You can keep track of (roughly) how much memory on the managed heap that your application has allocated by using performance counters.

To track memory usage:

  • Start Performance Monitor

 

  • Start your application
  • Click the “+” icon to add a counter

  • Expand the .NET CLR Memory section

  • Select the # Total Reserved Bytes counter and select your application

  • Click the Add button and then click OK

You’ll now see a graph indicating total number of bytes of memory that your application has reserved.  The Last field will show you the most recent value.

Advertisement

#665 – Maximum Amount of Virtual Memory

In 32-bit versions of Windows, a process has access to at most 4GB of virtual memory.  Of this, an application can access at most 2GB.  The remaining 2GB is reserved for the operating system.

The 4GB limit comes from the fact that 32-bit words are used as addresses into memory, resulting in a maximum of 4,294,967,296 bytes–or 4GB.

In 64-bit versions of Windows (x64), we get a 64-bit address space, resulting in 18,446,744,073,709,551,616 possible memory locations, or 16 exabytes (equivalent to 16,777,216 terabytes, or 17,179,869,184 GB).  An application running on 64-bit Windows, however, only gets access to a tiny fraction of the available total.  Each application can address a maximum of 8TB (or 8,192GB, which is still 4,096 times what a 32-bit app has access to).

#664 – Physical Memory vs. Virtual Memory

When writing software, you’ll often come across the terms physical memory and virtual memory.  Here’s a quick explanation.

Physical memory refers to the actual random-access memory (RAM) installed in your computer.  On newer desktops and laptops, this is typically from 4-16GB.  RAM is a place to store data.  It allows the operating system and programs running on it to read and write data while the computer is running.

Virtual memory is an area of memory that an application can read from and write to which behaves like a chunk of physical memory.  The application can freely read from and write to this memory without worrying about affecting other applications.  This chunk of virtual memory may be smaller or larger than the actual physical memory on the machine.