all groups > dotnet clr > january 2004 >
You're in the

dotnet clr

group:

Memory Leak Investigations



Memory Leak Investigations Nick Carter
1/30/2004 1:51:35 PM
dotnet clr: I am searching for a suspected memory leak and I have made some assumptions
and I would be
grateful if someone could check my thinking...

I have a remoting server that I believe has a memory problem. My first
assumption is that I
believe it has a memory problem because I am checking "Mem Usage" in Task
Manager. One
morning the Mem Usage is, say, 100Mb and the next morning it is, say, 200Mb
(after a
complete enforced garbage collection) and the next morning it is higher yet
again.
Eventually the remoting server throws an out of memory exception. So my
first assumption is
that it is safe to use the Mem Usage figure in this way. I know I could use
..NET's memory
performance counters instead but Task Manager was certainly present on the
target machine
so it was quick and easy.

My second assumption is that the increase in memory is a consequence of a
memory leak. I
wonder if this is actually true. I am wondering if the garbage collector is
resizing
generation 0, 1 and 2 so that they are bigger so that they can more easily
cope with the
load that it is under. If this were the case then surely the increase in
memory requirement
is simply normal behaviour and not a sign of a memory leak.

I am using SciTech's .NET Memory Profiler to see if there is a memory leak.
I am new to this
tool so I could be using it wrong. I take a snapshot once the remoting
server is up and
running and has been doing a little bit of normal processing. Then when I
think the memory
requirement has gone up unnecessarily and is not coming down again (even
after a garbage
collection) I take another snapshot and compare them. My next assumption is
that if the
"Delta" column shows "0" differences for all classes between the two
snapshots then there is
no memory leak. Is this right ?

I am also wondering if the memory leak could be in unmanaged code instead of
managed code. I
am not familiar with how unmanaged code handles memory and if I would see a
memory leak that
originated from unmanaged code.

Any pointers on any of these issues would be gratefully received.

Thanks.

Nick Carter

Re: Memory Leak Investigations Nick Carter
1/30/2004 2:32:24 PM
Vadym,

[quoted text, click to view]
objects.

The references to objects are released (because the references go out of
scope) so the objects are released.

[quoted text, click to view]

Fortunately we have no objects which use unmanaged resources (except for the
classes in the .NET Framework which uses unmanaged resources).

[quoted text, click to view]

Thanks. I started to look at it but it was a bit daunting. I will take
another look.
Nick

[quoted text, click to view]

Re: Memory Leak Investigations Vadym Stetsyak
1/30/2004 4:08:46 PM
Memory will never be released if you have strong references to the objects.
Try to use weak references, or ensure that objects that use unmanaged
resources have implemented Dispose method.

Try to use CLR Profiler from Microsoft, it will show you the heap map & the
amount of memory objects of particular type consume....

[quoted text, click to view]

Re: Memory Leak Investigations Niall
2/4/2004 11:36:28 AM
Nick,

I have been using the .Net Memory Profiler pretty heavily for quite some
time. At first, it can be a little confusing, but I have not found another
tool that gives you the information as usefully as this profiler. The CLR
Profiler is good, but if your app is of a decent size, you can sit there all
day watching it draw pretty pictures.

What the .Net Memory Profiler provides which I find most useful is the list
of root paths to an object. As far as I can see, the CLR Profiler and all
other memory profilers I've tried just show you all references, which is a
lot when you are looking at windows forms (event handlers are everywhere).
The root paths are the reasons why the object isn't collectible, and hence
the root paths are the only information you need to find a memory leak.

So. The way you are using the profiler sounds correct to me. Take a
snapshot, let it do some work and get to a stage where you think its memory
should be freed, and take another snapshot. Go View->Class Filter->Show
Classes With New Or Removed Instances. This will basically show you what was
created or collected between the two snapshots.

You can then look through the list of classes and find objects that you
think should have been created and then collected, but the profiler shows as
still alive. Double click on the suspicious classes to go to the list of
instances. You can have a toy with the allocation stacks to find which
instances you're interested in, then pick one and double click it to see its
details. The list of root paths that show are the reasons why the object has
not been collected. From this, you can trace it back to the the source of
the problem and change your code to free up the appropriate references.

Be aware that the profiler does a full GC when taking a snapshot. Hope this
helps!

Niall

[quoted text, click to view]

Re: Memory Leak Investigations Nick Carter
2/4/2004 9:58:49 PM
Niall,

Thanks for the confirmations, clarifications and advice. I will have another
look following some of your steps. (I hadn't realised that a GC is performed
before a SnapShot - that saves me a step that I was doing myself).

Thanks.

Nick

[quoted text, click to view]



AddThis Social Bookmark Button