简体   繁体   中英

How to deal with out of heap memory in this Java code?

I'm using JMatIO to read Matlab .mat file into my Java program. But when my code executed, it reported out of memory error:

java.lang.OutOfMemoryError: Java heap space

My program is just reading a mat file which is about 27M in size. I've tried using several -Xmx and -Xms VM options to increase the heap size but it didn't help. My code is as follows:

public class ReadMat {
    private MatFileReader reader;

    public ReadMat(File f) {
        try {
            reader = new MatFileReader(f);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        File matFile = new File("test.mat");
        ReadMat rmat = new ReadMat(matFile);
    }
}

test.mat is just a Matlab mat file which is about 27M. It will report OutOfMemoryError once I run it. So how to solve this problem?

There are two problems. One is that in any conversion routine like this you generally have to hold the source array in memory while you convert it or load it. So the whole 27M file is in memory to begin with and this could be larger because the holding buffer may be larger.

Secondly, JMatIO is probably converting the primitive types into Objects. For example, if the original file has shorts in it, each using up 2 bytes and you convert each of these to an Object which take, say, 16 bytes then your memory usage increases significantly.

If changing the minimum and maximum sizes for the JVM heap are not fixing the problem, then you need to evaluate how MatFileReader reads test.mat into memory. I'm willing to bet that the object allocation of MatFileReader during the reading of the file is what is causing the OutOfMemoryError.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM