Quantcast
Channel: Common Language Runtime Internals and Architecture forum
Viewing all articles
Browse latest Browse all 1710

OnPropertyChanged causes massive amount of Action allocations

$
0
0

(This is a crosspost from: https://stackoverflow.com/questions/51799253/onpropertychanged-causes-massive-amount-of-action-allocations)

I have a WPF application which displays images in a grid. The user has the possibility to scroll fast through images by moving the mouse while left clicking. So this means there are a lot of images changing at a fast rate.

I noticed a performance degradation for 24 and 40 element sized grids (which are the largest). I identified the cause of the slowdown was a lot of garbage being created which caused lots of blocking gen0 and gen1 garbage collections. By reducing the amount of allocations I could improve the peformance from about 10 fps to 25fps.

Still not completely satisfied with this, I analyzed a bit more and noticed there is a massive amount of Action objects being produced, exceeding all other types being allocated by far. When scrolling, I get around 50000 allocations of Actions per second.

I tracked this further down with a memory profiler, and the cause is one PropertyChanged event which fires every time an image shall change. This internally causes some events to be registered and deregistered, this event manager used the class ReaderWriterLockWrapper. Indeed, every time the properties WriteLock or ReadLock are accessed, an Action will be allocated, as seen in the reference source under WindowsBase/Shared/MS/Internal/ReaderWriterLockWrapper (I cannot post links here)

CallWithNonPumpingWait(()=>{_rwLock.EnterWriteLock();});

I noticed the code is this way since framework version 4.7. Is this intended? This alone causes 16MB worth of Action instances in my application when I continuously scroll for 8 seconds. Also, the way I see it, this lambda could just be an instance member.


Viewing all articles
Browse latest Browse all 1710

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>