Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Common Constants

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Master Rancher
Posts: 3537
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
No one can make you feel inferior without your consent - Eleanor Roosevelt. tiny ad:
ScroogeXHTML 9.0 - RTF to HTML5 and XHTML converter
https://coderanch.com/t/731949/ScroogeXHTML-RTF-HTML-XHTML-converter
    Bookmark Topic Watch Topic
  • New Topic