Skip to content

PGPCompressedData.getDataStream() never ends the Inflater for ZIP data #2580

Backlog

Description

@KubaJagiello

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.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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