Udostępnij za pośrednictwem


DispatcherTimer and Managed Memory Leaks

Recently, while diagnosing a managed memory leak,  we found a scenario where a running DispatcherTimer can cause an object to be ineligible for garbage collection.
Here's how it can happen: when a DispatcherTimer is started, it is adding itself to _timers list of the current Dispatcher.
So the Dispatcher is holding to the DispatcherTimer and the timer is holding on to its owner object via Tick event handler.

The bottom line: make sure DispatcherTimer is stopped before it goes out of scope.