简体   繁体   中英

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).

JVM got crashed with out of memory error as expected since poi usermodel has memory issues.

So I tried with SXSSFWorkbook by providing temp directory to flush rows to disk. 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.

  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)

  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()
  2. use File.deleteOnExit() plus a

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) . 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. This will cause problems if other JVMs with POI are still running.

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