When should we do memory management and How do we do it for both the types of resources? What is the list of managed and unmanaged resources? Do we really need to worry about memory leaks or not?
Asked
Active
Viewed 1,261 times
1 Answers
5
Managed resources are those that are fully written in .NET. Though not normally subject to classic memory leaks, one can still leak memory by not dereferencing unused resources (the most common reason is to not un-register event handlers).
Unmanaged resources are those that are generally those that are not pure .NET (and in the same process) - examples are:
- COM components
- Database connections/transactions
- Window handles
- Filesystem handles
- Registry handles
- Network connections
- etc... etc...
For these, you need to implement the Dispose
pattern, correctly and ensure proper disposal when you have finished using them.
Do we really need to worry about memory leaks or not?
Yes, we do need to worry about them, in particular when going outside of .NET.
-
@sly_Chandan - Not normally, no. – Oded Feb 17 '13 at 19:51
-
Do I need to dispose a dataset? – sly_Chandan Feb 18 '13 at 10:25
-
1@sly_Chandan - A good rule of thumb - if it implements `IDisposable`, dispose of it. – Oded Feb 18 '13 at 10:26
-
public class DataSet : MarshalByValueComponent, IListSource, IXmlSerializable, ISupportInitializeNotification, ISupportInitialize, ISerializable .......It doesnt implement IDisposable – sly_Chandan Feb 18 '13 at 10:33
-
@sly_Chandan - `MarshalByValueComponent` implements `IDisposable`. The magic of inheritance. – Oded Feb 18 '13 at 10:34
-
let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/24695/discussion-between-sly-chandan-and-oded) – sly_Chandan Feb 18 '13 at 10:34