• Post Reply Bookmark Topic Watch Topic
  • New Topic

How setting an Object to null help Garbage Collection?  RSS feed

 
Priyanka Dandekar
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Sachin Joshi
Ranch Hand
Posts: 83
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ankit Garg
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


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-


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().
[ August 07, 2008: Message edited by: AnkitJi Garg ]
 
Maurizio Nagni
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just to make clear the point. Whe you write

Object item = new Object();

you are making item referencing to the created Object

when you write

Object itemCopy = item;

you are referring another object to the previously created object. Note that item and itemCopy are links to one object, that is they are not the object!

Now, suppose you progam runs for 100 years, even if you DO NOT use both item or itemCopy for 100 years the garbage collector will never throw away the object because item and itemCopy refer to it.
To authorize the garbage collector to destroy the object it should be necessary that
1) the virtual machine is going out of memory then the garbage collector try to clean something
2) garbage collector can delete the object previously created only if
item = null;
intemCopy = null;

in this situation no link refer to the previously created object so the garbage collector is authorized to take back its memory space. Anway remember: the garbage collector runs ONLY when IT WANTS to run
 
Ankit Garg
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the final verdict that can be made is that whenever you don't need an object any more you must set it to null. This is applicable if you still have a lot of working to do.
Eg.
method()
{
SomeClass o = new SomeClass();
:
:
o = null; //no need as the object will be eligible for garbage collection any way
}

but here
method()
{
SomeClass o = new SomeClass();
:
:
//o is not needed any more
o = null; //good as you still have a lot of processing to do
:
://a lot of processing
}
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also,
To emphasise the point, one needs to remember that garbage collection cannot be forced by calling System.gc().

What calling System.gc() does is to give a chance for the garbage collector to collect as much memory it can depending on how much opportunity the GC has to do it. In Ankits' code you increase the opportunity to collect by putting the current thread to sleep.
 
Sachin Joshi
Ranch Hand
Posts: 83
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well said....System.gc() looks very tempting to programmers and many people think its helping runtime to manager memory.

Well sometimes ...it may...most of the times GC algorithms are smart enough to figure out when to do cleanup...

A GC cycle may be exhaustive and may slow down your application for that duration when its on go.... We have observed it for some cases...

The new JVMs have some more -XX options to play around with GC algorithm so try those options before you think of using System.gc() call.

Here is some good info about HotSpot JVM options
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A GC cycle may be exhaustive and may slow down your application for that duration when its on go.... We have observed it for some cases...

I am unclear what you are tyring to say here.
But there is no GC cycle as such.
And how have you observed it?

The new JVMs have some more -XX options to play around with GC algorithm so try those options before you think of using System.gc() call.

The -XX options requires more expertise and experience than the System.gc() call.

And I think there is no harm in calling the System.gc(), because you only request GC. Its not certain that GC will happen.
[ August 11, 2008: Message edited by: Amit Ghorpade ]
 
Sachin Joshi
Ranch Hand
Posts: 83
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am unclear what you are tyring to say here.
But there is no GC cycle as such.
And how have you observed it?

The generational GC has two types of cycle minor and major...

When the new generation fills up, it triggers a minor collection, in which surviving objects are moved to the old generation. When the old generation fills up, it triggers a major collection, which involves the entire object heap.

There are JVM options which can be provided to print verbose GC logs....this tell you when a Major or minor GC is run and how much time it took.

Read here about Major and Minor collection of GC Generational Garbage Collection


The -XX options requires more expertise and experience than the System.gc() call.


Certainly, performance tuning does require expertise!

And I think there is no harm in calling the System.gc(), because you only request GC. Its not certain that GC will happen.


When you call System.gc() its not guaranteed that it will be called immediately, but it will do a major collection whenever GC is called

yes System.gc() is certainly easy to use,that does not make it the best solution for improving performance.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ankit G@rg:

method()
{
SomeClass o = new SomeClass();
:
:
//o is not needed any more
o = null; //good as you still have a lot of processing to do
:
://a lot of processing
}


I'd rather say that that method is likely too big and should be splitted into at least two smaller methods. Which would likely solve the problem without having to explicitly set a reference null.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!