.NET Framework Bookmark and Share   
 index > Common Language Runtime > Setting an object to null claims back memory?
 

Setting an object to null claims back memory?

Hi,

Lets assume that i have a class called as DataBaseHelper and it has private fields which hold the database connection.

DataBaseHelper helper = new DataBaseHelpe();

//Some blocking code..

helper = null;

Now when i set helper=null,does that automatically reclaim memory that was alloted in the managed heap for the helper object and release all the resources held by that object?
Haripraghash
Hello,

No, memory will be reclaimed within next run of Garbage Collector. Setting reference to null only makes this object applicable for Garbage Collection.
Vitaliy Liptchinsky http://dotnetframeworkplanet.blogspot.com/
  • Marked As Answer byHaripraghash Wednesday, September 02, 2009 11:30 AM
  •  
Vitaliy Liptchinsky
Garbage collection should reclaim the memory when the object drops out of scope so you don't need to set it to null. Garbage collection runs non-deterministically.

You can force a garbage collection by calling GC.Collect(). Calling this is a big overhead.

See http://msdn.microsoft.com/en-us/library/xe0c2357.aspx

Sometimes you may need to call a Dispose() directly on the object (if available). This should ensure object is freed correctly when called.

May be useful links on this:

http://weblogs.asp.net/pwilson/archive/2004/02/20/77422.aspx
http://stackoverflow.com/questions/49950/garbage-collection-is-it-necessary-to-set-large-objects-to-null-in-a-dispose-met

Also, if you have connections to the database, I think it is best practice to always call Close() if the connection is open. I'd normally wrap this up in a Try... Finally block and only call Close() in Finally if the connection object is not null.

Andez
Andez
  • Marked As Answer byHaripraghash Wednesday, September 02, 2009 11:30 AM
  •  
Andez
Hello,

No, memory will be reclaimed within next run of Garbage Collector. Setting reference to null only makes this object applicable for Garbage Collection.
Vitaliy Liptchinsky http://dotnetframeworkplanet.blogspot.com/
  • Marked As Answer byHaripraghash Wednesday, September 02, 2009 11:30 AM
  •  
Vitaliy Liptchinsky
Garbage collection should reclaim the memory when the object drops out of scope so you don't need to set it to null. Garbage collection runs non-deterministically.

You can force a garbage collection by calling GC.Collect(). Calling this is a big overhead.

See http://msdn.microsoft.com/en-us/library/xe0c2357.aspx

Sometimes you may need to call a Dispose() directly on the object (if available). This should ensure object is freed correctly when called.

May be useful links on this:

http://weblogs.asp.net/pwilson/archive/2004/02/20/77422.aspx
http://stackoverflow.com/questions/49950/garbage-collection-is-it-necessary-to-set-large-objects-to-null-in-a-dispose-met

Also, if you have connections to the database, I think it is best practice to always call Close() if the connection is open. I'd normally wrap this up in a Try... Finally block and only call Close() in Finally if the connection object is not null.

Andez
Andez
  • Marked As Answer byHaripraghash Wednesday, September 02, 2009 11:30 AM
  •  
Andez
Thanks for responding. Since setting the object to null can make it applicable for garbage collection,what happens to the resources held by that object?Can we assume that the resources will be freed once we set the objcet to null?If thats the case,then i really dont need to close the database connection(I understand that closing the db connection is a best practice). Sorry if these questions sound stupid.
Haripraghash
Thanks for responding. Since setting the object to null can make it applicable for garbage collection,what happens to the resources held by that object?Can we assume that the resources will be freed once we set the objcet to null?If thats the case,then i really dont need to close the database connection(I understand that closing the db connection is a best practice). Sorry if these questions sound stupid.

In order to free external resources used by your object ASAP, you should implement IDisposable interface in your class. More info here:
http://msdn.microsoft.com/en-us/library/fs2xkftw.aspx

Vitaliy Liptchinsky http://dotnetframeworkplanet.blogspot.com/
Vitaliy Liptchinsky

Hello,

There is a nice article in MSDN for Garbage Collection, IDisposable and Finalizer. It is in 2 parts.

Part 1 :

http://msdn.microsoft.com/hi-in/magazine/bb985010(en-us).aspx

Part 2 :

http://msdn.microsoft.com/hi-in/magazine/bb985011(en-us).aspx

If you have enough time, do do do read this. It is very much informative. If you understand it you can figure out answer of your question.

Regards.

sisvis Eclipsys
Always close the connection to the database. The primary reason for this is not you application but the database itself. Though it will be returned (in theory) when it goes out of scope, that may be a while (in terms of cycles). Other processes may be hindered because they can't get a database connection OR because the database server has no more connections to issue. Even if it'sa simple web application, multiple users hitting it could exhaust the pool rather quickly. You only get the benefit of connection caching if there is a connection in the cache. I cannot be reused until it's open.

Ony way to really show this is to access something like aMySql database where the default max connections is 10. Create a web page to do something (hit the database, pull a few records, do about 3 seconds worth of processing -- sleep comes to mind, then exit the page). Run this 11 times real fast (within the 3 seconds) and you will have a database error because of those connections.

Gary Wayne Smith

You can use google to search for other answers

Custom Search

More Threads

• Unable to Specify fillPath in CustomLineCap Constructor
• RegFree COM - failing when .net dll using reg-free is loaded dynamically by system.web?
• How to run CodeDom code in the same context?
• System Restarts for any exception in .Net
• Hanged up on working concept of ISAPI.DLL ,ASPNET_WP.exe,Http Handler and Http Module
• CORSEC_E_NO_EXEC_PERM with host assembly store
• Reclaiming Memory
• Understanding Synclock
• how long should the key be
• Is there a safe way to set the process CommandLine value so it can be reset and resized?