This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Java in General and the fly likes Common Constants Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Common Constants" Watch "Common Constants" New topic
Author

Common Constants

Clayton Cramer
Ranch Hand

Joined: Aug 26, 2010
Posts: 41
We have started to use a tool called Your Kit Java Profiler. It shows something that just dumbfounds me, because I would have thought Java was smarter than this. It shows that we have 26 MB of heap devoted to duplicate copies of the empty string "". Unlike every C compiler that I have ever, used it would appear that if you have multiple strings that are identical, and immutable (you are referencing them as "" in expressions like str = i + ""), each reference produces its own copy of that empty string. The painful solution is to define an empty string in one file:

public final static String s = "";

and then replace all the "" references with s.

Is there really no way to tell the Java compiler to combine immutable constants?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7718
    
  20

Clayton Cramer wrote:Is there really no way to tell the Java compiler to combine immutable constants?

Have a look at String.intern().

However, if you say that there's 26Mb of this, I don't think that's the full extent of your problem.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3013
    
  10
This sounds very suspicious, as the Java Language Specification has always required that string literals and other String constant expressions be interned in the first place. It's possible that you've got a compiler/JVM which is somehow violating this I suppose. But it seems more likely you're misinterpreting what YourKit is telling you. Perhaps there are other objects being created by the expressions you're using. For example, for

it's probably generating something like this:

You can probably do better with
Clayton Cramer
Ranch Hand

Joined: Aug 26, 2010
Posts: 41
It is certainly the case that we have vast numbers of i + "" expressions scattered about the code, and that might explain why we have 26 MB of "". Each new StringBuilder("") would presumably create a distinct copy, because a StringBuilder object is mutable.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

26 MB of StringBuilder containing "", yes, could theoretically make sense, although they should be short-lived, so I'm not sure why you'd have 26 MB live at once.

26 MB of String objects containing "" sounds very wrong.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14117
    
  16

Do you have code that uses the String constructor with a literal, like this:

If you do that a lot, you would be creating a lot of unnecessary String objects, and it prevents the compiler from reusing String objects. Never use new String(...) with a string literal as its argument, it's never necessary.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Common Constants