Memory leak problem

Topics: Prism v2 - Silverlight 4
Feb 7, 2011 at 6:35 AM
We are building a LOB application using SL4,PRISM and MVVM. I noticed very lately that my App is not releasing any memory. It Starts around 150MB and grows to 1 GB+ very fast if i switch b/w pages. Our design involves 3 modules each having a Controller class(Unity singleton) which listens for events and injects views into the appropriate region. So this is whats happening in those controllers when an event is fired to inject a view.

var v = Container.resolve<view>()
var vm = Container.resolve<viewmodel>()
v.datacontext = vm; myregion.addtoregion(v);

so 100 calls for this view will exceute this 100 times. And in addviewregion method, i'm removing all existing views before adding the new one. And after seeing the memory problem, i set both view & viewmodel to null in addviewtoregion method for all the views nedd to be removed. still memry consumption is same. Is Unity ContainerControllled... objects(my controllers) holding referrences to these objects?



Update:

The following behavior illustrates the title of this discussion:

WeakReference ref1 = new WeakReference(Container.Resolve<Footer>());
WeakReference ref2 = new WeakReference(new Footer());
GC.Collect();
GC.WaitForPendingFinalizers();
bool isRef1Alive = ref1.IsAlive;
bool isRef2Alive = ref2.IsAlive;

Footer is a UserControl and here first one returns true whereas the second one returns false. Why is this behavior different? Does Container holds any reference to the item it resolves? BTW, i dont have any interfaces for this view. I added

Container.RegisterType<Footer, Footer>();

in the Bootstrapper(ConfigureContainer method). still same result.

 

Feb 7, 2011 at 2:33 PM

Hi,

This is an existing issue on Prism v2, you could take a look at the Damian Schenkelman’s Blog post, which describes a memory leak:

Memory Leak removing View with child regions in Prism-v2

I hope you find this information useful.

Thanks,

Miguel Bronzovic
http://blogs.southworks.net/mbronzovic

 

Mar 21, 2011 at 1:44 PM
Edited Mar 21, 2011 at 1:47 PM

Hi,

I register view like this (just user control with text block)

regionManager.RegisterViewWithRegion("MainRegion", typeof(Views.HelloWorldView));

region is declared like

<ItemsControl Grid.Row="1" Name="MainRegion" Regions:RegionManager.RegionName="MainRegion"/>

then I remove it from region like

IRegion mainRegion = _regionManager.Regions["MainRegion"];
object view = mainRegion.Views.FirstOrDefault();
mainRegion.Remove(view);
GC.Collect();

Then I create dump file to see references to the view in windbg and the result is

!gcroot 05a087dc       
Note: Roots found on stacks may be false positives. Run "!help gcroot" for
more info.
Please note that 05a087dc is not a valid object.
Scan Thread 5 OSTHread 158c
Scan Thread 28 OSTHread 3c8
Scan Thread 29 OSTHread f80

I also add destructor to view to see if it is called

~HelloWorldView()
        {
            GC.SuppressFinalize(this);
        }
But it is called only when browser is closed.

So why does this view hangs in memory? btw, I use Prism v.4 nov 2010

Mar 21, 2011 at 8:51 PM

Hi,

As we mentioned in the previous thread, this is a known existing issue in Prism, which affects v2.2 and v4.

A work item has been created on the issue tracker, so the team is aware of this issue. We expect this issue is going to be fixed in the next drops.

Thanks,

Miguel Bronzovic
http://blogs.southworks.net/mbronzovic