0

Hi I'm reading data from .xls sheet it contains 8500 rows of data and I'm trying to store it in double[][] but I'm getting an error

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

Code

public static double[][] getData_DoubleArray(String path, int sheetNo, int rowIndex1, int rowIndex2) {
    double[][] doubleArray=null;
    try {
        HSSFSheet sheet = PS_ExcelReader.getWorkSheet(path,sheetNo);
        System.out.println("sheet" + sheet);
        List<Object> data1 =  PS_ExcelReader.getFullColumnByIndex(sheet, rowIndex1);

        List<Object> data2 =  PS_ExcelReader.getFullColumnByIndex(sheet, rowIndex2);
        doubleArray = new double[data1.size()][data2.size()];
        for(int i = 0; i < data1.size(); i++) {
            for(int j = 0; j < data2.size(); j++) {
                doubleArray[i][0] = (Double)data1.get(i);
                doubleArray[i][1] = (Double)data2.get(j);
            }               
        }
        System.out.println("array  " + Arrays.deepToString(doubleArray));
    } 
    catch(IOException ioe) {
        log.error("data mis match");    
    }
    return doubleArray;       
}
3

3 Answers 3

5

Currently this line:

doubleArray = new double[data1.size()][data2.size()];

is creating 8500 x 8500 doubles, which is over 500MBs. You are basically allocating space enough for 8500rows and 8500columns.

But seeing that you are only using 2 of these columns in your algorithm:

doubleArray[i][0] = (Double)data1.get(i);
doubleArray[i][1] = (Double)data2.get(j);

I doubt that you really want to create that many columns. Given your remaining algorithm, this allocation should suffice your needs:

doubleArray = new double[data1.size()][2];
Sign up to request clarification or add additional context in comments.

Comments

1

in your java file you have to use double[] array. I think you are not declaring the proper size for the array

Comments

0

You are short of heap space. The default heap space is not enough for your program. You need to increase your heap space using the -Xms or -Xmx flag.

-Xms flag to specify minimum heap space. -Xmx flag to specify maximum heap space.

For example:

java -Xms512m -Xmx1024m YourMainClassName  

will set minimum heap space to 512 MB and maximum heap space to 1024 MB.

More reference here: http://www.mkyong.com/java/find-out-your-java-heap-memory-size/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.