Results 1 to 4 of 4
  1. #1
    piyu.sha is offline Member
    Join Date
    Oct 2008
    Posts
    31
    Rep Power
    0

    Question How setting an Object to null help Garbage Collection?

    I was reading through some forums and found suggestion that setting an object to null helps Garbage collection? Is it true. How does it matter for GC whether a reference is already set to NULL or not, GS has to still do its check on the reference right??

    Any insight? Does it depend on the GC algorithm ?
    Live life king size
    [Lucene]

  2. #2
    kzvi.kzvi.1 is offline Member
    Join Date
    Oct 2008
    Location
    US
    Posts
    58
    Rep Power
    0

    Default

    I don't know about many GC algorithms though, but I think setting an unused object reference to NULL explicitly should help any GC algorithm as it is going to save some effort for it.

    But it can create problem for GC if a program sets a object to NULL and later re-reference it. Read more about GC here...Garbage Collection

    ____________________________________________
    Software Wiki | Interview FAQs | Lucene Search | Oracle | ORM | Struts2 | Job Seeker
    Have fun....
    JAVA FAQs

  3. #3
    Join Date
    Oct 2008
    Posts
    68
    Rep Power
    0

    Default

    I think you misunderstood.

    Actually the garbage collector checks if an Object has any reachable reference in any threads. If it has then the Object is not garbage collected.

    And about setting the Object to null..See this example

    code:

    class GarbageTry
    {
    public void finalize()
    {
    System.out.println("Destroyed...");
    }
    public static void method()
    {
    GarbageTry obj = new GarbageTry(); //(1)
    System.out.println("Sleeping");
    try
    {
    System.gc();
    Thread.sleep(5000); //pause to simulate some long processing
    }
    catch(Exception e)
    {}
    System.out.println("Woken");
    }
    public static void main(String[] args)
    {
    method(); //(2)
    try
    {
    System.gc();
    Thread.sleep(5000); //pause so that the object is garbage collected before execution ends.
    }
    catch(Exception e)
    {}
    }
    }
    In this code the object will be destroyed after the call at (2) returns. So the memory occupied by the object created at (1) remains occupied for no reasons. To avoid this change this program to-

    code:

    class GarbageTry
    {
    public void finalize()
    {
    System.gc();
    System.out.println("Destroyed...");
    }
    public static void method()
    {
    GarbageTry obj = new GarbageTry(); //(1)
    obj = null; //(x)
    System.out.println("Sleeping");
    try
    {
    System.gc();
    Thread.sleep(5000); //pause to simulate some long processing
    }
    catch(Exception e)
    {}
    System.out.println("Woken");
    }
    public static void main(String[] args)
    {
    method(); //(2)
    try
    {
    System.gc();
    Thread.sleep(5000); //pause so that the object is garbage collected before execution ends.
    }
    catch(Exception e)
    {}
    }
    }

    The addition of (x) will make the object eligible for garbage collection before the call to sleep. So the object can be destroyed anytime during the execution of the method().

    ____________________________________________
    Priya,
    Cooking is Fun | Eat Healthy Stay Fit | Sweets | Raita | Bread | Dal

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    You don't "set an Object to null". When you do var = null, what you are doing is "erasing", or removing, that reference to the Object. The Object itself still exists, but that reference to it no longer does. Once all references to an Object are removed, it will be Garbage Collected.

Similar Threads

  1. Garbage collector and its impacts
    By RadhaJayalakshmi in forum Advanced Java
    Replies: 1
    Last Post: 07-23-2008, 12:56 PM
  2. Object Reflection: Setting value
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:13 PM
  3. what happen if an object set to null
    By SaYuNaRa in forum New To Java
    Replies: 5
    Last Post: 04-17-2008, 05:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •