• 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

gc question

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a GC-related question from a Whizlabs mock exam simulator for SCJP:



When the program reaches line X, how many of the String objects created at line y will be eligible for GC? I'm told to assume that the System.out object is not keeping a reference.

Now the answer is given to be 10, but from what I'm seeing here [probably seeing wrong ], all strings (11 in total) created at line y will be eligible for GC when execution comes to line X. Whizlabs says that 11 is wrong, because even though the tmp variable is out of scope in line X, the local variable still has a reference.

How come?
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,

i've a doubt here...

are the objects created inside a method or a for loop created on the heap???

well as far as this ques. is concerned what is happenning is...
we've 11 iterations, on each iteration the local ref. variable is assigned a new string object, so for the 11th time the prevoius 10 objects are there but they dont have any reference in the String pool, but the 11th object has a reference in the String Pool constant....

but i'm not sure that String Pool comes to play here..

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I know, we will have 11 iterations and 11 objects created on heap. Also the last object will remain on heap and others won't have any reference( so will be gone).
The question asks us to assume that System.out won't keep reference. What does it mean? I am confused.

Thanks
 
Mihai Alexe
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, how did you assume the 11th object remains on the heap? I believe that all objects or even none of the 10 that are eligible for GC can still be on the heap when line X gets executed. It depends whether the garbage collector is run or not and whether the respective objects' storage is reclaimed... I hardly see the point here... I'm asking why does the 11th object remain accessible? Where is it referenced from?


As far as System.out is concerned, it's an object that handles output to the console, so it shouldn't keep a track [i.e. reference] to all objects sent to its println / print methods - this seems reasonable, as doing otherwise would keep objects from being GC'd until System.out itself becomes eligible for garbage collection [and I'm not sure this happens at all during the program's execution...]
[ April 17, 2005: Message edited by: Mihai Alexe ]
 
Mihai Alexe
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think what you're seeing here is the ouput from the System.out.println(tmp) inside countDown(), not from the finalizer's println(). Moreover, the finalizer you wrote is for the gc class and not for the string objects constructed inside countDown()... so it misses the point . Anyway, remember that there is no guarantee the finalizer will be run - and is even less likely that all objects will be finalized (or GC'd) in the order they were created.

Don't forget to call super.finalize() inside your class's finalizer, or use a finalizer guard instead (see Josh Bloch's book for details).
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hope the whizlabs is wrong...

To justify my statement here is a modify content of above program



output is
10
9
8
7
6
5
4
3
2
1
0
BOOM!

this shows that all objects are collected by GC

by i cannot get this

I'm told to assume that the System.out object is not keeping a reference.
reply
    Bookmark Topic Watch Topic
  • New Topic