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;       
}
Ian2thedv
  • 2,691
  • 2
  • 26
  • 47
Chandu D
  • 505
  • 3
  • 17

3 Answers3

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];
Ian2thedv
  • 2,691
  • 2
  • 26
  • 47
ttekin
  • 827
  • 8
  • 9
1

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

Lakshmi Prasanna
  • 347
  • 1
  • 2
  • 9
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/

Aditya Singh
  • 2,343
  • 1
  • 23
  • 42