James Raitsev
James Raitsev

Reputation: 96391

On Java garbage collection, clarification needed

Upon new A(), we allocate sufficient amount of memory to hold all object that A contains.

Object A() may contain other objects like B() and C()

Question 1: When A() is no longer needed and you want to remove it from the heap, will setting it's reference to null do the trick? (If not, what would be the right way to signal the JVM to GC this object now?)

Question 2: If so, what happens with the instances pointing to B() and C()

Question 3: Is there a way this effect can be observed? (memory being deallocated to free up the object)

Upvotes: 2

Views: 178

Answers (7)

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

Its a thumb of rule, that when an object has NO reference attached to it, its a toast for the Garbage collector to eat.

Marking A null will, make A a toast for the GC. If the reference pointing to A, was the only reference to B and C, then both of these are too the candidate for being Garbage Collected

finalize is the object class method, that is called when an object is Garbage Collected.

Upvotes: 1

Evans
Evans

Reputation: 1599

Setting A to null will mark A as a candidate to be freed by the GC. Instances of B and C will be marked too if the only references to those instances were from instance A.

Edit about question 3: A simple way to debug this effect is using the method finalize; When an object is GC´ed, his finalize is called .

However, be careful with this method: finalize is not guaranteed to be executed always (as the GC is not guaranteed to free an object) and should never be used for application´s general purposes.

There are better debugging tools depending on your IDE. For example, in eclipse: http://www.yourkit.com/docs/80/help/garbage_collection.jsp

Upvotes: 5

Peter Lawrey
Peter Lawrey

Reputation: 533510

Object A() may contain other objects like B() and C()

Objects only contain primitives and refrences to other objects.

Question 1: When A() is no longer needed and you want to remove it from the heap, will setting it's reference to null do the trick?

It is rarely needed but you can do this.

Question 2: If so, what happens with the instances pointing to B() and C()

Nothing, they are unrelated objects.

Question 3: Is there a way this effect can be observed? (memory being deallocated to free up the object)

You can override the finalize() method or use ReferenceQueues. This only informs you about collected object rather than discard objects. Its best to avoid needing to do this.


Objects which do not have a strong reference from root context e.g. thread stacks, can be collected. It doesn't matter how this happens. Setting a reference to null can allow this to happen, but more often allowing a variable to go out of scope is a simpler way to discard an object(s).

If you have Object B and it points to C which points to B and there are no other references to these objects, they will still be cleaned up even though there are references to those objects.

Upvotes: 2

fmucar
fmucar

Reputation: 14558

If you nullify all the references to A and if there are no other live references to B and C then those will be GCed too when the GC runs.

Invoking System.gc() does not guarantee that gc will run instantly. It may or may not run, there is no guarantee at all.

And There is no guaranteed way to force gc run immediately as you request it.

Yes, you can see the effect as gc runs freeing memory.

to see visual graphs/info ... about heap/memory usage, you can use jdk tools, find it for windows at:

JAVA_HOME\bin\jconsole.exe 

Upvotes: 1

Jigar Joshi
Jigar Joshi

Reputation: 240900

If you have live reference to B and C then it won't GCed

at any point if any object doesn't have live reference to it, that object is ready to be GCed

class A{
 B b = new B();
 C c = new C();
}

now when you do

A a = new A();
a= null;//instance referred by a,b & c are ready to be collected in this system

Upvotes: 3

Lai Xin Chu
Lai Xin Chu

Reputation: 2482

  1. If you set it to null, then yes it is a signal to the GC to deallocate the memory when it needs the memory. But this will only happen if there are no more instances referencing that object.

  2. If there are still instances referencing an object, it will not be GC-ed until there are completely NO instances referencing that object. So B() and C() will be GC-ed only if A() was the only one referencing them.

  3. This effect can be easily observed by using Java to read a huge text file (about 3mb) with a Scanner, then closing and discarding the Scanner, and invoking System.gc() when you are done.

Upvotes: 1

Hans Z
Hans Z

Reputation: 4744

You cannot force GC to delete an object. GC will automatically delete any objects that are safe to be deleted. You can force GC to run by invoking System.gc(), which will delete any objects it can.

A = null will delete the object only if no other references to that object exists.

In general GC exists so you don't have to worry about deletion and memory collection. If a thing is safe to be deleted, it eventually will be.

B() and C() will be deleted if A stops pointing to them and A was the only thing pointing to them in the first place.

Upvotes: 1

Related Questions