Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET
Why its highly recommended to dispose all objects/Datasets/Datatables in finally block when .net have its own Garbage collector which handles memory deallocation automatically and runs after specific interval.
 
Thanks in advance.
Posted 22-Mar-13 2:13am
Edited 22-Mar-13 2:19am
v2
Comments
ryanb31 at 22-Mar-13 7:22am
   
Because it is my understanding that there is no specific interval. In one of the versions of .Net, which may have been fixed now, there was also an issue with how the garbage collector was counting the pointer to an object and in some cases it was wrong.
 
As practice, it's just a good idea to clean up after yourself.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

There are a couple of reasons.
The first one is because some objects use resources other than memory, which it is important to return to the system as soon as possible so that they can be reused for other purposes. Some of these may be local to you application, others may be local to your whole PC. If they are not released, then "leaks" occur because the total number of available resources drops each time you create an instance, and sooner or later the system runs out. Since the Garbage Collector is only activated by low memory, there is no mechanism for returning these resources other than explicit Disposal. Such objects include (but are not limited to) Graphics contexts, Pens and SqlConnection / SqlCommand objects.
 
It also helps to free up files which are held open by some commands: Bitmap.FromFile for example aquires a write lock on the file which is only released when the bitmap is disposed. Since you can't rrely on teh GC kicking in at any point, Disposing of the bitmap is the only way to free up the file it is based on for writing.
 
It also helps because it can keep the memory footprint down - if you constantly allocate and Dispose objects of the same size, the same memory can be re-used and your whole app runs more smoothly because the GC is never needed (and it always runs at the most awkward times Laugh | :laugh: )
  Permalink  
Comments
   
Good response. +5
Mickt1985 at 22-Mar-13 8:04am
   
Great Explination. +5
deepakdynamite at 22-Mar-13 10:07am
   
Thanks alot for such a wonderful justification
OriginalGriff at 22-Mar-13 10:32am
   
You're welcome!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You should dispose objects explicitly when they use unmanaged code inside them.
  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 410
1 Sergey Alexandrovich Kryukov 329
2 Afzaal Ahmad Zeeshan 264
3 BillWoodruff 235
4 CPallini 195
0 OriginalGriff 5,560
1 DamithSL 4,476
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 22 Mar 2013
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