dtaylor
dtaylor

Reputation: 1007

How to monitor memory alocation from code

I have a windows service that is eventually throwing an "Out of Memory" exception. It is written in C# and running on Windows 7.

Yes, I have read existing questions about this in Stack Overflow as well as other places on the internet. If fact, I found a great article by Eric Lippert called "Out Of Memory" Does Not Refer to Physical Memory, where he provides a very clear explanation of this condition.

http://blogs.msdn.com/b/ericlippert/archive/2009/06/08/out-of-memory-does-not-refer-to-physical-memory.aspx?PageIndex=1#comments

In this article he refers to "out of memory" with the statement: "Which is a misnomer, these days. It really should be an “unable to find enough contiguous address space” error; there’s plenty of memory because memory equals disk space".

He also states:

An “out of memory” error almost never happens because there’s not enough storage available; as we’ve seen, storage is disk space, and disks are huge these days. Rather, an “out of memory” error happens because the process is unable to find a large enough section of contiguous unused pages in its virtual address space to do the requested mapping.

When I look at the service within PerfMon, I see columns under Memory for Commit, Working Set, and Private all of which are continually growing. I'm sure there is some string that is continually being added to or some list that is not being cleared out.

My question is this, Is there some technology in C# that I can use to monitor memory requests for individual objects or collections from within the code itself such as a library of classes that could monitor the system? If so, I could keep a watch on contiguous memory requests from within the code itself.

Upvotes: 5

Views: 176

Answers (1)

Seabizkit
Seabizkit

Reputation: 2415

Use c# code to tell you who much the windows service is consuming.

private float GetActuratePrivateWorkingSet(Process currentProcess)
    {
        // get the physical mem usage for this process
        var pc = new PerformanceCounter("Process", "Working Set - Private", currentProcess.ProcessName, true);
        pc.NextValue();
        Thread.Sleep(1000);
        var privateWorkingSet = pc.NextValue()/ 1024/1024;
        return privateWorkingSet;
    }

You more than likely have some bad code rather than an actual memory leek. - memory leaks are caused by not closing connections to resources rather than the code.. soda speak... the c# garbage collection will free up managed resource.

Look for c# static objects... You shouldn't have ANY in a windows service unless you actually know why its there and its used as a helper.

Windows services are usually used for polling something.... in the work of the poll... there should not be any static classes being used for anything other than a helper.

If you give more context as to what your windows service does.. then better suggestions can be made.

PS. to answer your question: Ant Memory profiler by red-gate is quite good. BUT i bet you can find it faster by just understanding what the code is doing.

Upvotes: 2

Related Questions