简体   繁体   English

JAVA POI内存不足问题

[英]JAVA POI out of memory issue

I wanted to create excel(xlsx) report with 500000 records and hardly 10 cols.With jboss 512M and apache poi(3.9). 我想创建具有500000条记录且几乎没有10列的excel(xlsx)报告。使用jboss 512M和apache poi(3.9)。

JVM got crashed with out of memory error as expected since poi usermodel has memory issues. JVM由于内存不足错误而崩溃,因为poi用户模型存在内存问题。

So I tried with SXSSFWorkbook by providing temp directory to flush rows to disk. 因此,我尝试通过提供temp目录将行刷新到磁盘来使用SXSSFWorkbook。 The report got generated without any issue. 报告已生成,没有任何问题。

  1. I am generating report on web application wherein i am writing workbbok to servlet OP stream.Will this cause any issue if multiple users tried to fetch report on same time since temp dir is shared by multiple threads. 我正在生成关于Web应用程序的报告,其中我正在将Workbbok写到Servlet OP流中。如果由于多个线程共享temp目录,则多个用户试图同时获取报告,这将引起任何问题。

  2. I have also given call to dispose method of SXSSFWorkbook to cleanUp temp files, will it be safe since I doubt it should not delete temp files created by other threads(other user requests) 我也给了调用SXSSFWorkbook的dispose方法来清理临时文件的方法,这是否安全,因为我怀疑它是否应该删除其他线程创建的临时文件(其他用户请求)

  3. Also below code is safe ? 还下面的代码是安全的吗? Since I am disposing before closing output stream but after workbook is written to output stream. 由于我在关闭输出流之前但在将工作簿写入输出流之后进行处理。

     workbook.write(out); workbook.dispose(); out.flush(); out.close(); 
  1. use File.createTempFile() 使用File.createTempFile()
  2. use File.deleteOnExit() plus a 使用File.deleteOnExit()加上

try { /*...*/ } finally { file.delete() }

block. 块。

  1. I think it's safe. 觉得很安全。

It should be safe, as POI uses File.createTempFile() internally - the code is in org.apache.poi.util.TempFile.createTempFile(String, String) . 应该是安全的,因为POI内部使用File.createTempFile() -代码在org.apache.poi.util.TempFile.createTempFile(String, String) However, if you are running several web applications (with separate JVMs), you might get into trouble unless you have set the property poi.keep.tmp.files - POI creates its temp files inside a subdirectory called poifiles inside the temp directory, and tries to delete it on exit. 但是,如果您正在运行多个Web应用程序(具有单独的JVM),除非设置了属性poi.keep.tmp.files否则可能会遇到麻烦poifiles在temp目录内名为poifiles的子目录中创建其临时文件,并且尝试在退出时将其删除。 This will cause problems if other JVMs with POI are still running. 如果其他具有POI的JVM仍在运行,则将导致问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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