• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

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
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic