• 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

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which is the earliest line in the following code after which the object created on the line marked (0) will be a candidate for being garbage collected, assuming no compiler optimizations are done?
public class Q76a9 {
static String f() {
String a = "hello";
String b = "bye"; // (0)
String c = b + "!"; // (1)
String d = b;
b = a; // (2)
d = a; // (3)
return c; // (4)
}
public static void main(String args[]) {
String msg = f();
System.out.println(msg); // (5)
}
}
a) The line marked (1)
b) The line marked (2)
c) The line marked (3)
d) The line marked (4)
e) The line marked (5)
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
STRING LITERALS ARE NOT GARBAGE COLLECTED.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good one!
I do not know.
However as bye is still being pointed to by c, none of the options seem to be right!
Anyone?
Thanks
Rahul
 
Sanjeev Gupta
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks tvs, that was helpful.
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so what is the answer to the question
what if a string object is not referenced anymore
like here the object b is not being referenced anymore
i think the answer should be c.
what does the author of the exam say about the answer sanjeev
[This message has been edited by sona nagee (edited May 05, 2001).]
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
according to me, the answer is d.
B'coz the string objects created are all within the scope of the method.Therefore as soon as the method f() returns to the main() method ie on line 4 of the code, "bye" will be Garbage Collected.Moreover variable c which is returned to main() doesnot
refer to "bye" but to a new object "bye!".
Other than the object "bye!" none of the objects created in method f() are available outside f().hence d) should be the coreect answer.
 
sona gold
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi rajshree
the question here is asking after which line will the object be eligible for garbage collection.
therefore the answer should be c
if they would have asked when will the object become garbage then d would be correct
here the question is worded differently
 
Sanjeev Gupta
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sona,
All variables are referring to string literals. As string literals are not garbage collected, all options are incorrect.
If there were an object created with the 'new' operator, that would have been garbage collected.
The question is from Khalid's free mock exam . It does not explain any answers.
 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sanjeev is 100% right.
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Folks,
Let me have the pleasure of the LONG and MISLEADING discussion in this thread.



All variables are referring to string literals. As string literals are not garbage collected,
all options are incorrect.
If there were an object created with the 'new' operator, that would have been garbage
collected.
The question is from Khalid's free mock exam . It does not explain any answers.



Sanjeev the answer is indeed from Khalids Mock and it is Question # 18 and the answer is (c) why ?? Please refer to
pp 723 of the book.
TVS Sundaram dont have that misconception. If string literal will not be garbage collected then it would have a serious DESIGN ISSUE and LOT of RESOURCES WILL NOT BE USED BY THE JVM!
Let me explain, what happens at each line of execution!

Hope my explanation suffices.

Ravindra Mohan.
 
Sanjeev Gupta
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, chapter 4 of 'Thinking in Java' clearly states that the garbage collector works only for objects created with 'new'. But Ravindra seems to be confident about his explanation. I do not have Khalid's book. I would request one of the moderators to step in and clear the things (or should I say, the 'garbage')!
 
nitin sharma
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sanjeev gupta,
U are right that string created with new are eleigible for garbage collection not the string literal's.I had a long discussion on this with one of our moderator named "JANE GRISCTI".SHE said string's created with new keyword are eligible for garbage collection not the string literal's.I had discussed the same thing with ROESANE ZHANG, a moderator OF jchq site,she also told the same thing.
I think RAVINDRA MOHAN u need to consult other's for this.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,
There are a few points here that need clearing up

  1. The JVM specifications do not say how the gc is to be implemented; it is only required to work on objects created in heap memory. String literals are stored in a constant_pool, not in the heap. See JVM §3.5.3, §2.17.7 and §5.1.
  2. Most of the mock exams ignore this and use String literal objects in the gc examples.

  3. In view of the above,

    1. It is unlikely you will see real exam questions on gc that utilize string literals. Sun does not test on features that are system dependent.
    2. When answering a mock exam question involving String literals, assume they are gc'd

    3. In the example question given, assuming string literals are gc'd; the correct answer is 'c' as at that point no reference variables contain the memory address for the string literal 'bye' created in line 0.
      Hope that helps.

      ------------------
      Jane Griscti
      Sun Certified Programmer for the Java� 2 Platform
 
Sanjeev Gupta
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Nitin and Jane, that clarifies!
 
Ravindra Mohan
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That was good clarification by Jane.
SO AS FAR AS THE QUESTION GOES "MY ANSWER" IS "CORRECT".
BUT FROM THE SCJP POINT OF VIEW WE WON'T BE GETTING
QUESTIONS THAT BRINGS AMBIGUITY. QUESTIONS LIKE THESE
IN AN ACTUAL EXAM WILL CONSIST ONLY OF OBJECTS.
IN "MOCK EXAMS" THE ANSWER SHOULD BE BASED ON THE "ASSUMPTION" THAT STRING LITERALS ALSO GET GARBAGE COLLECTED.
HOPE THIS ENDS THIS LONG DISCUSSION HAPPENING IN THIS THREAD.

RGDS,
RAVINDRA MOHAN
 
Ravindra Mohan
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[This message has been edited by Ravindra Mohan (edited May 06, 2001).]
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ravindra,
Pls don't use caps . it is loud & offensive . Remember u'r here not 2 win an argument but 2 help u'rself & help others . try 2 tone down u'r language . it would sure do u a lot of good.
PS:- i don't see how after what Jane has said u win the so called "Argument".
Best of luck,
james
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic