aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection question from OCP Java SE 6 Programmer Practice Exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection question from OCP Java SE 6 Programmer Practice Exam" Watch "Garbage Collection question from OCP Java SE 6 Programmer Practice Exam" New topic
Author

Garbage Collection question from OCP Java SE 6 Programmer Practice Exam

Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 193
    
    1

Hi again JavaRanch,


I have another question I was hoping you all could help me with:
7.g Although difficult to prove, write code that creates instances of a class that are referred to by other instances, and yet are still eligible for garbage collection.


Does this program work from SCJP 6 page 259 for this?



Shouldn't the System.out.println(i2.i.toString()); statement print something, assuming the first S.O.P. is commented out, since it is an 'island' on the heap according to the graphic on page 260? What am i missing here? 0_o
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
i2 is a variable refering to null, so it will throw a null pointer exception.
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
You may want to take a look at the following posts for garbage collection topics related to the exam:
1. Garbage Collector posted on 2/27/2012
2. Garbage Collection problem-2 posted on 6/15/2010
3. Garbage Collection posted on 2/27/2012

Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 193
    
    1

Helen Ma wrote:i2 is a variable refering to null, so it will throw a null pointer exception.


I was curious about reference variable i2.i though...

I see why a null pointer exception is thrown for reference variable i2, but according to the diagram mentioned in my other post these other reference variables should still be alive on the heap, but I guess unreachable.

Thanks for the links to the other posts on gc, im gonna check these out.

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3679
    
  16
Ted North wrote:
Helen Ma wrote:i2 is a variable refering to null, so it will throw a null pointer exception.


I was curious about reference variable i2.i though...

Dereferencing i2 in any way (calling a method on it, accessing a variable) will cause a NPE.

Ted North wrote:I see why a null pointer exception is thrown for reference variable i2, but according to the diagram mentioned in my other post these other reference variables should still be alive on the heap, but I guess unreachable.

When that NPE is thrown your program is going to exit, so there will be nothing alive on the heap.


Joanne
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 193
    
    1

Joanne Neal wrote:
Ted North wrote:
Helen Ma wrote:i2 is a variable refering to null, so it will throw a null pointer exception.


I was curious about reference variable i2.i though...

Dereferencing i2 in any way (calling a method on it, accessing a variable) will cause a NPE.

Ted North wrote:I see why a null pointer exception is thrown for reference variable i2, but according to the diagram mentioned in my other post these other reference variables should still be alive on the heap, but I guess unreachable.

When that NPE is thrown your program is going to exit, so there will be nothing alive on the heap.


What if I comment-out i2 though? I still receive a NPE from i2.i but according to the diagram I should not. :/ 0_o
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3679
    
  16
Ted North wrote:What if I comment-out i2 though? I still receive a NPE from i2.i but according to the diagram I should not. :/ 0_o

i2 is null. Using it to do anything will cause a NPE.

7.g Although difficult to prove, write code that creates instances of a class that are referred to by other instances, and yet are still eligible for garbage collection.

The reason it is difficult to prove is that you can't access the object that i2 used to refer to from your code. If you could, then it wouldn't be eligible for garbage collection, because there would be a reachable reference to it.
The reason the object is eligible for garbage collection is because the garbage collector can work out that, even though the object that used to be referred to by i4 has a reference to the object that used to be referred to by i2, that reference is not reachable by your code (because there is no variable referring to the object that used to be referred to by i4).
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 193
    
    1

Joanne Neal wrote:
Ted North wrote:What if I comment-out i2 though? I still receive a NPE from i2.i but according to the diagram I should not. :/ 0_o

i2 is null. Using it to do anything will cause a NPE.

7.g Although difficult to prove, write code that creates instances of a class that are referred to by other instances, and yet are still eligible for garbage collection.

The reason it is difficult to prove is that you can't access the object that i2 used to refer to from your code. If you could, then it wouldn't be eligible for garbage collection, because there would be a reachable reference to it.
The reason the object is eligible for garbage collection is because the garbage collector can work out that, even though the object that used to be referred to by i4 has a reference to the object that used to be referred to by i2, that reference is not reachable by your code (because there is no variable referring to the object that used to be referred to by i4).


Hi Joanne,

I think I get it. The instance variables that refer to objects of the same class can not be reached without the class object references.


Does this code then fit the bill for 7.g exam preperation question?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collection question from OCP Java SE 6 Programmer Practice Exam