2

I am having a problem with creating 10 million objects from a csv file on import in my application. Doing a heap dump I figured the Out-Of-Memory occurres when reaching around 4.8 million objects.

Researching a solution and looking into how to improve the GC I stumbled upon the VMOption:

-XX:+UseStringDeduplication

Using this I was able to create 7.8 million objects before Out-Of-Memory occurred, which is an increase of more than 60 %. The heap dump showed, that the strings and the byte[] in the heap where decrease by up to 75 %.

In another stackoverflow answer someone showed similar results in memory usage optimization. https://stackoverflow.com/a/27953779/10020419

So I am asking myself, why this isn't standard (I am using Java 11)?

The only downsides I read are more like smaller increasing of CPU, because the GC has to work a little more.

Are there any other reasons to not use this as a default?

Chris
  • 5,109
  • 3
  • 19
  • 40
  • 2
    Well I guess reading [Why/When you would not want to have Java 8 UseStringDeduplication enabled in JVM?](https://stackoverflow.com/questions/42080648/why-when-you-would-not-want-to-have-java-8-usestringdeduplication-enabled-in-jvm) could be part of the answer. Now, for the "why is is not standard", this will be "opinion-based".. The link asked for the reason to use it and seems better fit SO – AxelH Aug 14 '19 at 08:43
  • Thanks, I found several questions/answers on stackoverflow, but not this one. – Chris Aug 14 '19 at 09:08

1 Answers1

1

When asking "why was this language decision made", all we can do is guess or listen to what the language designers have said publicly. I haven't heard any commentary from the developers as to why they did this, so here's a somewhat educated guess.

  • In most cases, CPU is a more precious resource than memory.
  • This flag requires the G1 garbage collector, which may not be applicable for some people. If it was on by default, you may come to switch to another GC and find that you now get OOM errors everywhere.

Like I say, without explicit comments from the devs, educated guesses are the next best thing.

cameron1024
  • 9,083
  • 2
  • 16
  • 36
  • 1
    Ok, thank. The question is rather not "why isn't this standard" but rather "can I use this or are there problems I didn't find?" The link provided by AlexH in the comment answers this question. – Chris Aug 14 '19 at 09:10