尝试使用Apache POI编写excel文件导致OutOfMemoryError

Luc*_*lli 0 java memory excel apache-poi

我有一个编写excel文件的程序.它使用Apache POI编写excel 2007文件(我有超过256个colums,所以我必须使用它).该计划有效.我已经在非常小的文件上测试了它,但是如果我使用更多的行,它会耗尽内存.

这是堆栈跟踪:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Unknown Source)
    at java.io.ByteArrayOutputStream.write(Unknown Source)
    at org.apache.poi.openxml4j.opc.internal.MemoryPackagePartOutputStream.write(MemoryPackagePartOutputStream.java:88)
    at org.apache.xmlbeans.impl.store.Cursor._save(Cursor.java:590)
    at org.apache.xmlbeans.impl.store.Cursor.save(Cursor.java:2544)
    at org.apache.xmlbeans.impl.values.XmlObjectBase.save(XmlObjectBase.java:212)
    at org.apache.poi.xssf.usermodel.XSSFSheet.write(XSSFSheet.java:2480)
    at org.apache.poi.xssf.usermodel.XSSFSheet.commit(XSSFSheet.java:2439)
    at org.apache.poi.POIXMLDocumentPart.onSave(POIXMLDocumentPart.java:196)
    at org.apache.poi.POIXMLDocumentPart.onSave(POIXMLDocumentPart.java:200)
    at org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:204)
    at model.Conversione.traduzioneFile(Conversione.java:219)
    at model.Main.scriviFile(Main.java:75)
    at model.Main.main(Main.java:51)
Run Code Online (Sandbox Code Playgroud)

在我写"workbook.write(fileOut)"的行发生错误(根据堆栈跟踪),其中fileOut是FileOutputStream.这意味着所有java对象显然有足够的内存来存储excel文件,但出于某种原因,因为它写入硬盘它必须占用更多的内存.

只是告诉你,我试图将java堆大小增加到1 gig(通过添加-Xms128m -Xmx1024m),但是仍然没有做到tric.

救命!吴


代码示例:

..

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

//I'M USING A DATABASE 
import DAO.EventoDAO;
import DAO.ParametroDAO;

public class Conversion {

public static void traduzioneFile(File read, File write){
    FileOutputStream fos=null;


    try {
        fos = new FileOutputStream(write);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    if (fos!=null) {

        try{

            Workbook wb = new XSSFWorkbook() ;

            Sheet sheet = wb.createSheet();

            //I'm reading from a table in a .txt file , converting values, and putting them in a table..

            FileInputStream fis;
            try {
                fis = new FileInputStream(fileLettura);
                InputStreamReader isr=new InputStreamReader(fis);
                BufferedReader br=new BufferedReader(isr);
                String line=br.readLine();

                //here there are some variables
                while(line!=null) {

                    Row row = null;
                    row=sheet.createRow((short)row_number);


                                            //arrayLinea contains all the words of the line
                    while (column_number<arrayLinea.length){
                    value=arrayLinea[column_number];
 //if value is ok i translate it and put it in a cell
                       row.createCell((short)contatoreColonne).setCellValue(value);
                                    contatoreColonne++                                  

                        }
                        //next line
                        linea=br.readLine();
                        row_line++;

                }



        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }catch (Exception ex){
            ex.printStackTrace();

        }


        wb.write(fos);
        fos.flush();
        fos.close();

    }catch (FileNotFoundException e){
    }catch (IOException e){
    }catch (Exception e){

    }

}
}
Run Code Online (Sandbox Code Playgroud)

我希望它是可读的..但是我正在扫描每一行,每列翻译值列,将它们放在单元格中...那部分是好的..我用systems.out.println ^^测试了它但是在最后一行之后说"翻译完成,开始写作",错误发生..

Tur*_*smo 11

使用POI编写.xlsx文件会占用大量内存.1演出可能还不够.

最近Apache POI引入了一个新的API(SXSSF),它是一个用于编写.xlsx文件的流实现.我还没有用过它,但也许这是你可以研究的东西.

  • 我刚刚尝试过 - 用 SXSSFWorkbook() 关闭了 XSSFWorkbook(),一切都立即完美运行,非常非常快。谢谢你! (2认同)

Oh *_*oon 0

您应该放置一个调试断点并跟踪您的代码。

我相信某个地方有一个永远不会结束的无限循环并导致 OOM 错误。