I am working on an application that has a lot of duplicate Strings and my task is to eliminate them to decrease memory usage. My first thought was to use String.intern
to guarantee that only one reference of a String would exist. It worked to decrease the heap memory, but it increased the PermGen way too much; in fact, because there are many strings that are declared only once, the total amount of memory used by the application increased, actually.
After searching for another ideas, I found this approach: https://stackoverflow.com/a/725822/1384913.
It happened the same thing as String.intern: The String usage decreased, but the memory that I saved is being used in the WeakHashMap
and WeakHashMap$Entry
classes.
Is there an effective way to maintain only one reference for each String that doesn't spend the same amount of memory that I'm recovering doing it?