Results 1 to 4 of 4
- 10-06-2008, 06:29 AM #1
Member
- Join Date
- Oct 2008
- Posts
- 31
- Rep Power
- 0
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]
- 10-06-2008, 06:30 AM #2
Member
- Join Date
- Oct 2008
- Location
- US
- Posts
- 58
- Rep Power
- 0
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 SeekerHave fun....
JAVA FAQs
- 10-06-2008, 06:35 AM #3
Member
- Join Date
- Oct 2008
- Posts
- 68
- Rep Power
- 0
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)
{}
}
}
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)
{}
}
}
____________________________________________
Priya,
Cooking is Fun | Eat Healthy Stay Fit | Sweets | Raita | Bread | DalCheers,
Eat Healthy Stay Fit
- 10-06-2008, 08:35 AM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,568
- Rep Power
- 15
Similar Threads
-
Garbage collector and its impacts
By RadhaJayalakshmi in forum Advanced JavaReplies: 1Last Post: 07-23-2008, 12:56 PM -
Object Reflection: Setting value
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 09:13 PM -
what happen if an object set to null
By SaYuNaRa in forum New To JavaReplies: 5Last Post: 04-17-2008, 05:15 AM
Bookmarks