Repository navigation
Description
For ZIP, PGPCompressedData passes its own new Inflater(true) to InflaterInputStream, but InflaterInputStream.close() only ends an Inflater it created itself. This one is never ended, so its native memory stays allocated until GC. Affects both getDataStream() and getDataStream(long). ZLIB is not affected.
Each stream holds roughly 23 KB of native zlib memory that the GC doesn't see, so under load it can build up between GCs. On JDK 25, 20,000 read/close cycles with GC deferred grew RSS by about 555 MB for ZIP against about 99 MB for ZLIB. With this fix ZIP drops to the same 99 MB.
bc-java/pg/src/main/java/org/bouncycastle/openpgp/PGPCompressedData.java
Lines 152 to 184 in 6560e91
| if (this.getAlgorithm() == ZIP) | |
| { | |
| return new InflaterInputStream(this.getInputStream(), new Inflater(true)) | |
| { | |
| // If the "nowrap" inflater option is used the stream can | |
| // apparently overread - we override fill() and provide | |
| // an extra byte for the end of the input stream to get | |
| // around this. | |
| // | |
| // Totally weird... | |
| // | |
| protected void fill() throws IOException | |
| { | |
| if (eof) | |
| { | |
| throw new EOFException("Unexpected end of ZIP input stream"); | |
| } | |
| len = this.in.read(buf, 0, buf.length); | |
| if (len == -1) | |
| { | |
| buf[0] = 0; | |
| len = 1; | |
| eof = true; | |
| } | |
| inf.setInput(buf, 0, len); | |
| } | |
| private boolean eof = false; | |
| }; | |
| } |
The write side already handles this: SafeDeflaterOutputStream.close() calls def.end().
Activity