0

My application has 10 jagged arrays and 5 lists. Average ones are filled with different data for example double or string types. So I know my application has to consume a lot memory but how can I determine the total amount of memory being used?

I have read to use the GC.GetTotalMemory so the first thing I did was something like this:

var initialMemory = System.GC.GetTotalMemory(true);
many line of code
...
..
var finalMemory = System.GC.GetTotalMemory(true);
var consumption = finalMemory - initialMemory;

All the code is inside a function which main() calls his function, but the final result is zero. I saw that finalMemory is zero also, so first what is going on? the initial memory has a number but final has none.

Is System.GC.GetTotalMemory is the best option to find out the total of memory that is been used by my application?

dmck
  • 7,801
  • 7
  • 43
  • 79
JUAN
  • 523
  • 4
  • 10
  • 27
  • 1
    The GC only knows about managed memory, which is only part of the memory used by your application. – Brian Rasmussen Oct 09 '12 at 19:20
  • 1
    You could do this with WMI google search using WMI to get Total Memory – MethodMan Oct 09 '12 at 19:23
  • For profiling I would highly recommend Red Gates memory profiler: http://www.red-gate.com/products/dotnet-development/ants-memory-profiler/ – dmck Oct 09 '12 at 19:25
  • Here is a Link you can look at as well.. http://www.codeproject.com/Articles/1285/Calling-API-functions-using-C – MethodMan Oct 09 '12 at 19:34
  • i was looking up for more information and foud how to get the physical memory, the paged memory used and the virtual memory and i was wodering if this the intered memory that a application used or are there more? – JUAN Oct 09 '12 at 21:59

3 Answers3

1

I once used:
In my global.asax file:

protected void Application_BeginRequest(Object sender, EventArgs e)
{
    if (WebConfigSettings.ShowMemoryUsage)
    {
        GC.Collect();
        logger.Debug(string.Format("Total memory: {0:###,###,###,##0} bytes", GC.GetTotalMemory(true)));
    }
}
TweeZz
  • 4,779
  • 5
  • 39
  • 53
  • Just fyi, you don't need to collect before if you pass `true` to GetTotalMemory (the `true` argument implies a collect before returning). – CrazyCasta Oct 09 '12 at 20:01
0

You could do something like:

using System.Diagnostics;

...
    Process.GetCurrentProcess().VirtualMemorySize64

This will give you the number you will see in places like Task Manager. However, this includes unmanaged overhead memory usage. System.GC.GetTotalMemory(true) will give you the actual amount of memory being used by the garbage collector (.net memory usage).

CrazyCasta
  • 26,917
  • 4
  • 45
  • 72
0

Here is a Link that you can utilize as well How to do (Almost) Everything in WMI

Look at this StackOverFlow post as well.. there are many options WMI is very powerful How to Get CPU Usage

If WMI is not what you are looking and or wanting to use.. then look at Performance Counter here is an example of how to use them as well Introduction to Performance Counters

Community
  • 1
  • 1
MethodMan
  • 18,625
  • 6
  • 34
  • 52