Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OutOfMemoryError: Java heap space when trying to read large file

I'm trying to read large file(approximately 516mb), and it has 18 lines of text. I tried to write down the code myself and got an error in the first line of code while trying to read the file:

 try(BufferedReader br = new BufferedReader(new FileReader("test.txt"))) {
        String line;
        while ((line = br.readLine()) != null) {
            String fileContent = line;
        }
 }

Note: File exists and it's size is approximately 516mb. If there is another safer and faster method of reading from please tell me(Even if it will linebreaks). Edit: Here I tried by using Scanner, but it lasts a bit longer and then gives the same error

try(BufferedReader br = new BufferedReader(new FileReader("test.txt"))) {
    Scanner scanner = new Scanner(br);
    while(scanner.hasNext()){
        int index = Integer.parseInt(scanner.next());
        // and here do something with index
    }
}

I even splitted file into 1800 lines, but nothing got fixed

like image 290
user3260312 Avatar asked Apr 15 '15 07:04

user3260312


People also ask

How to increase the size of the heap space in Java?

To increase the size of the heap space, use "-Xmx" java options to solve the out-of-memory error. This configuration will provide 1024 heap space to the application. However, increasing the heap size will not cover all the necessary errors (such as memory leaks) for your application that is running on the JVM.

How to fix Java out-of-memory error?

To fix the error, particular solutions could be taken up due to the causing Java.lang.outofmemory error: Solution 1: The easiest solution to remove such error is to increase the memory size of the JVM heap space. To increase the size of the heap space, use "-Xmx" java options to solve the out-of-memory error.

How to set the heap size of the JVM?

With the -Xmx JVM argument, you can set the heap size. For instance, you can allow the JVM to use 2 GB (2048 MB) of memory with the following command: $ java -Xmx2048m ... $ java -Xmx2048m ... Solution 2. Improve or fix the application to reduce memory usage

Why does the heap memory increase after creating an object?

If this is correct, then why this error occurred after running fine for sometime as space for instance variables are alloted at the time of object creation. That means you are creating more objects in your application over a period of time continuously. New objects will be stored in heap memory and that's the reason for growth in heap memory.


1 Answers

Using BufferedReader already help you to avoid loading the whole file into the memory. So, for further improvement, as you mentioned each number is separated by space, so instead of this:

line = br.readLine();

We can wrap the reader with a scanner,

Scanner scanner = new Scanner(br);

And extract each number in the file using scanner.next(); and store it into an integer array will also help to reduce memory usage:

int val = Integer.parseInt(scanner.next());

This will help you avoiding reading the whole sentence.

And you can also limit your buffer size for BufferedReader

BufferedReader br = new BufferedReader(new FileReader("test.txt") , 8*1024);

More information Does the Scanner class load the entire file into memory at once?

like image 52
Pham Trung Avatar answered Oct 11 '22 18:10

Pham Trung