Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Question about Garbage Collection

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How many objects are candidates for garbage collection by the end of the following code sinppet:
1.String s = "Hello";
2.s = "Hello" + " World";
3.System.out.println(s.toUpperCase());
A) 1
B) 2
C) 3
D) 4
E) none

This answer is B.
Can somebody explain why? Thanks
 
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jo Liang:
How many objects are candidates for garbage collection by the end of the following code sinppet:
1.String s = "Hello"; //1
2.s = "Hello" + " World";//2
3.System.out.println(s.toUpperCase());//3
A) 1
B) 2
C) 3
D) 4
E) none

This answer is B.
Can somebody explain why? Thanks


Hello Jo,
I think the answer shd be 'a' ie. only 1 object...
Have a look at code sequence...(to which i have given number)
1. at line 1, String literal "Hello" is created in literal pool
2. at line 2 Compiler sees that "Hello" is already present in literal pool. so it doesn't create it. Only String literal"World" is created
in pool
Now at runtime, because + operator, a string object is created on heap. ie. String object containing "HelloWorld"
3. Now at line 3, because of function toUpperCase() new string object containing "HELLOWORL" is created.
Now s is refering to the above object . so the previous object (containing "Hello World") is unreferenced now. so it can be garbage
collected.
Here 2 obejct in string pool and 2 in heap r created. and s refers to one of the heap object
so only one remains for garbage collection. String literals r never garbage collected.
Correct me if i am wrong
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, given the information in the assignment, this is just a snippet, so the code doesn't end there. That means that the text "HelloWorld" still has a reference to it. However, the "Hello", and the uppercase of "HelloWorld" does not (we're only printing the uppercase version, not changing any references. Remember: String is imutable).
/Mike
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,Mike...
i agree with u but one more question:
what about the "world" obj?
i think it also should be gc.
am i right?
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,,,
simoen u r perfevtly all right.....
------------------
Muhammad Hussain
 
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are they really garbage collected.
They are String literals and according to our discussion String literals are not garbage collected.
So my ans would be none.
Please correct me
-Neha
 
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
2 is the correct answer because:
s = "Hello" + " World"; is going to create a new String dynamically and s.toUpperCase as well. So we end up with to dynamically created Strings which are to be gced !
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Ranch Hand
Posts: 2120
  • 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.
"hello" + "world" is computed at compile time because is a constant as defined by JLS 15.28. So is not garbage collected either. Yes the string retuned by to UpperCase is ready for collection.
Many mocks doesn't actually take into account the fact that String literals are not g.c.ed . Just try answering as if they were normal String. In the exam I don't think we will be asked for the collection of String literals.
Use a WeakReference object in case you are interested in checking that String literals are not g.c.ed . Search the Ranch for my name and you will see previous examples.
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
then the answer will be A.
Since s.toUppercase() has no reference it will be gced.
Am i right
-neha
 
Valentin Crettaz
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
Sorry! Jose is right
s = "Hello" + " World"; is a compile-time constant and thus is not gced...
my mistake!
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jose Botella:
String literals are not garbage collected.
"hello" + "world" is computed at compile time because is a constant as defined by JLS 15.28. So is not garbage collected either. Yes the string retuned by to UpperCase is ready for collection.
Many mocks doesn't actually take into account the fact that String literals are not g.c.ed . Just try answering as if they were normal String. In the exam I don't think we will be asked for the collection of String literals.
Use a WeakReference object in case you are interested in checking that String literals are not g.c.ed . Search the Ranch for my name and you will see previous examples.


Perfect , i agree with Jose
 
reply
    Bookmark Topic Watch Topic
  • New Topic