This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Memery release from Objects in String pool

 
sujith Acharya
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

If the object is created in heap, it will be cleaned bt garbage collector when there is no reachable reference for the object.
We know String constants are created in String pool if there is no String object present in String pool. My doubt is when the objects in the String pool is cleaned or released from memory? If the objects are continuously getting added to the pool, there might be memory issue at some point of time.

-thanks
 
Henry Wong
author
Marshal
Pie
Posts: 21500
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the objects are continuously getting added to the pool, there might be memory issue at some point of time.


This is one reason why you should not intern() a string, unless you really know that it will save memory... because interning everything cause a memory issue.

Personally, I don't use intern at all. Just let the compiler intern the constants, and that will be it.

Henry
 
sujith Acharya
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if we consider this point, String s = new String("ABC") is better than String s = "ABC" where in former case when s is made null, the String Object with "ABC" will be eligible for gc where in later case the object will reside in constant pool for 'n' hous.
Henry please correct me if I m wrong..

-Thanks
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you're wrong in that in both cases there's a literal "ABC" in the string pool; in the first case, you create a second String on the heap as well.

To answer your original question, Strings in the string pool may be released when, and only when, the classes that defined them are collected. This in turn will happen only if the classloader that loaded the class is itself collected. In general, string literals live as long as the code that uses them. If you have a complex environment like an app server in which classloaders are being created and deleted, then there may be some string pool cleanup that gets done, but otherwise not.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15484
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, String s = new String("ABC") is not better. There is still the String object in the pool that represents the "ABC" string literal that you are using here. In this statement, you are explicitly creating a new String object that copies its contents from the pooled String object. So you'll have two String objects - the one in the pool, and the one you created yourself.

Writing something like String s = new String("...") is never necessary in Java and is always unnecessarily inefficient.
 
sujith Acharya
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for detailed explanation.
In this statement, you are explicitly creating a new String object that copies its contents from the pooled String object.


To make more clear , even if there is no literal "ABC" exists in String pool, and String s = new String("ABC") is the first statement of the application, then
i) String object "ABC" will be created in String Pool
ii) String Object will be created in heap which copies content from String Pool
So in total 2 objects will be created.. is that correct?

-Thanks
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15484
43
Android IntelliJ IDE Java Scala Spring
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic