I'm trying to measure, in a C# programm, the memory usage.
I'd like to know the C# equivalent of this Java function :
ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getCommitted()
that represent the total memory allocated for the heap. I need this to know the total size of memory allocated for my C# programm.
Then in Java I can get the used memory with :
ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed()
Currently I use this in C# to get used memory :
Process_Memory = MyProcess.PrivateMemorySize64;
But I'm not totally sure that it's the equivalent.
So in resume how can I get the total allocated space for my C# application and the current use at a time t?
EDIT :
From the answers and further research I've determine this :
Current memory in use
System.GC.GetTotalMemory(false);
Give the number of bytes currently allocated in managed memory. (http://msdn.microsoft.com/fr-fr/library/system.gc.gettotalmemory.aspx)
This method return a low value and I'm pretty sure it's not really a representation of all memory in use as I can get with getUsed()
in Java. In java for the same application used memory start at 5MB and go to 125MB max. With C# I get from 1 to 5 MB with above method.
It seems that :
MyProcess.PrivateMemorySize64; // return ~=25MB
or
MyProcess.WorkingSet64; //return ~=20MB
Give a more accurate value of all the memory in use. But I don't know wich one I should use...
For the global allocated memory this article suggests to use :
Process_MemoryEnd1 = MyProcess.VirtualMemorySize64;
It return always the same value along the programm, about 169MB that seems fair compared to Java : from 64MB to 170MB max
I'm still looking for an accurate answer all I found is very vague and I'm not very familiar with Windows memory management, I'm not really sure to undertand the docs I found :/
Check Computer Memory Usage EasilyTo open up Resource Monitor, press Windows Key + R and type resmon into the search box. Resource Monitor will tell you exactly how much RAM is being used, what is using it, and allow you to sort the list of apps using it by several different categories.
Linux glibc sysconf(_SC_AVPHYS_PAGES) and get_avphys_pages() These two glibc extensions should give you the available number of pages. We can then just multiply that by the pages size sysconf(_SC_PAGESIZE) to find the total available memory in bytes.
Run the program in one shell. Open another shell and run 'top' command. it will list running processes and home much memory they consume. you can, i guess, poll /proc/yourprocessid/stat to see how much memory it is using over time.
The ps command can also be used to monitor memory usage of individual processes. The ps v PID command provides the most comprehensive report on memory-related statistics for an individual process, such as: Page faults. Size of working segment that has been touched.
Heres one way, using the GC:
public void Test()
{
long kbAtExecution = GC.GetTotalMemory(false) / 1024;
// do stuff that uses memory here
long kbAfter1 = GC.GetTotalMemory(false) / 1024;
long kbAfter2 = GC.GetTotalMemory(true) / 1024;
Console.WriteLine(kbAtExecution + " Started with this kb.");
Console.WriteLine(kbAfter1 + " After the test.");
Console.WriteLine(kbAfter1 - kbAtExecution + " Amt. Added.");
Console.WriteLine(kbAfter2 + " Amt. After Collection");
Console.WriteLine(kbAfter2 - kbAfter1 + " Amt. Collected by GC.");
}
Or alternatively using System.Diagnostics.PerformanceCounter to get working set info:
PerformanceCounter performanceCounter = new PerformanceCounter();
performanceCounter.CategoryName = "Process";
performanceCounter.CounterName = "Working Set";
performanceCounter.InstanceName = Process.GetCurrentProcess().ProcessName;
Console.WriteLine(((uint)performanceCounter.NextValue()/1024).ToString("N0"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With