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.
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.
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.
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.
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.
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.
Process proc = Process.GetCurrentProcess();
Logger.Info(proc.PeakWorkingSet64/1024 + "kb");
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.
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.
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