Click here to Skip to main content
14,272,402 members
Rate this:
Please Sign up or sign in to vote.
See more:
I try to measure memory usage of my application in c# using this using System.Diagnostics.Process

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

but i didn;t get any results. if you have any ideas please let me know.

Thanks alot

What I have tried:

Process currentProcess = System.Diagnostics.Process.GetCurrentProcess();
long memoryUsage= currentProcess.WorkingSet64;
Updated 11-May-16 23:52pm
Richard Deeming 9-May-16 13:42pm
What do you mean by "didn't get any results"?
Member 12127665 11-May-16 8:37am
I try to print the value of memoryUsage but no value is printed!!
Rate this:
Please Sign up or sign in to vote.

Solution 4

I would like to propose my vision that may differ from purpose of your code. But it could introduce some new points that could help to find the best solution.

My vision is that process is an OS part. So the one question is how much memory OS allocated for the process. OS calls or external tools could be helpful here.

In other hand memory consumption in systems with automatic memory management (Garbage Collection) is depends on many factors. In one case application can produce much not handled objects and GC do not release them. In other case GC can release memory aggressively. So GC should be taken into consideration as active part.

Besides all GC runs its own processes. So if you want to assess whole application memory consumption, the better considering measuring memory in more wide context, but not for one process.

I use GC information to assess memory consumption - memory available and number of collecting actions. It is some rough evaluation but could be useful for some cases. My codes (for example / also platform independent):
memory = GC.GetTotalMemory(true);

int[] gccounts = new int[GC.MaxGeneration + 1];
for (int i = 0; i <= GC.MaxGeneration; i++)
    gccounts[i] = GC.CollectionCount(i);
Rate this:
Please Sign up or sign in to vote.

Solution 1

I use this which returns the value from the task manager :
private string GetMemoryUsage() // KLUDGE but works
        string fname = Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location);

        ProcessStartInfo ps = new ProcessStartInfo("tasklist");
        ps.Arguments = "/fi \"IMAGENAME eq " + fname + ".*\" /FO CSV /NH";
        ps.RedirectStandardOutput = true;
        ps.CreateNoWindow = true;
        ps.UseShellExecute = false;
        var p = Process.Start(ps);
        if (p.WaitForExit(1000))
            var s = p.StandardOutput.ReadToEnd().Split('\"');
            return s[9].Replace("\"", "");
    catch { }
    return "Unable to get memory usage";
Dave Kreskowiak 11-May-16 8:33am
The problem with using Task Manager (or the values it uses) is that you're not getting a value that's accurate. You're getting the amount of memory RESERVED by the .NET CLR for your application, NOT the amount of memory your application is actually using.

For that, you need to use the .NET Memory performance counters.
Mehdi Gholam 11-May-16 8:37am
Yes [the CLR reserves a lot of memory which may be free and not in use which it doesn't give back to the OS] but the overall outcome is that memory is out of the OS's hands and "used" by the app.
Dave Kreskowiak 11-May-16 8:42am
That's where you're wrong. The memory is reserved but NOT unavailable to Windows. If Windows is running low on resources, the .NET CLR will happily deallocate unused memory in its heaps and return it back to Windows.
Member 12127665 11-May-16 8:46am
so how can i get the value of memory usage of my current running c# application? if you know please let me know
Dave Kreskowiak 11-May-16 10:04am
First, why would your application even care?

Start reading these[^]. This isn't as simple as you would want.
Rate this:
Please Sign up or sign in to vote.

Solution 3

I use a third party tool: ReSharper dotMemory. See dotMemory: Memory profiler and unit-testing framework for .NET[^] on their website.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100