Page 1 of 1

About Question enthuware.ocajp.i.v8.2.883 :

Posted: Tue Sep 22, 2015 6:19 am
by heleneshaikh
Hello,

I have modified the question and was wondering if 'obj' is eligible for GC right after line 4? objArray[1] still points to obj, so I'm not sure. obj is now null, but still has a reference pointing to it and is therefore not eligible for GC. Am I correct?

Object obj = new String("a");
Object objArray [] = new Object[2];
objArray[1] = obj;
obj = null; //line 4

Re: About Question enthuware.ocajp.i.v8.2.883 :

Posted: Tue Sep 22, 2015 9:58 am
by admin
Yes, you are correct. Even after line 4, objArray[1] is still pointing to the object pointed to by obj.

Re: About Question enthuware.ocajp.i.v8.2.883 :

Posted: Sun Nov 15, 2015 2:10 am
by prasanna_ls
What if, instead of
objArr[0] = null;

at line 5, we had

objArr = null;

From what I understand, objArr would be eligible for GC. But not the String "aaaa" which is created at line 1. But when objArr gets GCd, the String "aaaa" will be eligible for GC. Or does the Garbage Collector have some special algorithm which enables it to detect such cases?(so that it can collect both objects at the same time)

Re: About Question enthuware.ocajp.i.v8.2.883 :

Posted: Sun Nov 15, 2015 6:37 am
by admin
You may want to read about "island of isolation" here: http://stackoverflow.com/questions/7928 ... collection

HTH,
Paul.