Killing instances of view-s

Topics: Prism v2 - WPF 3.5
Mar 4, 2010 at 1:42 PM


this is my scenario. I have 8 modules and in every module in IModule class I register viemodel+model via:


Me._container.RegisterType(Of IStranicaViewModel, StranicaViewModel)()

Me._container.RegisterType(Of IStranica, Stranica)()

Then I resolve them when the module gets initialized: 

 Dim model As IStranicaViewModel = Me._container.Resolve(Of IStranicaViewModel)()

                    model.sXml = arr(i)
                    Me._regionManager.Regions("Stranice").Add(model.View, "Stranica" & i.ToString)


 Me._regionManager.Regions("Stranice").Add(model.View, "Stranica" & i.ToString)



Thing that I want to do is on a event to kill the view instance added to the region and create new instance. I tried with removing the view from region:

  Dim Kolekcija As IViewsCollection = Me._regionManager.Regions("Stranice").Views

            For j As Integer = Kolekcija.Count - 1 To 0 Step -1

                If TypeOf (Kolekcija(j)) Is IStranica Then


                End If


I used the SciTech.NET.Memory.Profiler to see if I manage to kill old instances before creating new ones but their number just gets increasing. What else do I need to do to kill unwanted view instances, cos the memory usage of my application just coontinues to rise as i click....

Mar 4, 2010 at 7:40 PM


We have tried to reproduce your issue by creating a sample application with a scenario similar to the one you are explaining but were not able to reproduce it. However, below you can find some things that might help you:

  • Try forcing garbage collection, after all the views have been removed, by adding a button that simply calls GC.Collect. It is possible that the memory might not be being released since it is not being needed.
  • Check that no other object is keeping a reference to the views.
  • This post explains a possible scenario when removing views from a region could cause memory leaks.

It would be useful if you could provide a small repro sample so we can help you solve this issue.

Please let me know if this helps.

Damian Schenkelman

Mar 5, 2010 at 11:27 AM

I tried this approach with adding ClearChildViewsRegionBehavior (I am too adding a view with his own regions into a region in Shell), but when I remove that view from Shell region it doesn't get disposed... I tried GC.Collect, still no luck.

I tried unsubscribing all event subscriptions that didn't help either.

Mar 10, 2010 at 8:53 AM



Anyone having suggestions?

May 4, 2010 at 7:13 PM

Got the same problem, the ViewModels are living in the background, and catching events....

Jan 13, 2011 at 1:15 AM

Think i'm having the same problem.

Tried ClearChildViewsRegionBehavior and that didn't do much, i put a breakpoint on the actual property changed code and it never gets called.

I'm actually finding that if there are multiple instances of a view that it actually cleans some of them up, but it always leaves at least one instance of each view in memory.

This is the same case for every view in our system, it's not a code error, i've been using Redgate Memory Profiler and can see only a weak reference to the views from RegionCollection.

Jan 13, 2011 at 6:43 PM


Perhaps the IRegionMemberLifetime interface can be helpful for you to fix this undesired behavior.

You might find the following threads that deal with that interface useful:

I hope you find this helpful.

Guido Leandro Maliandi