• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Quick Q on String and GC

 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
today's question on JDiscuss was as follows...
How many objects will be eligible for GC just after the method returns?
public void compute(Object p)
{
Object a = new Object();
int x = 100;
String str = "abc";
}

the answer as i thought is 1.
but i was wondering, if the last line was changed to String str = new String("abc");
that would mean there are two objects available for GC after the method returns?
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes you are right.
 
Archana Annamaneni
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jasper or Jose,
Could any one of you pleass explain the answer clearly.
How the answer become 1.
Archana
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Archana, string objects computed from compile constant expressions (JLS 15.28) (*) are not normally eligible for g.c. But do not worry about that because the exam will not test it. You will be asked only about the eligibility of "normal" objects, those created with "new".

(*) mainly string literals
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Archana - there is a way to make a string object...like this:
String str = "abc";
this creates one reference variable (str) which points to a String literal ("abc") which is thrown into the "String pool". The String pool is not garbage collected. It is in non-normal memory. Normal memory is garbage-collectable.
Now here's a funny thing. There is another way to create a String object... except that this way creates TWO string objects!
String str = new String("abc");
Here, there is still ONE reference variable (str), but TWO objects. One is created in the "String pool" in non-normal memory, and ONE IS CREATED IN NORMAL MEMORY. So this way, there IS a String object that is created that will potentialy be available for garbage collection if later on no references point to it.
As the first technique for creating a String is used in the original question code, there is no String object that may be eligible for garbage collection....
So only Object A = new Object is creating an object which becomes available for garbage collection.

[ March 01, 2003: Message edited by: Jasper Vader ]
 
Archana Annamaneni
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You Jasper and Jose.I got it.
After I went thoough some other posts I reliazed that String literals are not on the exam for GC.
I am wondering why there are so many posts on GC which exclusively relates to Stirng literals.
Anyway I am cleared
Archana
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String literals might not be referred to directly on the exam, but GC is in a big way potentially.
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually here is something i am wondering about in the Kathy_Bert_book...in the last code block on page 434...
String now = d2.toString(); //is in a method
when the method returns, the book says that the String object will be eligible for garbage collection. But it looks like the String has been made in the technique of...
String now = "abc"; //which does not make an object that is available ever for G.C. ?
my idea is that if String now was constructed this way...
String now = new String(d2.toString());
then there WOULD be a String object eligible for G.C. when the method returns.
any thoughts/help/explanations?
thanks in advance
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In order to be sure you should post the whole example. As a guidance: if d2 points to a string literal object then that object is not g.ced after the method returns. If d2 points to another type of object toString() will return an object that might be g.ced
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thankyou Jose! so toString() returns an object?
hold on, as you say, i should post the whole code...okay then...

The text says that as the object d2 is returned and assigned to d, it is not ready for G.C. ... but the String now is ready for G.C. ... but to me, it looks like it is constructed in such a way that it is placed into the String pool, rather than garbage collectable memory. as opposed to the way of making a string by typing
String now = new String("abc");
[ March 02, 2003: Message edited by: Jasper Vader ]
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, no. Because d2 is not a String the method toString() will create a new one string to return it. This string is g.ctable after the method returns.
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
finally, i get it... .. thanks very much again Jose.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic