• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

strings and memory

 
Ranch Hand
Posts: 309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well, this is another question on strings and garbage collection.
When I create a string with "new", it's in heap, at the sametime one string will be created in the pool. So, this heap copy will get GCed fine but the one in the pool will remain till the application exits.
Now if in my appication, I have to create lot of string(magnitude of 1M and more), how do i cope up with this situation. All these strings will be created in the pool even if I use a new operator, and will remain there till I exit my appl. Is there no way to GC my strings if they are no more referenced??? What are the advantages, if any, to create a string with the "new" operator???
Thanx,
SHankar.
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only advantage or really difference is that the Strings objects are different, ie poolString==newString will return false. If you have a huge number of fixed strings you could put them in a property file or resource bundle which is better from a maintainence point of view than hard-coding literal strings. Nothing should go into the string pool that way.
 
shankar vembu
Ranch Hand
Posts: 309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Michael Morris:
If you have a huge number of fixed strings you could put them in a property file or resource bundle which is better from a maintainence point of view than hard-coding literal strings. Nothing should go into the string pool that way.


But when I read the strings from the properties file or the bundle, the string will be created in the pool. Right?
If you create a lot many objects in your appl, then at some point of time, they will be GCed and I am sure that I wont run into memory problems.
I dont have this luxury with strings. That is my point.
Regards,
SHankar.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But when I read the strings from the properties file or the bundle the string will be created in the pool. ...


There should be no literal strings placed in the pool because that occurs at compile time. The Properties class builds the strings from chars parsed in the input file.
 
shankar vembu
Ranch Hand
Posts: 309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh ok, i get it now. thanx for clarifying,michael.
regards,
shankar
 
reply
    Bookmark Topic Watch Topic
  • New Topic