• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

How many reference are left?

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,every one.
I have a question here.
question:Here is a method which creates a number of String objects in the course of printing a count down sequence. When the program reaches line 8, how many of the String objects created in line 5 are eligible for garbage collection? Assume that the System.out object is not keeping a reference.
1.public void countDown()
2.{
3. for( int i = 10 ; i >= 0 ; i-- )
4.{
5.String tmp = Integer.toString( i );
6.System.out.println( tmp );
7. }
8.System.out.println("BOOM!");
9.}
a.)none
b.)0
c.)10
d.)11
I think the answer is d,but the answer is c,they explain that though out of its scope,the local variable 'tmp' keep its one reference in line 8.
I don't understand how a variable can keep its reference even out of its scope.what's the rule here?
 
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strings and GCs
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Might be that variable tmp still exists (out of the for loop) but can't be seen. It maintains its reference but you can't use it(?).
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi fei,
The Eligibility of Strings objects for Garbage Collection is beyond the scope of the exam.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi fei peng,
I agree with you. The answer should be d.
Garbage collection questions can be tricky, but I think the rule is simple.
From the K&B book:


an object is eligible for garbage collection when no live thread can access it.


If a reference to an object falls out of scope, the reference can not be used to access the object. Hence, unless there is another reference from a live thread to the object, it is eligible for garbage collection.
So, in the above code, I agree with you that on line 8, because the the variable tmp (declared inside the loop) is already out of scope, the 11th String object is eligible for GC.
Hope this helps.
 
Bojan Knezovic
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by David Hadiprijanto:
Hi fei peng,
If a reference to an object falls out of scope, the reference can not be used to access the object. Hence, unless there is another reference from a live thread to the object, it is eligible for garbage collection.
Hope this helps.



The thing is in the above example, although the reference is out of scope the thread it belongs to is still running.
 
author
Posts: 9031
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just took a quick look at our GC chapter and I think we will make a clarification in the next update! Early on, we decided to remove any examples of GCing which used String objects. But, I confess, we kind of side-stepped talking directly about String objects and the GC. We do a high level review of the String constant pool in our String section, but we never relate the two.
I believe in the next reprint we will say something like: "All of our discussions concerning the GC apply to objects that are NOT of type String. The topic of how the GC handles objects of type String is not on the exam."
I don't know if we're going to dive in to the hornet's nest of how the String constant pool is dealt with (or not), by the GC. This might be a great topic to ask Marlene about , but the key point for this forum is: It's NOT on the exam, so you can focus your exam related studies elsewhere; like on threads, inner classes, and non-String GC issues, all of which are topics that are REALLY HARD on the exam.
 
fei peng
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you all,
and thank you,Bert Bates.Your book give me great help to be ready for SCJP
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic