Null buffer refs when closing SnappyOutputStream.
This commit is contained in:
parent
dcdada2ed4
commit
38ec9fd03b
|
@ -65,8 +65,9 @@ public class SnappyOutputStream extends OutputStream {
|
|||
private final BufferAllocator inputBufferAllocator;
|
||||
private final BufferAllocator outputBufferAllocator;
|
||||
|
||||
protected final byte[] inputBuffer;
|
||||
protected final byte[] outputBuffer;
|
||||
// The input and output buffer fields are set to null when closing this stream:
|
||||
protected byte[] inputBuffer;
|
||||
protected byte[] outputBuffer;
|
||||
private int inputCursor = 0;
|
||||
private int outputCursor = 0;
|
||||
private boolean closed;
|
||||
|
@ -340,6 +341,8 @@ public class SnappyOutputStream extends OutputStream {
|
|||
closed = true;
|
||||
inputBufferAllocator.release(inputBuffer);
|
||||
outputBufferAllocator.release(outputBuffer);
|
||||
inputBuffer = null;
|
||||
outputBuffer = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -30,10 +30,12 @@ import java.io.BufferedInputStream;
|
|||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.xerial.snappy.buffer.BufferAllocatorFactory;
|
||||
import org.xerial.snappy.buffer.CachedBufferAllocator;
|
||||
import org.xerial.snappy.buffer.DefaultBufferAllocator;
|
||||
import org.xerial.util.FileResource;
|
||||
import org.xerial.util.log.Logger;
|
||||
|
||||
|
@ -225,6 +227,18 @@ public class SnappyOutputStreamTest
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void closingStreamShouldMakeBuffersEligibleForGarbageCollection() throws IOException {
|
||||
ByteArrayOutputStream b = new ByteArrayOutputStream();
|
||||
SnappyOutputStream os = new SnappyOutputStream(b, 4095, DefaultBufferAllocator.factory);
|
||||
WeakReference<byte[]> inputBuffer = new WeakReference<byte[]>(os.inputBuffer);
|
||||
WeakReference<byte[]> outputBuffer = new WeakReference<byte[]>(os.inputBuffer);
|
||||
os.close();
|
||||
System.gc();
|
||||
assertNull(inputBuffer.get());
|
||||
assertNull(outputBuffer.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void longArrayCompress() throws Exception {
|
||||
long[] l = new long[10];
|
||||
|
|
Loading…
Reference in New Issue