1

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(); 
    
Anthon
  • 69,918
  • 32
  • 186
  • 246
user530158
  • 333
  • 7
  • 18
  • 1
    Some people are re-creating the styles all over in a loop which leads to a OOME. You should check your code for that. – Kai Mar 10 '15 at 07:45
  • About 3. why not dispose at the end. It might be that because of the data sizes the write buffer is just written at that time. `flush` is done by `close` and not needed. Maybe [this](http://stackoverflow.com/questions/28857790/java-reading-large-file-with-charset/28858762#28858762) – Joop Eggen Mar 10 '15 at 08:00
  • after googling for issue 3 I think i don't need to flush and close output stream since this stream is managed by servlet container . – user530158 Mar 10 '15 at 10:14

2 Answers2

0
  1. use File.createTempFile()
  2. use File.deleteOnExit() plus a

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

block.

  1. I think it's safe.
ArturoTena
  • 713
  • 5
  • 15
  • Am not creating and disposing temp files.This is done internally by Apache POI API with SXSSFWorkbook wherein I need to specify Temp directory to flush extra rows from memory. The question is , is it safe since it is web application and there would be multiple requests(Threads) so one thread disposing temp file should not conflict with other requests temp files during report generation. – user530158 Mar 10 '15 at 10:12
0

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.

Aasmund Eldhuset
  • 37,289
  • 4
  • 68
  • 81