aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Are Strings ever garbage collected? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Are Strings ever garbage collected?" Watch "Are Strings ever garbage collected?" New topic
Author

Are Strings ever garbage collected?

Bob Graffagnino
Ranch Hand

Joined: May 30, 2001
Posts: 81
Are Strings ever garbage collected? I would have thought so, but JQ+ has a question involving GC and the explaination stated that Strings were never garbage collected. Sorry, I don't remember the question id, but I do remember that the question was classified as "very tough".
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
String objects you create with a new are eligible for gc when there is no longer a reference to them, just like any other object.
For example:
String myString = new String("I am a string");
myString = null;
This code creates a new String object, then immediately sets its reference variable to null, thus making this object eligible for gc.
Now, there is a concept of a "String literal", which is the actual text between the quotes ("I am a string"). This string literal is *also* a String object in memory, BUT this object will never be gc as long as the class in which it is defined is loaded...for most cases, and for the SCJP, you can state that "string literals are never eligible for gc."
Here's a *similar* example as above, but with different results...
String myString = "I am a string";
myString = null;
The myString reference variable is set to null, but the java VM keeps its own private reference to the String object on the right. That's why it's never eligible for gc.
Rob
[ January 31, 2002: Message edited by: Rob Ross ]

Rob
SCJP 1.4
Bob Graffagnino
Ranch Hand

Joined: May 30, 2001
Posts: 81
Thanks Rob, I understand it now.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Rob Ross:
String objects you create with a new are eligible for gc when there is no longer a reference to them, just like any other object.
For example:
String myString = new String("I am a string");
myString = null;
This code creates a new String object, then immediately sets its reference variable to null, thus making this object eligible for gc.

When you create a String in this way, a new String object is created on the heap, correct? Is a String literal also created which will never be garbage collected or is the one that is created on the heap the only one that is created?
Thanks,
Corey


SCJP Tipline, etc.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by Corey McGlone:

When you create a String in this way, a new String object is created on the heap, correct? Is a String literal also created which will never be garbage collected or is the one that is created on the heap the only one that is created?
Thanks,
Corey

Corey
When you create Strings using the new operator, the strings are created on the heap, but they are not treated as string literal and they will not be included in the pool of strings. Only if you call the intern method are the string objects available to the pool of strings.
I hope this helps!!!
Amish
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Amish A Patel:

Corey
When you create Strings using the new operator, the strings are created on the heap, but they are not treated as string literal and they will not be included in the pool of strings. Only if you call the intern method are the string objects available to the pool of strings.
I hope this helps!!!
Amish

Thanks, Amish. Just to confirm what you had said, I wrote this little code snippet:

Indeed, in the first case, the Strings seem to be part of the pool (since they both reference the same object) but, in the second case, two distinct String objects are created and referenced.
Thanks again,
Corey
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by Corey McGlone:

Indeed, in the first case, the Strings seem to be part of the pool (since they both reference the same object) but, in the second case, two distinct String objects are created and referenced.
Thanks again,
Corey

Hi Corey
Please put this code in a main method and run it and see what you get.

Hope this helps
Amish
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Originally posted by Corey McGlone:

When you create a String in this way, a new String object is created on the heap, correct? Is a String literal also created which will never be garbage collected or is the one that is created on the heap the only one that is created?
Thanks,
Corey

All objects are always created on the heap, regardless of who creates them.
String literals are objects. They are also created on the heap. BUT, they are created by the classloader when it loads a class that contains String literals, and then a reference to that String object is kept internally by the Class object. So even String literals are on the heap. They are just never garbage collected.
Rob
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Are Strings ever garbage collected?
 
Similar Threads
GC Stuff
GC Question
is Strings in Java are not garbage-collected ?
Garbage collection
garbage collection