aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Quick Q on String and GC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Quick Q on String and GC" Watch "Quick Q on String and GC" New topic
Author

Quick Q on String and GC

Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
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?


giddee up
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
yes you are right.


SCJP2. Please Indent your code using UBB Code
Archana Annamaneni
Ranch Hand

Joined: Jan 29, 2003
Posts: 147
Jasper or Jose,
Could any one of you pleass explain the answer clearly.
How the answer become 1.
Archana
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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

Joined: Jan 10, 2003
Posts: 284
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

Joined: Jan 29, 2003
Posts: 147
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

Joined: Jan 10, 2003
Posts: 284
String literals might not be referred to directly on the exam, but GC is in a big way potentially.
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
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

Joined: Jul 03, 2001
Posts: 2120
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

Joined: Jan 10, 2003
Posts: 284
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

Joined: Jul 03, 2001
Posts: 2120
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

Joined: Jan 10, 2003
Posts: 284
finally, i get it... .. thanks very much again Jose.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Quick Q on String and GC
 
Similar Threads
GC Question
question with GC
String object : Garbage Collection
Can String objects be GCed?
Garbage Collection