Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

garbage collection and String type  RSS feed

 
Zeeman Chen
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know String is a special type of object,
Can garbage collector effect on String?
thanks in advance.
 
Joe Ess
Bartender
Posts: 9429
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!