Forums Register Login

GC for String object

+Pie Number of slices to send: Send
I found this question in a mock test -
From the following code how many objects are eligible for garbage collection?
String string1 = "Test";
String string2 = "Today";
string1 = null;
string1 = string2;
A) 1
B) 2
C) 3
D) 0
The answer given in the test is 'A'.
I would expect the answer to be 'D' because the object "Test" is not garbage collected but rather put back into the pool maintained by the JVM.
Please explain.
+Pie Number of slices to send: Send
But there is no reference to String Test. So its eligible for Garbage collection.
Note : Question is asking whether eligible for garbage collection.
+Pie Number of slices to send: Send
 

Originally posted by ramki srini:
But there is no reference to String Test. So its eligible for Garbage collection.
Note : Question is asking whether eligible for garbage collection.


But String literals in the String pool are never garbaged collected. "Test" should not be eligible for GC.
+Pie Number of slices to send: Send
I agree with Vivek and Yan.
now take the follwing situation..
String s1="test"//line 1
String s2=s1.toUpperCase()//line 2
s1=null//line 3
s2=null//line 4
value hold by s1 will not be eligible for garbage collection after line 3 coz "test" is string literal.
But wat about s2.Does the value it refer at line 3 not be eligible for garbage collection after line 4.???what i mean to ask is "s1.toUpperCase()" gives string literal or string object.
thanx in advance.
regds
Arpana
+Pie Number of slices to send: Send
s1.toUpperCase() returns a new String object since it has to change the string. It will be eligible for garbage collection after line 4, when the reference "s2" no longer points to it.
However, note that "FOO".toUpperCase() does not return a new string -- it returns its argument, since it's already all-upper-case.
+Pie Number of slices to send: Send

Do u mean to say that in the above code, the value held by s2 in line 1 will not be eligible for garbage collection after line 2 coz here s1.toUpperCase() doesn't return any string object coz that string already present in string pool and both s1 and s2 refer to the same literal.
Thanx in advance.
regds
Arpana
+Pie Number of slices to send: Send
Yep!
In fact, after line 1, you will find that s1==s2.
[ November 15, 2002: Message edited by: Ron Newman ]
+Pie Number of slices to send: Send
Probably a very basic question:
Are ALL and ONLY string literals put in the string pool?
+Pie Number of slices to send: Send
String objects that are interned are placed in the String pool, if they were not already there.
Strings computed from compile constant expressions (JLS 15.28) are also placed in it. That is, any String expression made up from string literals and others compile constants:

Parsing the class file with "javap" there is a line...
"3 ldc #3 String "The integer 9223372036854775807 is mighty big."
...that shows that the compiler has identified the first string expression to print as a compile constant. At runtime the JVM will create a String object to represent this information, and it will place it in the string pool. That object will be used in the future for any reference to a string "interned" with the same content.
However the second string expression to print spanws the typical sequence of "StringBuffer" and sucessive "appends" to concatenate the strings. The resulting string is not placed in the string pool.
+Pie Number of slices to send: Send
 

Originally posted by Vivek Talyan:
I found this question in a mock test -
From the following code how many objects are eligible for garbage collection?
String string1 = "Test";
String string2 = "Today";
string1 = null;
string1 = string2;
A) 1
B) 2
C) 3
D) 0
The answer given in the test is 'A'.
I would expect the answer to be 'D' because the object "Test" is not garbage collected but rather put back into the pool maintained by the JVM.
Please explain.


I really want to know the answer of this question. I meet at a mock exam too.
+Pie Number of slices to send: Send
The real answer is 0, because string literal are not garbage collected unless the class that declared them is unloaded from the JVM.
The exam is nottesting the eligibility for garbage collection of objects pointed to by string literals.
Many mock exams, however, do not level up to this knowledge. Thus you can pretend the string literals were created with the new operator and answer the mock.
Do not worry about the exam. You will not be asked about garbage collection of string literals.
[ November 16, 2002: Message edited by: Jose Botella ]
Nothing? Or something? Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 891 times.
Similar Threads
garbage collection ?
garbage collector
Garbage Collection of Strings
question about gc
GC
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 04:46:46.