Corey McGlone has said in artical under the topic Garbage Collection:
What makes an object eligible for garbage collection? If you're preparing for the SCJP exam (or even if you're not), the answer to that question should roll right off your tongue. An object is eligible for garbage collection when it is no longer referenced from an active part of the application. Anyone see what is special about garbage collection for String literals? Let's look at an example and see if you can see where this is going.
public class ImmutableStrings {
public static void main(String[] args) {
String one = "someString";
String two = new String("someString");
one = two = null;
} }
Just before the main method ends, how many objects are available for garbage collection? 0? 1? 2?
The answer is 1. Unlike most objects, String literals always have a reference to them from the String Literal Pool. That means that they always have a reference to them and are, therefore, not eligible for garbage collection.
As you can see, even though neither of our local variables, one or two, refer to our String object, there is still a reference to it from the String Literal Pool. Therefore, the object is not elgible for garbage collection. The object is always reachable through use of the intern() method, as referred to earlier.
In general, String Literals are not eligible for garbage collection. Ever.
A small program can create a lot of String objects very quickly. For example, how many
do you think this piece of code creates?
10: String alpha = "";
11: for(char current = 'a'; current <= 'z'; current++)
12: alpha += current;
13: System.out.println(alpha);
The empty String on line 10 is instantiated, and then line 12 appends an "a". However,
because the String object is immutable, a new String object is assigned to alpha and the
“” object becomes eligible for garbage collection. The next time through the loop, alpha is
assigned a new String object, "ab", and the "a" object becomes eligible for garbage
collection. The next iteration assigns alpha to "abc" and the "ab" object becomes eligible
for garbage collection, and so on.
This sequence of events continues, and after 26 iterations through the loop, a total of 27
objects are instantiated, most of which are immediately eligible for garbage collection.
S B Patel wrote:As per article any String literal object referencing through String pool never been able for garbage collection... Whereas book says different thing... ?
S B Patel wrote:When I've tried this code... (Even I don't know much about intern() method)...
S B Patel wrote:intern() method is only works before setting any reference variable with null value, but not after that... If we use, it throws an nullpointerException at run time. *** Does this means that the object has already been finalized by garbage collection???***
S B Patel wrote:1) String assign to any reference variable with "" is a String literal and goes in the String literal pool, and never been eligible for garbage collection.
2) All String objects other than a String literal is only eligible for garbage collection.
S B Patel wrote:s is set to null but "abc" String literal is alive in background in String literal pool until the program runs
S B Patel wrote:At Line 1 a new object with string "abc" is created on the heap and not on a String literal pool b'z it's not a String literal
At Line two s1 is eligible for garbage collection... and thus "abc" also be garbage collected because it is not on String literal pool
S B Patel wrote:Comments about the code snippet:
- Line 1 (Object and not String Literal)
- Line 2 (Must be an Object and not String Literal)
- Line 3 (Now s is String Literal... The old object s with "abcdef" is eligible for garbage collection)
- After either of last two statements at Line 4 or Line 5 s is an reference to an Object "abcdefgh" OR "abcd" and not a String Literal any more, String Literal s on line 3 is not reference to s anymore but still alive in String Literal Pool
K&B7 wrote:Note: Due to the vagaries of the String constant pool, the exam focuses its garbage collection questions on non-String objects, and so our garbage collection discussions apply to only non-String objects too.
a fool thinks himself to be wise, but a wise man knows himself to be a fool - shakespeare. foolish tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|