• Post Reply Bookmark Topic Watch Topic
  • New Topic

String Object and GC  RSS feed

 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are the lost string objects in the String Constant Pool eligible for Garbage Collection?

String s = "abc"
s = "def";

is the object "abc" eligible for GC. In short, does Garbage Collector runs only in heap or in String constant pool as well.

I have read some post on this in Java Ranch, but everyone seemed to have different opinion about it.
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All Objects are created in heap and String is also an Object. String constant pool is a part of heap memory.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that correct? I was under the impression that the string pool is not a part of the heap, and that string pool elements are not subject to garbage collection.

That doesn't apply to String objects stored in the heap, however. There are two ways to create a string:

1. String s = "abc";
2. String s = new String("abc");

In 1., the string pool is checked for a String equal to the literal "abc." If it is found, reference s is bound to that String object. If it is not found, a String object for "abc" is created in the pool, and s is bound to it.

In 2., if "abc" is in the pool, a new String object for "abc" is created in heap memory regardless, and s is assigned to it, but if "abc" is not in the pool, it will also be added to the pool.

Am I missing something? Are the strings in the pool subject to garbage collection as well?
[ December 17, 2008: Message edited by: Ruben Soto ]
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well its true that Heap and String pool don't follow the same rules like for gc and when you use new the String Object is created in heap. But as far as Internal architecture is considered I don't see any special area for String pool. What may be possible that String pool uses the heap space internally.


[ December 18, 2008: Message edited by: Himanshu Gupta ]
 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First things first. String constant pool is not a collection of String objects, it's a collection of references to String objects

This is what I found in Java Ranch itself in an article by Corey McGlone. Consider the following.






Now Consider this..



So how many objects eligible for GC just before the main method ends?

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. This is the same example as I used above so you can see what our picture looked liked originally there. Once we assign our variables, one and two, to null, we end up with a picture that looks like this(Assuming that one object got GC):


[ December 18, 2008: Message edited by: Himalay Majumdar ]
 
Bert Bates
author
Sheriff
Posts: 8954
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a very interesting discussion, and it makes total sense that you'd have to understand this for the exam, but the good news is that the real exam DOES NOT cover how the GC interacts with objects of type String. On the real exam GC questions will always use objects of types other than String.

Off to the intermediate forum for this discussion...
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to remember when you're having discussions like this that if it does not specifically say something in the Java Virtual Machine Specification or the Java Language Specification, then the answer is implementation dependent. At some point, if you keep arguing about details, you'll get into an area where there are no absolute answers. You have to keep the discussion on a theoretical level. This discussion has already wandered into the "implementation dependent" category.

Corey's article is completely correct from a theoretical perspective, but recent Sun JVMs actually do allocate literal String objects in a special heap area called the "Permanent Generation;" they're not in the normal heap. Despite the "permanent" name, however, they can be garbage-collected if and when the class that refers to them is actually collected (classes can be garbage collected only when the ClassLoader that loaded them is itself collected.)

As Bert says, this ain't on the test.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest, thank you for the answer, and thanks everyone else as well.

It's good that this sort of thing is not in the exam.
 
Peter Lawrey
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would expect that String literals are referenced by the Classes in which they are used. Thus, they will not be GCed until all classes which has that String are unloaded.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!