Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

garbage collection for String  RSS feed

 
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody ,
How Strings are garbage collected ?

Thanks & regards,
S
 
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The same as every other object, once there are no more references to that object, it becomes eligible for garbage collection, I put that in bold because is doesn't guarantee that it will be collected, only that it could be collected.

Hunter
 
lowercase baba
Bartender
Posts: 12621
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and i believe String literals, which are in the string pool, are never collected.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

fred rosenberger wrote:and i believe String literals, which are in the string pool, are never collected.


right, but the references to those literals can be collected. Sorry I shouldve mentioned that, thanks Fred.

Hunter
 
Java Cowboy
Sheriff
Posts: 16083
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hunter McMillen wrote:right, but the references to those literals can be collected. Sorry I shouldve mentioned that, thanks Fred.


No, the garbage collector only deals with objects. Variables (references to objects) are not objects themselves; the garbage collector doesn't clean up those.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When people discuss String GC and memory the following comes to mind.
Might be worth while reading this.
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4513622
 
S Majumder
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Hunter McMillen for your reply.
Actually we know that strings are located on the String constant pool , so after calling the GC what is happened ?

regards,
S


 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

s begri wrote:we know that strings are located on the String constant pool


Only string literals and interned strings are located in the string pool. As Fred said these are not garbage collected. All other string objects are located in normal heap memory and are garbage collected the same way as other objects
 
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Joanne Neal wrote:Only string literals and interned strings are located in the string pool.


actually the references of the string literals and interned strings are located in the string pool. always String objects are live in heap. and those references never removed by JVM, thus the objects which referenced by string constant pool are not eligible for Garbage Collection.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Seetharaman Venkatasamy wrote:

Joanne Neal wrote:Only string literals and interned strings are located in the string pool.


actually the references of the string literals and interned strings are located in the string pool.


Indeed. Thanks for clarifying that.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!