• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

help scjp5.0 question

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class CardBoard {
Short story = 5;
CardBoard go(CardBoard cb) {
cb = null;
return cb;
}
public static void main(String[] args) {
CardBoard c1 = new CardBoard();
CardBoard c2 = new CardBoard();
CardBoard c3 = c1.go(c2);
c1 = null;
// do Stuff
} }

When // doStuff is reached, how many objects are eligible for GC?

0

1

2

Compilation fails.

It is not possible to know.

An exception is thrown at runtime.

in the above example the ans is C(2 objects are available for gc)
that i understood but the explanation given in the exam is this
C is correct. Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible.

can you explain
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The object referenced by c1 has a reference to another object of type Short.

at c1 = null, object referenced by c1 becomes eligible for gc, since this object has reference to another Short object, the second object automatically becomes eligible for gc.

That is why in total 2 objects are eligible for gc.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nikunj, please quote your sources.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the answer should be: there is 1 object eligible for garbage collection. In this code the object Short story = 5; is not eligible for garbage collection because it's creation is optimized by the Java compiler. Remember that values between -128 and 127 are shared by Byte, Short, Character and Integer objects. You can easily check this concept and the answer to this question:


And for the above code the answer should be: one object is eligible for garbage collection.

However in this example there are 2 objects eligible for garbage collection:


[ November 17, 2007: Message edited by: Jan Nowak ]
 
Mohit Jain
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its true that Java compiler optimizes the Wrapper objects and a given reference points to an existing object with same primitive value instead of creating new one, however, in this case,

Short story = 5;

is going to create a new Short object as their is no other Short object with same value that exists earlier.

Further, object pointed by "story" itself depends on object pointed by "c1".
Thus when "c1" becomes eligible for gc, object "story" automatically becomes eligible for gc.
Hence in total 2 objects are eligible for gc.
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so why isn't c3 elligable for garbage collection ?

c3= c1.go(c2);

c1.go(c2) returns null

so isn't that the same as
c3 = null ?
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Robert Elbourn:
so why isn't c3 elligable for garbage collection ?



Because c3 was never assigned an object in the first place.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Robert Elbourn:
so why isn't c3 elligable for garbage collection ?

c3= c1.go(c2);

c1.go(c2) returns null

so isn't that the same as
c3 = null ?



I think c3 is eligible too. I wait a response from experts.
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

First at line 1 a new CardBoard() object is created in heap and assigned to c1. Remember c1 is just a reference to the new object. During the process of this object creation as class CardBoard as a instance variable
Short story = 5; and since story is a reference to a wrapper object another new Short object is created and its assigned to story. So after line 1 executes two objects are created c1 referes to CardBoard object and its instance variable story refers to Short object.
So we now have 2 objects in heap.
After line 2 executes c2 refers to a new CardBoard object and this time its instance variable story is not instantiated but instead refers to the same object that was refered by c1.story. Remember Wrapper objects are immutable and to save memory JVM will use the same wrapper objects for
Boolean,
Byte,
Short & Integer [ -128 to 127]
Character
Since here 5 is well within the above range a new object is not created.
So after line 2 there will be three objects in heap.
c1 ---> CardBoard object 1
c1.story ---> Will refer to Short(5) object.
c2 --> CardBoard object 2
c2.story ---> Will refer to same Short(5) object as above.

Now at line 3
CardBoard c3 = c1.go(c2);
In the above call to go() c2 is passed and its assigned to cb [Pass by value]. This will assign cb to null and will not to anythign to c2.
The method go() returns null that is assigned to c3 reference . Remember @ line 3 we are simply creating a CardBoard reference but not instantiaing any object and this reference is now assigned to null.
After line3 the objects in heap are
c1 ---> CardBoard object 1
c1.story ---> Will refer to Short(5) object.
c2 --> CardBoard object 2

And the objects available for GC are
None

Remeber c2.story was never instantiated to a new Short object it was just a reference to c1.story as explained above.
After line 4 c1 is assigned to null and hence discarding any live references to Carboard object 1. So after line 4

objects in heap are
c1.story ---> Will refer to Short(5) object.
c2 --> CardBoard object 2

And the objects available for GC are
c1 --> CardBoard object 2

Even though there are no references to the Short(5) object created by c1.story it is still not eligible for GC as JVM will continue to keep this object in memory [Wrapper object poool , just like String object pool]

So the answer is 1 object are eligible for GC , the object that was referenced by c1.

Phew !!!
Thanks
Deepak
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jan Nowak:
I think the answer should be: there is 1 object eligible for garbage collection.



Ah, I missed this thread earlier somehow. Yes, Jan is absolutely right. Only one object is eligible for garbage collection here, because the second Cardboard instantiation will indeed assign story the same Short object as in the first Cardboard instantiation. The correct answer is that only one object is eligible for garbage collection.

The original poster did not cite a source for the problem, but I believe this comes from K&B, which unfortunately also incorrectly states that there are two eligible objects. To rectify this, the K&B errata list changes the short assignment statement to "Short story = 200;".
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have seen this question in Bert Bates book. I feel the answer should be 1 and not 2 objects.
Associated Short object is not available for GC because JVM will not make this available for GC inorder to save memory. If Short object was available for GC then a next instantiation of Short s = 5. would again have to create a new Short object then where is the question of saving memory. I think it works same as Strnig pool.
Kathy can you please suggest what happens here.
Thanks
Deepak
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Deepak Jain:
I have seen this question in Bert Bates book. I feel the answer should be 1 and not 2 objects.



Hi Deepak,

This problem has been discussed before. The answer given in K&B is incorrect, since they too forgot that the Short object will be reused. It should be one object, like you said. As I mentioned above, the official K&B errata list changes the statement to "Short story = 200;".
 
Robert Elbourn
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kelvin Lim:


Because c3 was never assigned an object in the first place.



thanks!
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1 public class CardBoard {
2Short story = 5;
3
4CardBoard go(CardBoard cb) {
5cb = null;
6return cb;
7}
8
9public static void main(String[] args) {
10CardBoard c1 = new CardBoard();
11CardBoard c2 = new CardBoard();
12CardBoard c3 = c1.go(c2);
13c1 = null;
14//System.out.println("c1....."+c1.story);
15System.out.println("c2....."+c2.story);
16//System.out.println("c3....."+c3.story);
17}
18 }
In the above example,line 14 & line 16 are compilation errors.because both objects are null.That objects are eligible for GC.
 
reply
    Bookmark Topic Watch Topic
  • New Topic