• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Object reference for Garbage collection

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I came across this question and answer and I don't understand the answer.
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)1
C)10
D)11
C is correct. Answer A and B are wrong and C is correct because only the last String object of the 11 created still has a reference. Answer D is wrong because even though the tmp variable is out of scope in line 8, the local variable still has a reference.
I don't understand why the local variable still has a reference at line 8. I don't understand why its not 11 or 1
Any help in understanding this would be appreciated Thanks Jason
 
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I am not trying to provide a solution but adding to more confusion here...
are local object references stored on the stack along with automatic variables? Can anybody explain this or guide me to a url where I can find such stuff
Thanks in advance
Sri
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sri Sri:
are local object references stored on the stack along with automatic variables?

Yes. The reference lives on the stack, the object they refer to lives on the heap. Really, they're not much different from an integer.
- Peter
 
Sridhar Srikanthan
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Peter,
That clears my concepts.
So, in the above question, as there are 11 String objects being created, with the same string reference variable tmp , (and I am safely assuming that the objects that tmp refers to are not added to String literal pool) , we have 11 objects eligible for garbage collection ?
my reasoning is, as tmp lives on the stack, it ceases to exist once it exits the for loop, but the objects are still on the heap without any valid references pointing to them.
Is there a flaw in my reasoning ?
Thanks in advance
Sri
 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
tmp reference keeps getting assigned to a String, 11 times, so the first ten have no reference as the eleventh String object is the last and it is referred to by tmp.
still, it is part of a loop which one would assume would mean that the tmp reference variable is no longer accessible after the loop has finished?
 
Sridhar Srikanthan
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Jasper,
Pardon me but i am not able to make out whether you are trying to answer my question or you are posing a question ?
Sri
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for what I know, the first 10 String reference objects will be lost in space.... (a strange way to tell that they are not reachable, isn't it???)
the 11 string reference object, allthough in a local variable, will be in the root set as a strongly reachable object, when the 11 string reference object gets out of the scope nothing is placed over the old reference in the root set!! In other words, even if it's out of scope it was not set to null, so there will remain a reference in the root set!!!
-------------------------------------------------
please!!! correct me if I'm wrong!!!
 
author
Posts: 9031
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jason -
I have to say I don't think this is a very good GC question. Let's break it down a little:
Let's call 'tmp' a 'local block variable'. The exam may very well test to see that you know that you can't use tmp once the 'for' loop has completed - that question would be fair game.
So we'll call that concept: 'block scope'
Now let's look at the GC portion of the question:
You will never see a question like this on the exam. The exam does not expect you to know whether objects referenced by a block variable are eligible for GC when the variable goes out of scope but before the method has ended.
From a GC perspective the exam will test you to see if you know whether there are still variables referring to an object. For example, let's say that the countDown() method returned a String, and that line 8 said:
return tmp;
Given that, how would you answer the question? Or let's say that line 8 said:
tmp = null;
And the question said 'what happens when line 9 is reached?
Then how would you answer?
Those last two examples are much less fuzzy, and those are exactly the types of questions you will be asked...
Does that help?
-Bert
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sri Sri:
Dear Jasper,
Pardon me but i am not able to make out whether you are trying to answer my question or you are posing a question ?


hey Sri, umm, thinking out loud really, sorry if it was confusing!
 
Jasper Vader
Ranch Hand
Posts: 284
  • 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:

Or let's say that line 8 said:
tmp = null;
And the question said 'what happens when line 9 is reached?
Then how would you answer?


wouldn't there be a compiler error because tmp is out of scope?
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Boy, am I confused here. So what is the answer here? I still am not convinced about 10 as tmp would not be accesible anyways at line 8 right? Please clarify...
 
Bert Bates
author
Posts: 9031
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeez you guys....
Ok - you're right, so let's ALSO say that tmp is a method variable, not a block variable...
The point is that the question as originally stated is out of scope for the cert exam. The examples I gave were to show what the real exam questions would focus on.
Sorry for the confusion... It's easy to let yourself get caught up in studying the wrong topics, and I was hoping to re-align the original question so that the focus would actually apply to the exam :roll:
 
Jasper Vader
Ranch Hand
Posts: 284
  • 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:
Jeez you guys....


haha classic . i think it is possible to actually hear the machinations of our brains as we all hover and swoop for information, any information, to find out what to do in the exam.
thanks for the subtle directives here Bert
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic