I am profiling an application and noticed that 52% (195MB) of the memory is being used by char[] and 20% by String. This is a large project with a lot of dependencies and I’ve just seen it so I have a couple of related questions to help me get started:
Does String s = "some text" create a char[]?
I’ve noticed there’s hundreds of String s = new String("some text") with no apparent reason. Is this the culprit?
This doesn’t create any objects.
This creates a copy of the String and possibly the char[] (two objects). A copy is only taken if the String represents the substring of another string.
I would ensure you have a version of Java which supports
-XX:+UseCompressedStringsThis is on by default in later versions of Java and allows the JVM to usebyte[]instead ofchar[]which can be half the size.However, 400 MB isn’t that big these days and buy more memory may be the simplest solution. You can get 16 GB for as little as $120.