Others offer reading and processing parts of your file at a time. If possible, one of these methods would be better.
However, if this is not possible, and you can load the String initially into memory, as you specify, but later it parses that string, which creates problems, you can use substrings. In Java, a substring is displayed on top of the original char array and just takes the memory for the Object base, and then the start and int pointers.
So, when you find the part of the line that you want to keep separately, use something like:
String piece = largeString.substring(foundStart, foundEnd);
If you instead or the code that does this inside, then memory usage will increase dramatically:
new String(largeString.substring(foundStart, foundEnd));
Note that you should use String.substring() with caution for this very reason. You may have a very large string from which you take a substring, and then drop the reference to the original string. The problem is that the substring is still referencing the original large char array. The GC will not release it until the substring is also removed. In such cases, it is useful to use new String(...) to ensure that an unused large array will be discarded by GC (this is one of the few cases where you should use new String(...) ).
Another method, if you expect to have many small lines, and they are likely to have the same value, but from an external source (such as a file) should use .intern() after creating a new line.
Note. It depends on the String implementation, which you really should not know about, but in practice for large applications you sometimes have to rely on this knowledge. Keep in mind that future versions of Java may change this (albeit unlikely).
Kevin brock
source share