How to find out memory consumed by classes, objects, variables, etc

M. Ali Iftikhar picture M. Ali Iftikhar · Jun 13, 2015 · Viewed 7k times · Source

I am trying to play with memory profiling (for the first time, so please forgive my ignorance), just to see how much memory is consumed by classes, objects, variables, methods etc. I wrote this sample c# console program called MemPlay.exe:

using System;
using System.Text;

namespace MemPlay 
{
   class Program 
   {
      static void Main(string[] args) 
      {
         SomeClass myObject = new SomeClass();

         StringNineMethod();
      }

      private static void StringNineMethod() 
      {         
         string someString0 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

         string someString1 = string.Empty;
         string someString2 = string.Empty;

         for (int i = 0; i < 9999; i++) {
            someString1 += "9";
            someString2 += someString1;
         }
      }
   }   

   class SomeClass 
   {

   }
}

Once the program ran, I want to find out:

How much memory was consumed by

  • MemPlay.exe
  • Program class
  • SomeClass
  • Main method
  • myObject
  • StringNineMethod
  • someString0
  • someString1
  • someString2

and how much processor was used by:

  • MemPlay.exe
  • Main method
  • StringNineMethod

I tried using VisualStudio's 'Performance Diagnostics' tool, but all I can see is how much memory was used by the whole function (i.e. Main method, StringNineMethod, and the String.Concat method).

Is there any way/tool which can help me see all the details of how much memory each variable, object, class, method consumed? Thanks in advance.

EDIT: No my question is not duplicate of the question suggested, as that question is trying to get object sizes at runtime, I am asking how can I get this information after the program has ended. Just what Visual Studio's Performance Diagnostics tool does, it gives this information after the program has ended execution.

Answer

PiotrWolkowski picture PiotrWolkowski · Jun 14, 2015

You can use System.Diagnostics namespace classes to get different kind of measurements and statistics. To get total memory allocated for the process use WorkingSet property (more details on MSDN):

Process currentProcess = System.Diagnostics.Process.GetCurrentProcess();
long processAllocatedMemory = currentProcess.WorkingSet64;

So that's process.

To get specific object allocation you probably can use GC to check initial memory, then allocate an object and finally to check memory again:

// Check initial memory
var memoryStart = System.GC.GetTotalMemory(true);

// Allocate an object.
var myClass = new SomeClass;

// Check memory after allocation
var memoryEnd = System.GC.GetTotalMemory(true);

To check memory consumption on specific process after specific operation you probably can use the same trick as with the GC only on the current process (like in the first example).

To check executables and programs use Visual Studio profiler. In VS2013 Community Edition go to ANALYZE -> Performance and Diagnostics menu (or hit Alt+F2). It allows you to analyze standard project, an exe, an ASP.NET website, and Windows Phone App:

enter image description here

There, you select Performance Wizard, click Start, and in the next step you have a choice of metrics you would like to run. One of which is memory consumption:

enter image description here