Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to keep String literals away from literals table?  RSS feed

 
Vadim Vararu
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can we tell the jvm not to save all Strings in literals pool? Is this enough - new String("xxx") ? I guess JVM looks for "xxx" in literals table and if doesn't find "xxx" than pushes it inside. Is it correct?

I just want JVM to treat this -> new String("xxx") like any other object and not put the string into the literals table. I want my memory cleaner.

Does the JVM, actually, put each new String into the table, or just "" literals?
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[VV]: How can we tell the jvm not to save all Strings in literals pool?

You can't.

[VV]: Is this enough - new String("xxx") ? I guess JVM looks for "xxx" in literals table and if doesn't find "xxx" than pushes it inside. Is it correct?

Yes, except then it also creates a new, totally useless string containing identical contents, which is not part of the String pool. In other words, the use of "xxx" creates one string, which is in the pool, and the use of new String() creates another, not in the pool.

[VV]: I just want JVM to treat this -> new String("xxx") like any other object and not put the string into the literals table.

Well, it's two objects, and one of them is going into the pool - there's nothing you can do about that unless you don't use literals. Or don't use Java. It's much simpler to use just literals (no "new String()") and not worry about it. That way you only get one object, which is in the pool.

[VV]: I want my memory cleaner.

Remember: mauve has the most RAM.

[VV]: Does the JVM, actually, put each new String into the table, or just "" liter

Just literals (written in code using "") plus any String created using the intern() method. Not all strings, definitely not.
 
Vadim Vararu
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about the case my application uses thousands and more Strings? Won't this trash the memory?
 
Vadim Vararu
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember: mauve has the most RAM.
What means "mauve"?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vadim Vararu:
What about the case my application uses thousands and more Strings? Won't this trash the memory?


The only strings that go into the string pool are the literals -- the double-quoted strings. Now, think about this: all of your .class files are going to be loaded into memory. The JVM will parse each .class file. It will put the bytecode for all your methods in one place. It will create java.lang.Class objects to represent the classes in another place. And it will store the String literals from the class file in the String pool. Without those String literals, the class can't run, right? The JVM has to store them somewhere!

If your classes contain so many embedded literal Strings that you're worried about excessive memory use from them, then maybe you need to consider moving some of those into resource files, and reading them in at runtime. The performance will, of course, be lower, but it will be possible for unused Strings to be garbage collected.

"Mauve" is a color; Mike is having a little fun with you.
 
Vadim Vararu
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, using something like String x = obj.toString(); doesn't make the JVM store that string into the literals pool? Just, what's between the ""?

Than, this (String x = new String("") instruction doesn't ever make sense?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct. There are only two ways for a String to get into the String pool: either it appears between double quotes in the source for a loaded class, or someone calls the method intern() on that String (that's what intern() does, is add a copy of a String to the String pool.)

And yes, the String(String) constructor is virtually never appropriate. Because Strings are immutable, there's no reason to ever make a copy of one. In fact, if you see this constructor being used, that's a pretty good way to tell that the code was written by someone new to Java!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!