Click here to Skip to main content
12,950,832 members (57,202 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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 26-Feb-12 23:56pm
Rate this: bad
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!
Rate this: bad
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.
Rate this: bad
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:-[^])

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.


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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 5,984
CHill60 3,460
Maciej Los 3,053
Jochen Arndt 1,975
ppolymorphe 1,820

Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 27 Feb 2012
Copyright © CodeProject, 1999-2017
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