Thursday, March 7, 2013

Garbage collection in C#

Useful links to summarize Garbage collection working:

1. Dispose Pattern (used for freeing up unmanaged resources) :
2. Dispose Method (exist with each disposable object) :

3. In case we are using Dispose method call then order of dispose method call is significant. E.g. If object A allocates object B which in turn allocates object C then dispose method of object A should call dispose method of object B, dispose method of object B should call dispose method of object C.

4. Most common memory leaks in WPF and their fixes:
Brief: 1. Event handlers registered with parent object.
2. Registering event with static objects. Static members never releases memory so window won't be able to dispose properly.
3. Timers: Associating events with timers can cause memory leaks because timer won't stop automatically and event will keep on calling.
4. Data Binding: Make sure data binding on control should use the dependency property because only they are disposed by the GC. If you bind a control with a non dependency property then that control will not be disposed.
5. Changing text property too much. Keep limit on no. of operations to keep in undo stack. 

No comments:

Post a Comment