File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes garbage collection and String type Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "garbage collection and String type" Watch "garbage collection and String type" New topic
Author

garbage collection and String type

Zeeman Chen
Greenhorn

Joined: Oct 30, 2003
Posts: 9
I know String is a special type of object,
Can garbage collector effect on String?
thanks in advance.


If a man take no thought at what is distant, he will find sorrow near at hand. ~ Confucius
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8259

Originally posted by zeeman chen:
I know String is a special type of object,

It is? How? It's declared final so it can't be extended, but that's it. I guess you can count the "+" operator as a special case. . .

Can garbage collector effect on String?

Yes.
[ November 25, 2003: Message edited by: Joe Ess ]

"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
It is my understanding that literal String objects are a special case. There is a special pool of these in the JVM, without duplication. These will not get garbage-collected, I think.
However, other String objects, created in any other way (deserialised, created from char[], created by concatenation etc.) are eligible for garbage collection, like any other object. When no-one is referencing the String, it can be GC'd.
A further complication is that the actual array of characters used by a particular String object may be shared with other String objects. So, although a String may be eligible for GC, its character array may not. However, none of this is special "String magic"; you will often share objects between other objects in this way, yourself.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: garbage collection and String type
 
Similar Threads
the "+" operator
garbage collection
Garbage
how many objects Eligible for garbage collection
Garbage Collection for String.