Others have suggested reading and processing portions of your file at a single time. If possible, one of those ways would be better.
However, if this is not possible and you are able to load the String
initially into memory as you indicate but it is later parsing of this string that creates problems, you may be able to use substrings. In Java a sub-string maps on top of the original char
array and just takes memory for the base Object
and then the start and length int pointers.
So, when you find a portion of the string that you want to keep separately, use something like:
String piece = largeString.substring(foundStart, foundEnd);
If you instead this or code that internally does this, then the memory use will increase dramatically:
new String(largeString.substring(foundStart, foundEnd));
Note that you must use String.substring()
with care for this very reason. You could have a very large string off of which you take a substring and then discard your reference to the original string. The problem is the substring still references the original large char
array. The GC will not release that until the substring also is removed. In cases like this, it's useful to actually use new String(...)
to ensure the unused large array will be discarded by the GC (this is one of the few cases where you should ever use new String(...)
).
Another technique, if you expect to have lots of little strings around and these are likely to have the same values, but come from an external source (like a file), is to use .intern()
after creating the new string.
Note: This does depend on the implementation of String
which you really shouldn't have to be aware of, but in practice for large applications sometimes you do have to rely on that knowledge. Be aware that future versions of Java may change this (though not likely).