43

Possible Duplicate:
How to get memory available or used in C#

I want to visualize the memory which is used by my application in the statusbar of my application. I am searching for a memoryleak - but I don't know where. Now, my idea is to visualize the used memory in the statusbar so that I can see it while I am working with the application and find the part, where the problem occurs and then I can profiling this.

Can someone give me some help, how can I get the used memory.

0

4 Answers 4

52

You can use the following function (The true parameter tells the GC to perform a collection first):

long memory = GC.GetTotalMemory(true);
1
  • 6
    As mentioned in the duplicate, beware: this solution only looks at the managed heap. To get the full process size see: stackoverflow.com/a/750595/331281
    – Dejan
    Commented Sep 27, 2017 at 15:02
25

You can try GC.GetTotalMemory:

It Retrieves the number of bytes currently thought to be allocated. A parameter indicates whether this method can wait a short interval > before returning, to allow the system to collect garbage and finalize > objects.


or

using System.Diagnostics;

Process currentProc = Process.GetCurrentProcess();

Once you have a reference to the current process, you can determine its memory usage by reading the PrivateMemorySize64 property.

long memoryUsed = currentProc.PrivateMemorySize64;
1
  • 4
    are they the same?
    – mcmillab
    Commented Jun 7, 2017 at 1:42
5

If you want to monitor the memory used by your application, you do not need to write code for that. Just use the performance counters from Windows (http://www.codeproject.com/Articles/8590/An-Introduction-To-Performance-Counters). They'll provide the information you need with charts! There are lots of memory counters (http://msdn.microsoft.com/en-us/library/x2tyfybc.aspx), one of them will have the data you need and I guess it'll help you to find out when your app starts to use more memory than it should.

However, if you do need to put this information in your application, you can still use performance counters. .NET has classes in System.Diagnostics namespace to access their data. See this example: https://weblog.west-wind.com/posts/2014/Sep/27/Capturing-Performance-Counter-Data-for-a-Process-by-Process-Id

3
  • Geekpedia link is not available anymore Commented Apr 7, 2019 at 18:04
  • 1
    I changed the example, @MichaelFreidgeim, thanks
    – Fabio
    Commented May 5, 2019 at 18:16
  • 1
    To do this, I will need access to client's computer which I don't. I need a way to collect this data and send back to server in log.txt form
    – AaA
    Commented Dec 16, 2019 at 3:08
-6

Why not just monitor the memory usage with the TaskManager or with more advanced tools from Sysinternals and only after you determine what workflow raises the memory usage abnormally then profile the application with CLR Profiler or others.

Not the answer you're looking for? Browse other questions tagged or ask your own question.