Repository navigation
Optimize ZipImporter to buffer streams to disk to prevent OOM - #192
Conversation
petrberan
left a comment
There was a problem hiding this comment.
Requesting changes based on our discussion last week
a49ab08 to
a228acd
Compare
| * 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; |
|
|
||
| ZipEntry entry; | ||
| while ((entry = zipStream.getNextEntry()) != null) { | ||
| // Get the name |
a228acd to
e263937
Compare
| memoryBuffer.write(buf, 0, bytesRead); | ||
| totalRead += bytesRead; | ||
|
|
||
| if (totalRead > DISK_BUFFER_THRESHOLD) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
|
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; |
…old defaults to 100 MB, configurable via shrinkwrap.zipImporter.diskBufferThresholdMb system property.
e263937 to
14fcfff
Compare
|
I added tests, but for testability, I also had to move threshold resolution from a static final field to a runtime lookup. |
petrberan
left a comment
There was a problem hiding this comment.
Let's release a new version with the functionality in the current state and see if we would need to tweak it later
fixes #190
Buffer the InputStream to a temporary disk file instead of holding the entire archive in the Java Heap.