Java OutOfMemoryError in reading a large text file

Try to use java.nio.MappedByteBuffer.

http://docs.oracle.com/javase/7/docs/api/java/nio/MappedByteBuffer.html

You can map a file’s content onto memory without copying it manually. High-level Operating Systems offer memory-mapping and Java has API to utilize the feature.

If my understanding is correct, memory-mapping does not load a file’s entire content onto memory (meaning “loaded and unloaded partially as necessary”), so I guess a 10GB file won’t eat up your memory.

Leave a Comment