File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection question from OCP Java SE 6 Programmer Practice Exam

 
Ted North
Ranch Hand
Posts: 200
1
Java Python Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 451
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i2 is a variable refering to null, so it will throw a null pointer exception.
 
Helen Ma
Ranch Hand
Posts: 451
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 200
1
Java Python Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ted North
Ranch Hand
Posts: 200
1
Java Python Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 200
1
Java Python Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic