0

I have a WPF/E application which is seeing huge performance issues along with memory leaks.

Using the .NET memory profiler by red gate did help, but what concerns me is that objects(>100MB after 10 mins of usage) keep staying at Generation 2 of GC.

This leads to performance issues as the app significantly slows down. Calling the GC.Collect() does reclaim some of the memory, but this is not the solution as calling GC is not recommended- and i also do not want objects to move to Gen 2 in the first place.

Are there any best practices to follow in the WPF MVVM pattern(or otherwise) so that objects that reach Gen 2 remain at a minimum? Thanks in advance!

CharithJ
  • 46,289
  • 20
  • 116
  • 131
  • GC cannot collect "alive" instance simply because your instance is still on hold by some other objects. You need to find that out by inspecting suspious area in your code by memory profiler. There's some alternative e.g. WeakReference to make sure your object can be collected but i'm not sure if it fits in your situation as there's no information provided. BottomLine is that long-lived instances are supposed to stay in Gen2 and stay there. There's nothing wrong about it. The only problem is that you need to release those memory in Gen2 shortly after they reach there. Don't mix up the problem. – cscmh99 Oct 13 '15 at 06:12
  • Memoy leak is another problem that not much people can help. Well ... unless you can narrow down the specific class or segment of code that you suspect causing the memory leak and post the code to the question... – cscmh99 Oct 13 '15 at 06:17
  • @cscmh99 I understand that. Can you please elaborate on **The only problem is that you need to release those memory in Gen2 shortly after they reach there.** I will also dig into the code and investigate further on this. – Turing Machine Oct 13 '15 at 07:33
  • Please refer to the "Too Many Almost-Long-Life Objects" section of https://msdn.microsoft.com/en-us/library/ms973837.aspx. Again, having long lived memory in Gen2 is not a problem at all. Having "Almost-Long-live" object is the problem. So, don't worry about your object in Gen2 if "Almost-Long-live" object is not your case – cscmh99 Oct 13 '15 at 07:38

1 Answers1

0

Are there any best practices to follow in the WPF MVVM pattern(or otherwise) so that objects that reach Gen 2 remain at a minimum?

Nothing special for MVVM other than general best practices such as disposing all IDisposable instances and unsubscribing from long lasting even subscriptions.

There is no point of calling GC.Collect manually in this case. If your objects are in Gen2 that means they have already gone through two garbage cycles. So, there are some references to those instances.

I guess what you should do is have another look at those instances and see if you have got any live event subscriptions, if so unsubscribe them. And check if there are any IDisposable fields that are not disposed. If there are any IDisposable fields you better to implement IDisposable Pattern.

Community
  • 1
  • 1
CharithJ
  • 46,289
  • 20
  • 116
  • 131