1

To let me explain why I need to know, in my server launcher (for a game) someone requested that I added a 'Restart on Excessive Memory Usage' checkbox. So, after an hour or so of adventuring, I came here.

I just need to know how to read the memory usage of a running application and I can do the rest. I just have no idea how to do this withing a c# application.

Thank you for the help!

  • 1
    Look at this: http://stackoverflow.com/questions/755919/memory-usage-in-c-sharp Just change the "current process" to "another process". – Styxxy Oct 28 '13 at 23:39
  • When I do that, I get the following error. `Cannot implicitly convert type 'System.Diagnostics.Process[]' to 'System.Diagnostics.Process'` – Wesley Bellah Oct 29 '13 at 00:22
  • That means that whatever function call you did returned an array of processes instead of a single process like you tried to do. You must look at the array returned and select the correct element (if there are any elements in it at all) – Scott Chamberlain Oct 29 '13 at 00:35
  • @ScottChamberlain How exactly would I do that? Haha, I'm a bit new at this sorta thing. – Wesley Bellah Oct 29 '13 at 00:53

2 Answers2

8

"Memory" is quite an inaccurate term on a demand-paged virtual memory operating system like Windows. You need to pick one or more of these Process class properties:

  • NonpagedSystemMemorySize64: a high value indicates that a device drivers have allocated lots of memory in the non-paged kernel memory pool. Such memory is used for critical device driver operations that need to execute when devices generate an interrupt. Not a very good measure unless you rely on a buggy driver. Killing the process isn't typically going to help.

  • PagedSystemMemorySize64: another driver resource, allocated from the paged kernel memory pool. Typically used for I/O buffers. Not typically a very good measure, getting a high value just indicates that your program is busy. You however ought to correlate that with a steadily increasing HandleCount property value. If that just keeps going up then you have a handle leak in the program that does merit killing it.

  • VirtualMemorySize64: the total amount of virtual memory allocated for the process. This covers both code and data used by the program, an OutOfMemoryException is raised when you run out of a hole big enough in the VM address space. A pretty good measure for general leakage.

  • WorkingSet64: the total amount of virtual memory actually mapped to RAM. This number can heavily fluctuate as the operating system unmaps virtual memory pages to make room for other processes. Not a good measure, you're likely to kill the process just because it got busy. And a leaky program doesn't necessary have a large working set, many pages can be swapped out and not get swapped back in because a program doesn't typically reference leaked objects anymore.

  • PrivateMemorySize64: the amount of virtual memory that's not shared with other processes, otherwise a better measure than VirtualMemorySize64 since it is representative for how many resources are allocated for just your process. In a .NET app that will be data as well as code that wasn't ngen-ed.

  • PagedMemorySize64: the amount of virtual memory that's backed by the paging file. A good measure if you are leaking data, the normal problem in a leaky .NET app.

Some odds that when you check these numbers for your program to find out which one is best that you'll also have a good lead on the bug in your code. A memory profiler can help a lot. Fixing that bug is of course the far better solution to your problem.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0

Use the Process object to get everything about a running process. To get information about a running process you can call static method Process.GetProcessByName. That should give you details including memory use (this call returns array of all processes that match your name). To get the memory currently being used by the process use Process.WorkingSet64 property. Links referenced in MSDN gives you enough example to work with Process object.

loopedcode
  • 4,863
  • 1
  • 21
  • 21