I'm trying to see how much memory is used when I have a lot of duplicate strings. I am using the method highlighted in this answer (at the bottom)
Here's me creating a list of a ten million strings, where each string has only a few characters.
public class Test1 {
public static void main(String[] args) {
int count = 10000000;
List<String> names = new ArrayList<String>();
for (int i = 0; i < count; i++) {
names.add("test");
}
Runtime rt = Runtime.getRuntime();
long usedMem = rt.totalMemory() - rt.freeMemory();
System.out.println(usedMem / (1024*1024) + " MB");
}
}
I run it, and it says 88 MB. I am not too sure what this represents, but I'll just take it as a number to compare iwth.
Here's me doing the same test again, except I replaced the small string with some lorem ipsum text
public class Test1 {
public static void main(String[] args) {
int count = 10000000;
List<String> names = new ArrayList<String>();
for (int i = 0; i < count; i++) {
names.add("Lorem ipsum dolor sit amet, brute euismod eleifend te quo, ne qui iudicabit hendrerit. Ea sit dolore assentior prodesset. In ludus adipiscing eos, ius erat graeco at, cu nec melius copiosae. Epicuri suavitate gubergren id sea, possim animal eu nam, cu error libris expetendis his. Te sea agam fabulas, vis eruditi complectitur ei. Ei sale modus vis, pri et iracundia temporibus. Mel mundi antiopam ad.");
}
Runtime rt = Runtime.getRuntime();
long usedMem = rt.totalMemory() - rt.freeMemory();
System.out.println(usedMem / (1024*1024) + " MB");
}
}
I run this, and it says 88 MB again.
This is not meant to be an attempt to properly benchmark memory usage, but I was expecting the number for the ipsum lorem string to be somewhat larger because there are about 50x as many characters in the string.
How does Java store arrays of strings in memory? Or, am I doing something wrong?