Skip to content

Commit 097353f

Browse files
committed
GEODE-6662 NioPlainEngine.ensureWrappedCapacity
Fixing a memory leak: Return the old buffer to the Buffers pool after copying its contents to a newly allocated buffer. (cherry picked from commit 064892c)
1 parent ed13a72 commit 097353f

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

geode-core/src/main/java/org/apache/geode/internal/net/NioPlainEngine.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public ByteBuffer ensureWrappedCapacity(int amount, ByteBuffer wrappedBuffer,
7676
buffer = Buffers.acquireBuffer(bufferType, amount, stats);
7777
buffer.clear();
7878
buffer.put(oldBuffer);
79+
Buffers.releaseBuffer(bufferType, oldBuffer, stats);
7980
lastReadPosition = buffer.position();
8081
lastProcessedPosition = 0;
8182
}

geode-core/src/test/java/org/apache/geode/internal/net/NioPlainEngineTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,14 @@ public void unwrap() {
5858

5959
@Test
6060
public void ensureWrappedCapacity() {
61-
ByteBuffer wrappedBuffer = ByteBuffer.allocate(100);
61+
ByteBuffer wrappedBuffer = Buffers.acquireReceiveBuffer(100, mockStats);
62+
verify(mockStats, times(1)).incReceiverBufferSize(any(Integer.class), any(Boolean.class));
6263
wrappedBuffer.put(new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
6364
nioEngine.lastReadPosition = 10;
6465
int requestedCapacity = 210;
6566
ByteBuffer result = nioEngine.ensureWrappedCapacity(requestedCapacity, wrappedBuffer,
66-
Buffers.BufferType.UNTRACKED, mockStats);
67+
Buffers.BufferType.TRACKED_RECEIVER, mockStats);
68+
verify(mockStats, times(2)).incReceiverBufferSize(any(Integer.class), any(Boolean.class));
6769
assertThat(result.capacity()).isGreaterThanOrEqualTo(requestedCapacity);
6870
assertThat(result).isNotSameAs(wrappedBuffer);
6971
// make sure that data was transferred to the new buffer

0 commit comments

Comments
 (0)