Bug 61895 - DefaultTempFileCreationStrategy uses File.deleteOnExit() which is a memory leak
Summary: DefaultTempFileCreationStrategy uses File.deleteOnExit() which is a memory leak
Status: RESOLVED WORKSFORME
Alias: None
Product: POI
Classification: Unclassified
Component: POI Overall (show other bugs)
Version: unspecified
Hardware: PC Mac OS X 10.1
: P2 enhancement (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-12-13 12:05 UTC by Jan-Willem Gmelig Meyling
Modified: 2017-12-24 11:37 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jan-Willem Gmelig Meyling 2017-12-13 12:05:17 UTC
The DefaultTempFileCreationStrategy uses File.deleteOnExit() which holds on to temporary files until the JVM exists. This causes the JVM heap size to grow indefinitely until you run into an OutOfMemory error. More problematically, this queue of files cannot be cleaned manually. So code that writes to spreadsheets regularly, MUST restart every once and a while. Hence all File.deleteOnExit calls should be removed.

See also: https://puneeth.wordpress.com/2006/01/23/filedeleteonexit-is-evil/
Comment 1 Dominik Stadler 2017-12-13 14:35:55 UTC
Apache POI provides a plugable temp file creation strategy for exactly this, so you can easily write your own which does not use File.deleteOnExit() and define it as the strategy to use in your application. 

If you succeed and think your approach would be useful for POI overall, then please share it here.
Comment 2 Jan-Willem Gmelig Meyling 2017-12-13 15:15:07 UTC
Would it be an idea to just remove all associated temp files whenever the workbook gets disposed?
Comment 3 Javen O'Neal 2017-12-13 18:22:42 UTC
(In reply to Jan-Willem Gmelig Meyling from comment #2)
> Would it be an idea to just remove all associated temp files whenever the
> workbook gets disposed?

No, because the user may create more than 1 temporary workbook at a time.
Deleting all temporary workbooks would be particularly bad if one of the open workbooks referenced another workbook in a formula or name reference.
This wouldn't make for a very good default strategy.

As suggested in the JavaDocs, you probably want to implement a FileCreationStrategy that is better suited to long-running JVM processes (such as in a Tomcat container). The default strategy is not suitable for your application. I think this is clear in the documentation.

See bug 59166 comment 4
https://poi.apache.org/apidocs/org/apache/poi/util/TempFileCreationStrategy.html

If you'd like to write a LongRunningJVMFileCreationStrategy class with the appropriate unit tests, we'd be happy to review and merge it.
Comment 4 Dominik Stadler 2017-12-24 11:37:26 UTC
Closing this issue for now, please reopen if you want to contribute a strategy that is useful in your case.