Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String literal GCed?

 
Cathy Song
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I read some where that String literal is never gc'ed.
Now String literals are created in the String constant pool..it that not a heap?
Can someone shed some light on this issue?
Thanks.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

At source code line 3, s1 refers to a String object. At line 4, s1 is set to null. Normally we would say the object is eligible for gc after line 4.

But byte code line 5 is loading the same reference as byte code line 0.
ldc is the byte code instruction to load a reference to an instance of class String representing a String literal. #2 is an index into the runtime constant pool (not String pool).
This is as close as I can come to proving that interned String objects are not garbage collected.
[ November 08, 2003: Message edited by: Marlene Miller ]
 
Cathy Song
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Marlene.
GC combined with string literals can get tricky.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The good news is that "for the purpose of the exam" (sorry Marlene, it's the focus police again ), GC and the String pool won't be mixed together. When the exam asks you to determine how many objects are eligible for the GC, it won't be referring to String objects.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic