none
Setting an object to null claims back memory? RRS feed

  • Question

  • 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?
    Wednesday, September 2, 2009 8:05 AM

Answers

All replies

  • 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 by Haripraghash Wednesday, September 2, 2009 11:30 AM
    Wednesday, September 2, 2009 8:16 AM
  • 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 by Haripraghash Wednesday, September 2, 2009 11:30 AM
    Wednesday, September 2, 2009 8:18 AM
  • 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.
    Wednesday, September 2, 2009 8:36 AM
  • 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/
    Wednesday, September 2, 2009 8:39 AM
  • 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.

     

    Wednesday, September 2, 2009 8:50 AM
  • 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's a 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 a MySql 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.

    Thursday, September 10, 2009 6:38 PM