
Just found this article on a possible memory leak in Java. I’ve been under the understanding that StingBuffers where the preferred method of building large strings. While this is still the case, it turns out that when you call the StringBuffer.toString() method the entire byte array of the StringBuffer is returned.
if you initialise your StringBuffer to have a large capacity, then any String generated from it will occupy memory according to that capacity, regardless of the length of the resulting string.
This is nuts! This is supposed to be fixed in the latest verision of Java (v5), but I’ve only been using 1.4 for my java work and this is where the issue occurs. I’m going to run some more tests tomorrow, but this lines up with the information I’ve been seeing in JProfiler. The largest memory allocations occur in the String an byte[] classes.
What are you supposed to do when it’s the virtual machine that is causing the problems in your software! I hope this solves the problem, because otherwise it’s back to the profiler.