• 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

garbage collection

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi fellows,
This class below was posted for a garbage collection topic but I'm having question related to this:

CODE] 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
}
} [/CODE]

So.. how many object are garbage collected after //do stuff?
c1 will be, so 1 object is for sure... but will "story" count also? Will "story" be garbaged collected? stay tuned ...

Thanks,
Valentin
[ November 11, 2006: Message edited by: Valentin Mone ]
 
Valentin Mone
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually my question is ... what is the thing with primitives pool?
Short x=5; will be created on the heap? or some pool just like String does?
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer for this question has been nicely expalined by Bu in one of the previous threads.
Any way answer is 2.
The reason is at the time the execution reaches //doStuff
The 2 references c1 and c3 will be nullified.c3 is not pointing to any object on the heap.c1 is refering one object of CardBoard type.
Total elegible object for the GCed is 2 (One cardboard type and other is story object( Short story = 5;this will also be GCed as there is no live object reference to refer to object originilly pointed by c1)).
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sanjeev Kumar Singh posted November 11, 2006 06:15 AM

The answer for this question has been nicely expalined by Bu in one of the previous threads.




I found it not so nicely.

Because Valentin's pool question still remains.
Not the first question, see below: older thread.



I don't know, how many objects were eligible, if we made another CardBoard an null it also.

To make the problem clearer, I made the code a bit easier:


At line one, two objects are eligible, the first simple card board and its associated Short object with the value 5.
But how many are eligible on line two?

The second card board of course, but no Short object, as the second card board only has a reference to the 5 in the pool???

Anyone?


Yours,
Bu.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys -

This one's in the errata, make story = 200;
 
Valentin Mone
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bert,
can you be more specific? as I don't know what you mean...
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mone,
Actually, it goes like this...



The above relation is because, Wrapper objects with values greater than 127 and less than -128, are unique so the == fails. The same is true only for Character, Byte, Short, Integer and Long. This behaviour is NOT seen in Floating point literals i.e. Double and Float.
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you check the errata page, listed at the top of this forum, you'll see that:

Short story = 5;

should be changed to:

Short story = 200;

That way we don't have to worry about the wrapper pool - which really wasn't supposed to be a part of the question. The bottom line is that the real exam won't test you how the wrapper pool or the String constant pool interacts with the G.C. On the real exam, G.C. questions will always use non-String and non-wrapper objects.

hth,

Bert
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bert Bates posted November 11, 2006 05:42 PM

The bottom line is that the real exam won't test you how the wrapper pool or the String constant pool interacts with the G.C. On the real exam, G.C. questions will always use non-String and non-wrapper objects.



That's good news. So Valentin's and my following question have been posted to the wrong forum then?



Yours,
Bu.
[ November 12, 2006: Message edited by: Burkhard Hassel ]
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI friends,
Sorry for asking a stupid question in between but i dont understand what is Short story=5;....is it an object of type short? Because when i was trying this code,it couldnt get compiled..& as far as I know for creating an object of type Short; the correct syntax is Short s=ew Short(4);...can you please help me out in knowing that
 
Valentin Mone
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I guess you are not using jdk 1.5. Autoboxing is a new feature that allows you to write code like that; Beyond the stage there is a "new" happening...but it's hidden.
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Originally posted by Bert Bates :

The bottom line is that the real exam won't test you how the wrapper pool or the String constant pool interacts with the G.C. On the real exam, G.C. questions will always use non-String and non-wrapper objects.



This is fine. But now since the question is raised about how GC works with wrapper pool objects, lets come to a conclusion for it as to what should be the correct answer in this case. Just for the sake of understanding.

Lets take the example modified by Bu



Originally posted by Bu.

At line one, two objects are eligible, the first simple card board and its associated Short object with the value 5.
But how many are eligible on line two?

The second card board of course, but no Short object, as the second card board only has a reference to the 5 in the pool???



If we go by the understanding that Wrapper object Short will be using the object from the pool than at line one there should be only 1 object for GC ie of card board. Short object should not be eligible for GC since it still have an active reference from sc2.
Thus at line two there would we 2 objects for GC, one of card board and other of Short.

Bert please let us know whether the references for Short object in sc1 & sc2 will share a common object from the pool or not.


regards
Saurabh
 
reply
    Bookmark Topic Watch Topic
  • New Topic