• 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: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are a number of labels in the source code below. These are labeled a through j. Which label identifies the earliest point where, after that line has executed, the object referred to by the variable first may be garbage collected?
class Riddle {
public static void main(String[] args) {
String first, second;
String riddle;
if (args.length < 2)
return;
a: first = new String(args[0]);
b: second = new String(args[1]);
c: riddle = "When is a " + first;
d: first = null;
e: riddle += " like a " + second + "?";
f: second = null;
g: System.out.println(riddle);
h: args[0] = null;
i: args[1] = null;
j:
}
}
Select the one right answer.
d:
e:
h:
i:
j:
There were no answer for this question in mock exam. Can anyone tell and explain the answer?
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IMHO,
The object referred to by first gets garbage collected at line d).
first is the only string that is referring to it.At d) first is assigned a null reference and hence it no longer has the reference to the object to which it originally referred and hence the object gets garbage collected at step d).
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(Be carefull: what you are about to read is NOT CORRECT
The variable first holds a reference to an object. Then it is used to create riddle. The riddle variable also gets a reference to the same object in line c. Setting first to null does not get rid of the reference in riddle, which is held until the end of the method.
At the end of the method all of the variables for the method are discarded so there are no more references to the object. At that time it is eligible for the gc. So the answer should be j.
(Cindy, Cindy, Cindy. . what were you thinking??? (hangs head in sadness) . . .)
[This message has been edited by Cindy Glass (edited March 09, 2001).]
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Cindy Glass:
The variable first holds a reference to an object. Then it is used to create riddle. The riddle variable also gets a reference to the same object in line c. Setting first to null does not get rid of the reference in riddle, which is held until the end of the method.


Oh boy... i am confused now because I thought that sai was correct. Is not a NEW string object created when riddle is created (because of the "when is a" +). Strings are immutable (pounding this into my own head) so riddle cannot point to the same object as first but must point to a different object.
Help!
Lisa
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Garbage Collection always confuses me. But that does not stop me from sharing with you my understanding. Please correct me if I am wrong.
c: riddle = "when is a" + first
creates a new String object and the variable riddle holds a reference to this new object.
The assignment of null reference to variable first makes the object refered by first "eligible for Garbage Collection", but there is no guarantee that "which" thread will execute garbage collection and "when".
It could even be possible that garbage collection will not take place at all!(because the main method complete execution and JVM might exit!). However, if garbage collection takes place, it is not necessary that the object should be garbage collected in the same order it was made eligible for garbage collection. I could be garbage collected in any order!
I think this is why there was no answer for this question.
What do you think?
 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i thought sai was correct as well.
on line "c", isn't a new string object created containing
the string "when is a ***" with the reference riddle
referring to it? (leaving the String referred to by "first"
just hanging around doing nothing?)
 
John M. Gabriele
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i know this is a few days old but i'm still stumped on it.
"Which label identifies the earliest point where, after that line has executed, the object referred to by the variable first may be garbage collected?"

I know Cindy posted the (correct, i assume) answer but i still
don't get it. Something funny seems to be going in in line c.
Help..?
thanks.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very interesting discussion. Here are my two cents -
Look at line c:

c: riddle = "when is a" + first

Here the result of concatenation operation is a new string. I strongly feel that the content of "first" string used to build the new string, not the reference itself.
Java internally uses StringBuffer for string object concatenation since they are mutable and easy to handle. Here is an extract from API documentation for java.lang.StringBuffer class -


String buffers are used by the compiler to implement the binary string concatenation operator . For example, the code:
x = "a" 4 "c"

is compiled to the equivalent of:

x = new StringBuffer().append("a").append(4).append("c").toString()

which creates a new string buffer (initially empty), appends the string representation of each operand to the string buffer in turn, and then converts the contents of the string buffer to a string. Overall, this avoids creating many temporary strings.


Clearly the new string created is not made of references to old string objects. ie., in our example "riddle" is not made of two reference pointers - one pointing to "when is a" and another refering to first. Instead "riddle" points to a whole new string "when is a first" and has nothing to do with our original "first" String object.
To test my argument simply print the "riddle" immediately after assigning "first" to null ie., just after line d. If indeed "riddle" was referring to "first", it should print "When is a (null) like a ...?". But that doesn't happen, which means "riddle" was a whole new object that was built using the content of the "first" variable, not the reference of the "first" variable.
After line "d" there are no active references pointing to the "first" variable and hence it is the earliest point at which the object referred to by "first" is eligible for garbage collection.

Hope this clarifies your doubts( if not ask for more )
------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
IBM Certified Developer - XML and Related Technologies, V1.
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Exactly!
Ajith has given good explanation.
in line c riddle points to new string object which has been concatinated and the resulatant string is assinged to riddle
After this first is never used and immediately set to null at d:
For me its very clear now
Thanks Ajith
Siva
 
John M. Gabriele
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the reply Ajith. great argument.
i didn't notice this until now but what about arg[0]?
even after line d:, it still thinks it's referring
to a string object. what if, after line d:, i want
to do something like System.out.println( args[0] ); ?
if the object referred to by first is garbage collected,
the system would've screwed me. correct?
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
John you're right. Even after line "d" the args[0] is still pointing to a valid string object. The reason is simple. Just like the new string that was created using the concatenation operator, the new String(args[0]) creates a whole new String object that has the content of args[0]. This means we now have two 'identical' string objects in memory. One is being pointed to by the reference "first" and the other one is being pointed to by "arg[0]".
Setting "first" to null in no way affects "args[0]" since they are two different objects. Try printing args[0] after line "d" and see what happens.
The point here is not to get confused between objects and references. An object can have any number of references. Some classes has overloaded constructors to help you achieve some kind of "copy construction". They are just convenience methods. However when you use new() keyword to create an object, Java creates a totally new object. The object created and the object that was used as an argument to the new() operator are two different objects eventhough their initial content may be the same.
I hope I have not confused you folks even more. The best way to master this concept, in my opinion is to write programs like this and play with it
Good luck,
------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
IBM Certified Developer - XML and Related Technologies, V1.
 
John M. Gabriele
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
whoops.
right you are!
thanks again Ajith. I've got it straight now.
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So let me just be clear on this...
The common concensus here is that Cindy is incorrect, the answer should be d?
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You would think, after all the times that I have explained that concatenating two Strings creates a new String object, SURELY I would not make that silly mistake. (Cindy slowly shakes head . . )
Just to be perfectly clear - Cindy was wrong. The answer should be d.
Perhaps it was early dementia??? Perhaps a long vacation is in order??? At any rate THANK YOU Ajith for clearing up the confusion that I caused.
 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cindy
Have you being doing too much celebrating of your 1000th post?
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome Cindy
 
Do you pee on your compost? Does this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic