2

I have a custom application that allows to open some custom models. If I open a model in the application, then open another model - memory isn't released from the first model.

When I try to profile memory leak with profiler (ANTS memory profiler), the application releases memory and I'm not able to track the leak. How can I manage this problem? Memory usage chart from profiler

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user1104698
  • 111
  • 8
  • 4
    This would indicate that you don't have a leak and that the conditions that trigger GC simply haven't been satisfied in your example. Is it really a "problem"? – spender Jun 16 '15 at 14:53
  • Just how to you know it is not releasing memory? – paparazzo Jun 16 '15 at 14:57
  • 1
    Indeed... @Blam s question is very pertinent. Looking at memory usage in Task Manager (for instance) is not a reliable indicator. – spender Jun 16 '15 at 14:59
  • As @spender said, it it really a problem? Because if it ain’t broke, don’t fix it. – Dolgsthrasir Jun 16 '15 at 15:00
  • After opening of first model the app use 2gb of RAM, after opening of second model first model is closed and memory should be released. But app use 4gb – user1104698 Jun 16 '15 at 15:00
  • 2
    @user1104698 How are you measuring that? – spender Jun 16 '15 at 15:02
  • spender, It is indicated in TaskManager and in MemoryProfiler (image is attached). The vertical lines on the attached chart are momens of taking snapshots. – user1104698 Jun 16 '15 at 15:05
  • 1
    Task Manager is NOT a reliable memory use indicator. Releases memory often does not show up in Task Manager. Trust ANTS. – paparazzo Jun 16 '15 at 15:15
  • @Blam, Ok, I trust ANTS, but it also doesn't show that the memory is released until I press "Take memory snapshot" button. – user1104698 Jun 16 '15 at 15:18
  • I would agree with the first comment from @spender. It is not a memory leak. If it is a memory leak, the red horizontal line will not drop down, it will keep going up. It looks like that your models have some native code, or calls to win32 which is not calling the GC. Have you tried adding GC when you close the model? – Jegan Jun 16 '15 at 15:51
  • 1
    "Take memory snapshot" *forces* a GC collection. If you want to emulate this in your app, you *can* call GC.Collect at the appropriate time. However, unless you're actually seeing negative effects due to GC not doing it's job properly, just let it run when it wants to. – spender Jun 16 '15 at 15:59
  • @Jegan: Yes, I've tried. I have this code on Close: GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); but it doesn't work. – user1104698 Jun 16 '15 at 16:29
  • What do you mean does not work? Just because you don't see it does not mean it does not work. – paparazzo Jun 16 '15 at 22:31
  • 1
    You almost certainly do not have a memory leak. I strongly suggest you stop worrying about that until you find your application running out of memory. You are simply not understanding how GC and virtual memory work. There _is_ no problem. – John Saunders Jun 17 '15 at 01:16

1 Answers1

3

When you take a snap shot ANTS memory profiler does a full garbage collection.

When you want to take a snapshot, I normally take 2-3 snapshots until there is not memory differences between two consecutive snapshots. Then compare with your base snapshot.

Go to instance list and see if there is any instances are growing. Select the Objects with Source in order to get rid of heaps of system object.

If there is any growing instances, pick one and see the objects retention graph which will show you exactly which instance holds the reference.

And also, make sure that you have implemented IDisposable properly and dispose all disposable objects and unsubscribe from all event subscriptions.

Have a look at below walkthroughs

http://www.red-gate.com/products/dotnet-development/ants-memory-profiler/walkthrough http://www.red-gate.com/products/dotnet-development/ants-memory-profiler/

CharithJ
  • 46,289
  • 20
  • 116
  • 131