Null buffer refs when closing SnappyOutputStream.

This commit is contained in:
Josh Rosen 2015-05-14 11:22:34 -07:00
parent dcdada2ed4
commit 38ec9fd03b
2 changed files with 19 additions and 2 deletions

View File

@ -65,8 +65,9 @@ public class SnappyOutputStream extends OutputStream {
private final BufferAllocator inputBufferAllocator; private final BufferAllocator inputBufferAllocator;
private final BufferAllocator outputBufferAllocator; private final BufferAllocator outputBufferAllocator;
protected final byte[] inputBuffer; // The input and output buffer fields are set to null when closing this stream:
protected final byte[] outputBuffer; protected byte[] inputBuffer;
protected byte[] outputBuffer;
private int inputCursor = 0; private int inputCursor = 0;
private int outputCursor = 0; private int outputCursor = 0;
private boolean closed; private boolean closed;
@ -340,6 +341,8 @@ public class SnappyOutputStream extends OutputStream {
closed = true; closed = true;
inputBufferAllocator.release(inputBuffer); inputBufferAllocator.release(inputBuffer);
outputBufferAllocator.release(outputBuffer); outputBufferAllocator.release(outputBuffer);
inputBuffer = null;
outputBuffer = null;
} }
} }

View File

@ -30,10 +30,12 @@ import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.lang.ref.WeakReference;
import org.junit.Test; import org.junit.Test;
import org.xerial.snappy.buffer.BufferAllocatorFactory; import org.xerial.snappy.buffer.BufferAllocatorFactory;
import org.xerial.snappy.buffer.CachedBufferAllocator; import org.xerial.snappy.buffer.CachedBufferAllocator;
import org.xerial.snappy.buffer.DefaultBufferAllocator;
import org.xerial.util.FileResource; import org.xerial.util.FileResource;
import org.xerial.util.log.Logger; 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 @Test
public void longArrayCompress() throws Exception { public void longArrayCompress() throws Exception {
long[] l = new long[10]; long[] l = new long[10];