• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

String's and Gargage Collection

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,
i have a dougt regarding String and how garbage the collector works.
for instance String created like this :
String a = "Isto e um teste";
can be garbage collected ?
if yes, does the garbage collector works with this Strings like any other object ?
best regards
Lu�s Filipe Meira
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Garbadge collection isnt applicable for string literals, they are just returned to the pool.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, that String will never be garbage collected. The reason for this has to do with an optimization used by the JVM.
This topic has been discussed many times before. Take a look at this thread for a great explanation.
Corey
 
luis meira
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by brent spearios:
Garbadge collection isnt applicable for string literals, they are just returned to the pool.


many thanks.
that was my guess but i needed to be sure.
best regards
Lu�s Meira
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Luis - read through that thread that I pointed out. This turns out to be a more complicated issue than it seems at first glance. Strings aren't really "returned to a pool." Rather, there is a constant reference to them from a special table. Read the thread. It's good.
Corey
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Corey McGlone:
Luis - read through that thread that I pointed out. This turns out to be a more complicated issue than it seems at first glance. Strings aren't really "returned to a pool." Rather, there is a constant reference to them from a special table. Read the thread. It's good.
Corey


Thanks Corey pointing out the thread.
Yeah, it is really good.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This information is not needed for the exam.
String literals can be gc.ed if we download the class in which they were created. Doing that we download the constant pool of the class. This is the table that holds references to them.
This is an example
Go to the last program in the thread
 
We're all out of roofs. But we still have tiny ads:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic