Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Poll C# app's memory usage at runtime?

I have an app that, while running, needs to poll its own memory usage. It would be ideal if it could list out the memory usage for each object instantiated. I know this can be achieved by WMI, but I was hoping for something that doesn't rely on WMI.

like image 397
maxfridbe Avatar asked Jan 20 '09 23:01

maxfridbe


People also ask

What is poll () in C?

The poll() function provides applications with a mechanism for multiplexing input/output over a set of file descriptors. For each member of the array pointed to by fds, poll() shall examine the given file descriptor for the event(s) specified in events.

What is poll () use for?

The poll() function is used to enable an application to multiplex I/O over a set of descriptors. For each member of the array pointed to by fds, poll() will examine the given descriptor for the event(s) specified. nfds is the number of pollfd structures in the fds array.

How does poll work in Linux?

When poll() is called for some file descriptor, the corresponding device poll_xyx() method registered with file operation structure is invoked in kernel space. This method then checks if the data is readily available, if this condition is true then the event mask is set and the poll returns to user space.

Is poll a system call?

The poll() system call was introduced in Linux 2.1. 23. On older kernels that lack this system call, the glibc poll() wrapper function provides emulation using select(2). The ppoll() system call was added to Linux in kernel 2.6.


4 Answers

Two functions you might find useful are:

  GC.GetTotalMemory();
  Process.PagedMemorySize64();

My experience has been that GC.GetTotalMemory() is not terribly reliable. It often reports memory usage that is much smaller than the actual memory usage. I've seen it report that I'm using only 8 gigabytes when my program runs out of memory on a 16 gigabyte machine.

I haven't yet tested Process.PagedMemorySize64, although it does look promising.

like image 134
Jim Mischel Avatar answered Oct 31 '22 19:10

Jim Mischel


Process proc = Process.GetCurrentProcess();
Logger.Info(proc.PeakWorkingSet64/1024 + "kb");
like image 32
user318554 Avatar answered Oct 31 '22 19:10

user318554


You could listen on perfmon counters, which will give you plenty of data (GC activity / physical memory usage / managed heap etc..)

If you need to go deeper you will probably have to attach a debugger to yourself, which is really super tricky cause you will have to spawn a new process and communicate with it, and walk through your memory.

like image 2
Sam Saffron Avatar answered Oct 31 '22 21:10

Sam Saffron


You can get some coarse granularity about your process from System.Diagnostics, the Process class. http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx.

None of the 'per object' stuff, but at least some memory info about your process can be gleaned.

like image 2
Joe Avatar answered Oct 31 '22 20:10

Joe