Xtreme Visual Basic Talk

Xtreme Visual Basic Talk (http://www.xtremevbtalk.com/)
-   .NET General (http://www.xtremevbtalk.com/-net-general/)
-   -   Which objects require "DISPOSE" ? (http://www.xtremevbtalk.com/-net-general/328558-objects-require-dispose.html)

skyfox 01-28-2017 02:18 PM

Which objects require "DISPOSE" ?
Can someone please shed some light in as to what the criteria is used in the .NET world for determining which objects require the "Dispose" method to release their resources vs. using "MyObject = Nothing?


PlausiblyDamp 01-28-2017 06:23 PM

If you have a class that holds onto non-dotnet resources e.g. file handles, database connections and similar for longer than a single method then your class should implement IDisposable and therefore provide a Dispose method. These resources need to be cleaned up in a timely fashion and the Dispose method is a way of allowing an application to free up these external resources at a know point in time, rather than waiting for the garbage collector to kick in and free them up at some point in the future.

Setting a variable to nothing is often unnecessary in .Net - the runtime tracks when a variable is still accessible or not and setting a variable to nothing can actually keep a variable in memory longer than just letting the GC figure it out.

Setting things to nothing can be useful if you wish to free up a single element in an array or similar, it can also help to free up memory if you have variables at the class level that are no longer needed; within a method though it is probably not needed.

All times are GMT -6. The time now is 07:57 AM.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2018, vBulletin Solutions, Inc.
Search Engine Optimisation provided by DragonByte SEO v2.0.15 (Lite) - vBulletin Mods & Addons Copyright © 2018 DragonByte Technologies Ltd.
All site content is protected by the Digital Millenium Act of 1998. Copyright©2001-2011 MAS Media Inc. and Extreme Visual Basic Forum. All rights reserved.
You may not copy or reproduce any portion of this site without written consent.