Skip to content

Optimize ZipImporter to buffer streams to disk to prevent OOM - #192

Merged
petrberan merged 1 commit into
shrinkwrap:mainfrom
xjusko:issue#190-Optimize-ZipImporter-to-buffer-streams-to-disk
May 28, 2026
Merged

petrberan merged 1 commit into
shrinkwrap:mainfrom
xjusko:issue#190-Optimize-ZipImporter-to-buffer-streams-to-disk

Conversation

@xjusko

@xjusko xjusko commented Nov 27, 2025

Copy link
Copy Markdown
Contributor

fixes #190

Buffer the InputStream to a temporary disk file instead of holding the entire archive in the Java Heap.

@xjusko
xjusko requested a review from petrberan November 27, 2025 15:41

@petrberan petrberan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requesting changes based on our discussion last week

@xjusko
xjusko force-pushed the issue#190-Optimize-ZipImporter-to-buffer-streams-to-disk branch from a49ab08 to a228acd Compare February 27, 2026 20:29
@xjusko
xjusko requested a review from petrberan March 2, 2026 17:28
* Archives smaller than this threshold (10 MB) are kept in memory for performance.
* Larger archives are buffered to a temporary file to avoid OutOfMemoryErrors.
*/
private static final int DISK_BUFFER_THRESHOLD = 10 * 1024 * 1024;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 10 MB exactly?


ZipEntry entry;
while ((entry = zipStream.getNextEntry()) != null) {
// Get the name

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why removing the comments?

@xjusko
xjusko force-pushed the issue#190-Optimize-ZipImporter-to-buffer-streams-to-disk branch from a228acd to e263937 Compare March 13, 2026 16:13
memoryBuffer.write(buf, 0, bytesRead);
totalRead += bytesRead;

if (totalRead > DISK_BUFFER_THRESHOLD) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's assume an archive of 200 MB with the threshold set to 100 MB. What would happen in this case is that first 100 MB goes into heap, then it's exceeds the threshold and based on L#137 it copies the whole 200 MB into the file. Which kind of erases the purpose of this PR.

We should check if the archive would exceed the threshold first, then decide whether to use heap or disk

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that currently it works as we wanted.
First, the stream is read until a 100mb size is reached in the heap. Then this flushed to a temp file. The remaining 100mb is then copied into the same temp file (L#137).
We don't copy the full 200mb.

We can't check the size of a generic InputStream upfront since it has no size metadata.

@petrberan

Copy link
Copy Markdown
Member

Can you also add tests to the PR please?

* Larger archives are buffered to a temporary file to avoid OutOfMemoryErrors.
* Defaults to 100 MB, configurable via the {@value #DISK_BUFFER_THRESHOLD_PROPERTY} system property.
*/
private static final int DISK_BUFFER_THRESHOLD = Integer.getInteger(DISK_BUFFER_THRESHOLD_PROPERTY, 100) * 1024 * 1024;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use long

…old defaults to 100 MB, configurable via shrinkwrap.zipImporter.diskBufferThresholdMb system property.
@xjusko
xjusko force-pushed the issue#190-Optimize-ZipImporter-to-buffer-streams-to-disk branch from e263937 to 14fcfff Compare May 12, 2026 10:13
@xjusko

xjusko commented May 12, 2026

Copy link
Copy Markdown
Contributor Author

I added tests, but for testability, I also had to move threshold resolution from a static final field to a runtime lookup.

@xjusko
xjusko requested a review from petrberan May 18, 2026 08:22

@petrberan petrberan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's release a new version with the functionality in the current state and see if we would need to tweak it later

@petrberan
petrberan merged commit 8caf8ab into shrinkwrap:main May 28, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Please do not read the whole archive to memory in zip importer

2 participants