Memory Cleanup Question

A forum dedicated to WPF version of LightningChart Ultimate.

Moderator: Queue Moderators

Post Reply
User avatar
blakeadkins
Posts: 44
Joined: Tue Feb 25, 2014 7:49 pm

Memory Cleanup Question

Post by blakeadkins » Mon Jan 12, 2015 10:15 pm

If there are events that are on the chart, but I call dispose on the chart, does the chart actually get garbage collected or do I need to remove the event handler?

Same if I have event handlers on items such as bands; do I need to remove the event handlers for them as well?

ArctionJari

Re: Memory Cleanup Question

Post by ArctionJari » Wed Jan 14, 2015 12:17 pm

Calling Dispose method doesn't mean automatic memory deallocation of an object. It just tells the object to clean up itself (i.e. release internal resources etc.) so that it is ready for garbage collection. If you want to force a garbage collection you have to call GC.Collect and GC.WaitForPendingFinalizers methods. Forcing is not recommended in most cases because GC knows what it's doing and forcing will only interrupt the current work.

What comes to events, event delegate contains reference only to event target (e.g. your main application where your event handling method is) so event source (chart, band, ...) is not affected by the event. Therefore, you don't have to unsubscribe from the events to make you chart ready for garbage collection. Just call Dispose and make sure that nothing is referencing to chart or its properties.

Events cause memory leaks if event source outlives event target. Then you have to explicitly unsubscribe from events hence making reference from event delegate to event target null. Otherwise event target won't get collected.

User avatar
blakeadkins
Posts: 44
Joined: Tue Feb 25, 2014 7:49 pm

Re: Memory Cleanup Question

Post by blakeadkins » Wed Jan 14, 2015 9:19 pm

Thank you; that clears it up!

Post Reply