WeakReference RoutedEventHandlerData.Handler dies prematurely

Feb 11, 2010 at 4:20 PM

The WeakReferences in my app are pointing to event handlers in a UI element that is being contained within a VirtualizedStackPanel. Because of this, it creates new objects quickly as I scroll through the list and new items come into view. But even though those UI elements themselves stay in memory (if I stop scrolling), the WeakReference dies within several seconds.

From Jeffrey Richter's book, CLR via C#:
Garbage collections do not occur when memory is full or close to full. Instead, garbage collections occur whenever generation 0 is full, which occurs approximately after every 256 KB of memory is allocated. So objects are being tossed out of memory much more frequently than desired, ...

So with the rapid creation of UI elements in a virtualized stack panel, it seems likely that this 256 K limit will be hit over and over again, causing the garbage collector to spin up and destroy these weak references, causing the problem. Perhaps WeakReference is not the right tool for the job in this scenario?