Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
I have created list of object.
I am deleting objects from list of objects, do i need to dispose these objects through code or Garbage Collector will take care of it?
Posted 27-Feb-12 0:56am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

It depends.
 
If the object implements IDisposable, then you should call Dispose on it, (or enclose it in a using block) as it contains scarce resources which need to be released ASAP. Examples here are Streams, SQL Connections, Graphics objects, and so forth.
 
If it doesn't, then just let the GC get on with it normally - 99% of the time, it does a better job of deciding what and when to dispose things that you will!
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

The garbage collector should take care of it unless you're using GDI objects or images. At that point, you should probably derive your objects from IDisposable and then call your object's Dispose method when applicable. Your Dispose method should dispose of any resources it created.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

It is always better to dispose off the unused objects which are no longer in use. Like if you take an example of the Connection object, it is generally being disposed off in a 3-Tier architecture when the full transaction gets completed as it is not always good to close a connection as it may have performance issues om the application. Although a Garbage Collector takes care of it and destroys an open connection when it is no longer in use for long time. You must have heard about the GENERATIONS in Garbage collector (refer:-http://msdn.microsoft.com/en-us/library/ms973837.aspx[^])
 
In the same way disposing the objects which are no longer in use will obviously increases the performance rather then allowing the garbage collector to do the same task.
 
Thanks
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 200
1 PIEBALDconsult 150
2 BillWoodruff 148
3 Jochen Arndt 135
4 DamithSL 130
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 27 Feb 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100