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

I am trying to play with memory profiling (for the first time, so please excuse my ignorance) just to see how much memory is being consumed by classes, objects, variables, methods, etc. I wrote this example C # console program 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 starts, I want to know:

How much memory was consumed

  • MemPlay.exe
  • Program class
  • SomeClass
  • The main method
  • MyObject
  • StringNineMethod
  • someString0
  • someString1
  • someString2

and how much processor was used:

  • MemPlay.exe
  • The main method
  • StringNineMethod

I tried using the VisualStudio Performance Diagnostics tool, but all I can see is how much memory the entire function was using (ie Main method, StringNineMethod and String.Concat method).

Is there a way / tool that can help me see all the details of how much memory each variable, object, class, method is consuming? Thanks in advance.

EDIT: No, my question does not duplicate the suggested question, since this question is trying to get the dimensions of an object at runtime, I ask how can I get this information after the program ends. What the Visual Studio Performance Diagnostic Tool does is give this information after the program finishes running.

+3


source to share


2 answers


I am using this one: RedGate ANTS

I've also used this in the past: SciTech Memory Profiler



They both have free challenges and are worth watching. I was impressed enough at various times to buy versions of both.

(I don't work for any company, just recommend what worked for me - there are other tools like JetBrains Memory Profiler , but I haven't tried this personally and can't offer my opinion).

+2


source


You can use namespace classes System.Diagnostics

to get different kinds of measurements and statistics. To get the total memory allocated for a process use the property WorkingSet

(more on MSDN ):

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

      

So this process.

To get a specific allocation of objects, you can probably use GC

to check the original memory, then allocate the object, and finally check the 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 a specific process after a specific operation, you can probably use the same trick as for GC, only in the current process (like in the first example).

Use the Visual Studio Profiler to check executables and programs. In VS2013 Community Edition, go to ANALYZE → Performance and Diagnostics (or press Alt+ F2). It allows you to parse a standard project, exe, ASP.NET website and Windows Phone application:

enter image description here

In this section, you select "Performance Wizard", click "Start", and in the next step, you will have a choice of metrics that you would like to run. One of them is memory consumption:

enter image description here

+1


source







All Articles