• 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 question from J@Whiz1.4

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 contDown()
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.}
Answers:
a.none
b.1
c.10
d.11
c is correct because only the last String object of the 11 created still has a reference. Even though tmp variable is out of scope in line 8, the local variable still has a reference.
I do not understand this explanation, we cannot refer to tmp after line 8. Then why does it still have a reference, and when will it loose it?
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right in my opinion.
WARNING: the following paragraph is a a complete nonsense
The author of the question could be thinking in the fact that some VM implementations do not set out-off-scoped local variables to null automatically after they reach the end of their scope.
However this is not the case in JDK 1.4 as this program shows:
end of Warning
However the following code shows that tmp is eligible for g.c. just after leaving out the for scope.

[ October 18, 2002: Message edited by: Jose Botella ]
 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Anna Swartz:
When the program reaches line 8, how many of the String objects created in line 5 , are eligible for garbage collection?
I do not understand this explanation, we cannot refer to tmp after line 8. Then why does it still have a reference, and when will it loose it?


This one is a little tricky, if indeed the question itself says, "when the program reaches line 8..." then that is not the same as saying, 'AFTER line 8 executes..." or "When the program reaches line 9..."
It's a very misleading question, though, since when you look at it YOU know that line 8 is the last line. On the real exam there will usually be either additional code:
8. // code here
9. // assume more code here
10. } // NOW the method ends
And then if the question asks, "When you get to line 8..." you will know that this is not about local variable scope, but rather the affect of reassigning the reference over and over.
On the other hand, if they want to test your knowledge of local variable scope, then the question would say something like, "After line 10 completes..." or "After the go() method completes..."
So it will be more clear.
I agree that the way this question is worded makes it confusing, but technically when the program gets to line 8, the local variable is still not out of scope, even though you can see that there isn't any code that can access it. But to the VM, it's still eligible until after line 8 executes and the method returns (is popped off the stack).
Cheers,
Kathy
p.s. you just can't believe how many garbage collection questions could be on that test ; )
 
Anna Swartz
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still do not get it. Let me try again, my understanding was that variable defined in for loop is not reachable outside of that loop. So, once the control is out of the for loop String tmp is ready for the g.c. I even tried to access it right after the loop end or assign it a new value. It would not compile, just like I thought. So, my question is really about the scoop of the variable. I tried to redefine it as new within the method, right after the loop, and it compiles no problem. To me it looks like the scoop for tmp is the loop, not the method. Then why it is not ready for g.c. after loop ends, before method returns. I am really puzzled about that, would appreciate if somebody explains it to me.
class Untitled1 {
public static void main(String[] args){
Untitled1 u = new Untitled1();
u.contDown();
}
public void contDown()
{
for ( int i = 10; i >= 0; i-- )
{
String tmp = Integer.toString( i );
System.out.println( tmp );
}
tmp = "abc";
// String tmp = "abc"; - this will compile,
System.out.println(tmp);
System.out.println("BOOM!");
}
}
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm puzzled too. Kathy, do you mean that the for statement does not
define a scope, but just a compound or grouped statement?
That leaving the "}" does not pop any locally defined variables off the stack?

-Barry
 
Kathy Sierra
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I was answering a different question, wasn't I
... the one that says when you reach a line but it isn't yet the end of the method... blah blah blah, when the real question wasn't about method scope it was about the interior LOOP scope... [so file what I said before under 'ways the questions might be worded with respect to when a method ends...']
Then as for the goes-out-scope-before-method-ends question:
You *are* of course expected to know the scope of a loop variable, to recognize that when you can and cannot use it, but you are *not* expected to asses gc eligibility until/unless the method completes or something else is assigned to the variable.
But you will NOT be asked if something becomes eiligible while the method is still in scope but the variable itself is not. You WILL be asked if something becomes eligible for gc *before* the method ends, but **only* when the issue is about whether the variable has been assigned a new value (abandoning the reference to the object that had been *previously* assigned).
Anyway, sorry about the confusion; that question doesn't reflect what you can know with any certainty, as far as the exam is concerned.
So when I say the gc questions are tricky, it's because they're tricky to READ but in the end ALL the gc questions are about only the obvious GC things:
* method/local/automatic scope (at the method level)
* instance variable scope
* references assigned new values or null
* references passed into other methods -- and what happens if you can't see the code for the method you pass the reference too.
cheers,
Kathy
 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kathy, After reading your post, I added 2 more lines to the code.

and the opcodes obtained were

Without the lines A & B, the opcodes end at line 23. So I confirmed what you wrote (initially I was confused) - that the reference to String tmp is still retained at position 2 of the local variables until the method returns.
WITH lines 26 & 27, (i.e., int i=5 , the value 5 is being stored at position 2 of the local variables and string "2" is being stored in position 3 of the stack. Therefore JVM knows that the position 2 of the local variable stack is available for use (since 'tmp' is no longer in scope).
My Question is - why was position 1 not used? (it housed the 'int i=10' variable of the for loop).
I know this is beyond the scope of the exam but I would really like to know how the JVM was implemented. Thanks for your explanation though - set me thinking.
 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kathy, I really wonder whether Sun is testing our knowledge of the Java concepts and specifications or if we are totally awake during the exam, I mean what's the point behind having tricky questions that take your attention away from the real objective the question is about and make you look at something else? I don't see the point, maybe one knows the objective and the right answer but because the question is tricky and get a person distracted, one would answer wrong.
For me I think this is like puzzels more than an exam question.
A question like this:
for (int i=0; i<0; i++) {
// complicated ++i ~i ^i code or case switch
}
This doesn't test anything about Java, any C programer would know - if he/she notice and don't get distracted by all the complicated code - that the loop will never be executed, so what is this testing? if the candidate had enough sleep and wearing his new glasses before he goes to the exam? I'm giving this as an example of tricky questions
Why does it have to be tricky? what do you test with tricky?
 
Anna Swartz
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kathy, so what is going to happen in this last case you mentioned?
* references passed into other methods -- and what happens if you can't see the code for the method you pass the reference too.
 
Kathy Sierra
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Alfred Kemety:
Kathy, I really wonder whether Sun is testing our knowledge of the Java concepts and specifications or if we are totally awake during the exam, I mean what's the point behind having tricky questions that take your attention away from the real objective the question is about and make you look at something else?


You make a REALLY good point. I'll give you the most honest answers I can about this... but first don't shoot the messenger I am not a decision maker on the process, just a participant. But that said, the test needed to stay difficult, to keep its value up as a certification, and because of sites like javaranch and all the mock exams, etc. it has become more challenging to come up with questions that are unique, when testing the same thing. At least 10 times a day we'd look at each other and say," is it really POSSIBLE to come up with another garbage collection question?" How many times can you set x to null?
BUT... I will say that we have a distinction between "trick" questions and "tricky" questions. "Trick" questions are designed to mislead you, in ways that require you to know trivia or something you really should never have to know, and we tried desperately to avoid THOSE. If there's one thing I see that most differs between Sun and so many mock exams, it is that most mock exams test a lot more on "trivia". Its a fine line, though -- deciding what is and is not trivia. In other words, what things should you as a programmer expect to figure out from the compiler vs. things you really should just KNOW in order to have an understanding of how things work.
The "tricky" questions though, in many cases DO show code that, as I've said before, you would fire someone for writing. However, this experience will come in handy when you work for someone and your job is to fix the code written by the worst Java programmer in the universe.
-Kathy
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anna, I think we were left in mid air back there . According to The Java Programming Language 3rd Edition, James Gosling et.al, that variable tmp has gone out of scope when it left the for loop, so any object it was referring to is eligible for garbage collection. The loop was executed 11 times so there are 11 objects eligible for garbage collection. When and if garbage collection will actually take place we cannot say. But there are definitely 11 objects eligible at that point.
The given answer is wrong.
-Barry
[ October 21, 2002: Message edited by: Barry Gaunt ]
 
Vin Kris
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would have to agree with Barry. The last object assigned to variable tmp is still on the stack after the end of the loop because there is no instruction later that requires it to be over-written or popped. But if a new variable is created, then it occupies the position previously occupied by the variable tmp. Therefore, jvm should recognize that the object is eligible for GC snce it recognises the object is no longer in scope.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know this is beyond the scope of the exam but I would really like to know how the JVM was implemented.
Unfortunately, Sun does not provide the JVM sources. BUT, Kaffe does provide the source code of its JVM and it is very interesting. I checked once how they implemented the private pool in the String class and I discovered lots of things Check it out at http://moot.kaffe.org/ftp/pub/kaffe/v1.0.x-production
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kathy says:


The "tricky" questions though, in many cases DO show code that, as I've said before, you would fire someone for writing. However, this experience will come in handy when you work for someone and your job is to fix the code written by the worst Java programmer in the universe.


While working for the exam I was asking myself the same question: why tricky questions and tricky code.
Your answer is definitely a good point.
I have been a contractor for the last 14 years.
In that position you encounter the most unbelievable situations. It is mind boggling what some programmers come up with.
In those cases a well documented analysis of the state of the system is of great value.
In virtually all cases I could convince managers to dump the system and build a new one from scratch.
There was only 1 case in which the system had to go on as it was already running in production environment. This was for a big multi-national three letter company starting with an "I".
It took me 3 months just to get a handle on the system. In that case you really need to know your stuff inside out.
I hope it never happens again, but in case it happens I know I am prepared. Thanks to all these tricky questions.
 
I'm not sure if I approve of this interruption. But this tiny ad checks out:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic