Skip to content

Apache POI Temporary Files Not Automatically Cleaned Up When Exporting XLSX Files with Many Images, Leading to Disk Exhaustion #879

@Programmer-yyds

Description

@Programmer-yyds

Environment:

  • Java 21
  • Apache POI 5.4.0

Description:
When using Apache POI to export an XLSX file containing a large number of images, the program encounters an OOM error.
To mitigate the memory issue, I added the following configuration:

static {
    ZipPackage.setUseTempFilePackageParts(true);
}

This configuration successfully resolved the OOM problem, but introduced a new issue:

  • A large number of temporary files are created during the XLSX file generation process
  • These temporary files are not automatically deleted after the program finishes
  • Frequent exports cause temporary files to accumulate, eventually exhausting container disk space

Expected Result:

  • Provide an automatic cleanup mechanism for temporary files, or release the related temporary file resources when the Workbook is closed
  • If cleanup cannot be done on close, in multi-threaded scenarios, generate separate temporary file directories for each file to distinguish them and safely clean up

Steps to Reproduce:

  1. Create an export logic for an XLSX file containing many images
  2. Enable ZipPackage.setUseTempFilePackageParts(true)
  3. Repeatedly perform the export operation and observe the temporary file directory (e.g., /tmp)

Impact:

  • A large number of temporary files occupy disk space, potentially exhausting container storage

Temporary Workaround & Limitations:

  • Manually clean up temporary files, but with the following issues:
    • In a multi-threaded environment, all threads write temporary files to the same directory
    • Cannot distinguish between temporary files from completed exports and those still being generated
    • Deleting files blindly may corrupt files that are still being created

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions