Skip to content

Commit

Permalink
Eliminate usage of releaseLater(...) to reduce memory usage during tests
Browse files Browse the repository at this point in the history
Motiviation:

We used ReferenceCountUtil.releaseLater(...) in our tests which simplifies a bit the releasing of ReferenceCounted objects. The problem with this is that while it simplifies stuff it increase memory usage a lot as memory may not be freed up in a timely manner.

Modifications:

- Deprecate releaseLater(...)
- Remove usage of releaseLater(...) in tests.

Result:

Less memory needed to build netty while running the tests.
  • Loading branch information
normanmaurer committed Nov 18, 2016
1 parent a0e375b commit 0bc30a1
Show file tree
Hide file tree
Showing 36 changed files with 1,222 additions and 552 deletions.
203 changes: 153 additions & 50 deletions buffer/src/test/java/io/netty/buffer/AbstractByteBufTest.java

Large diffs are not rendered by default.

382 changes: 260 additions & 122 deletions buffer/src/test/java/io/netty/buffer/AbstractCompositeByteBufTest.java

Large diffs are not rendered by default.

92 changes: 65 additions & 27 deletions buffer/src/test/java/io/netty/buffer/ByteBufUtilTest.java
Expand Up @@ -23,7 +23,6 @@
import java.util.Random;

import static io.netty.buffer.Unpooled.unreleasableBuffer;
import static io.netty.util.ReferenceCountUtil.releaseLater;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -99,36 +98,45 @@ public void notEqualsBufferUnderflow() {
@Test
public void testWriteUsAscii() {
String usAscii = "NettyRocks";
ByteBuf buf = releaseLater(Unpooled.buffer(16));
ByteBuf buf = Unpooled.buffer(16);
buf.writeBytes(usAscii.getBytes(CharsetUtil.US_ASCII));
ByteBuf buf2 = releaseLater(Unpooled.buffer(16));
ByteBuf buf2 = Unpooled.buffer(16);
ByteBufUtil.writeAscii(buf2, usAscii);

assertEquals(buf, buf2);

buf.release();
buf2.release();
}

@Test
public void testWriteUsAsciiWrapped() {
String usAscii = "NettyRocks";
ByteBuf buf = unreleasableBuffer(releaseLater(Unpooled.buffer(16)));
ByteBuf buf = unreleasableBuffer(Unpooled.buffer(16));
assertWrapped(buf);
buf.writeBytes(usAscii.getBytes(CharsetUtil.US_ASCII));
ByteBuf buf2 = unreleasableBuffer(releaseLater(Unpooled.buffer(16)));
ByteBuf buf2 = unreleasableBuffer(Unpooled.buffer(16));
assertWrapped(buf2);
ByteBufUtil.writeAscii(buf2, usAscii);

assertEquals(buf, buf2);

buf.unwrap().release();
buf2.unwrap().release();
}

@Test
public void testWriteUtf8() {
String usAscii = "Some UTF-8 like äÄ∏ŒŒ";
ByteBuf buf = releaseLater(Unpooled.buffer(16));
ByteBuf buf = Unpooled.buffer(16);
buf.writeBytes(usAscii.getBytes(CharsetUtil.UTF_8));
ByteBuf buf2 = releaseLater(Unpooled.buffer(16));
ByteBuf buf2 = Unpooled.buffer(16);
ByteBufUtil.writeUtf8(buf2, usAscii);

assertEquals(buf, buf2);

buf.release();
buf2.release();
}

@Test
Expand All @@ -140,12 +148,15 @@ public void testWriteUtf8Surrogates() {
.append('\uDC00')
.append('b')
.toString();
ByteBuf buf = releaseLater(Unpooled.buffer(16));
ByteBuf buf = Unpooled.buffer(16);
buf.writeBytes(surrogateString.getBytes(CharsetUtil.UTF_8));
ByteBuf buf2 = releaseLater(Unpooled.buffer(16));
ByteBuf buf2 = Unpooled.buffer(16);
ByteBufUtil.writeUtf8(buf2, surrogateString);

assertEquals(buf, buf2);

buf.release();
buf2.release();
}

@Test
Expand All @@ -155,12 +166,15 @@ public void testWriteUtf8InvalidOnlyTrailingSurrogate() {
.append('\uDC00')
.append('b')
.toString();
ByteBuf buf = releaseLater(Unpooled.buffer(16));
ByteBuf buf = Unpooled.buffer(16);
buf.writeBytes(surrogateString.getBytes(CharsetUtil.UTF_8));
ByteBuf buf2 = releaseLater(Unpooled.buffer(16));
ByteBuf buf2 = Unpooled.buffer(16);
ByteBufUtil.writeUtf8(buf2, surrogateString);

assertEquals(buf, buf2);

buf.release();
buf2.release();
}

@Test
Expand All @@ -170,12 +184,15 @@ public void testWriteUtf8InvalidOnlyLeadingSurrogate() {
.append('\uD800')
.append('b')
.toString();
ByteBuf buf = releaseLater(Unpooled.buffer(16));
ByteBuf buf = Unpooled.buffer(16);
buf.writeBytes(surrogateString.getBytes(CharsetUtil.UTF_8));
ByteBuf buf2 = releaseLater(Unpooled.buffer(16));
ByteBuf buf2 = Unpooled.buffer(16);
ByteBufUtil.writeUtf8(buf2, surrogateString);

assertEquals(buf, buf2);

buf.release();
buf2.release();
}

@Test
Expand All @@ -186,12 +203,15 @@ public void testWriteUtf8InvalidSurrogatesSwitched() {
.append('\uD800')
.append('b')
.toString();
ByteBuf buf = releaseLater(Unpooled.buffer(16));
ByteBuf buf = Unpooled.buffer(16);
buf.writeBytes(surrogateString.getBytes(CharsetUtil.UTF_8));
ByteBuf buf2 = releaseLater(Unpooled.buffer(16));
ByteBuf buf2 = Unpooled.buffer(16);
ByteBufUtil.writeUtf8(buf2, surrogateString);

assertEquals(buf, buf2);

buf.release();
buf2.release();
}

@Test
Expand All @@ -202,12 +222,15 @@ public void testWriteUtf8InvalidTwoLeadingSurrogates() {
.append('\uD800')
.append('b')
.toString();
ByteBuf buf = releaseLater(Unpooled.buffer(16));
ByteBuf buf = Unpooled.buffer(16);
buf.writeBytes(surrogateString.getBytes(CharsetUtil.UTF_8));
ByteBuf buf2 = releaseLater(Unpooled.buffer(16));
ByteBuf buf2 = Unpooled.buffer(16);
ByteBufUtil.writeUtf8(buf2, surrogateString);

assertEquals(buf, buf2);

buf.release();
buf2.release();
}

@Test
Expand All @@ -218,62 +241,77 @@ public void testWriteUtf8InvalidTwoTrailingSurrogates() {
.append('\uDC00')
.append('b')
.toString();
ByteBuf buf = releaseLater(Unpooled.buffer(16));
ByteBuf buf = Unpooled.buffer(16);
buf.writeBytes(surrogateString.getBytes(CharsetUtil.UTF_8));
ByteBuf buf2 = releaseLater(Unpooled.buffer(16));
ByteBuf buf2 = Unpooled.buffer(16);
ByteBufUtil.writeUtf8(buf2, surrogateString);

assertEquals(buf, buf2);

buf.release();
buf2.release();
}

@Test
public void testWriteUtf8InvalidEndOnLeadingSurrogate() {
String surrogateString = new StringBuilder(2)
.append('\uD800')
.toString();
ByteBuf buf = releaseLater(Unpooled.buffer(16));
ByteBuf buf = Unpooled.buffer(16);
buf.writeBytes(surrogateString.getBytes(CharsetUtil.UTF_8));
ByteBuf buf2 = releaseLater(Unpooled.buffer(16));
ByteBuf buf2 = Unpooled.buffer(16);
ByteBufUtil.writeUtf8(buf2, surrogateString);

assertEquals(buf, buf2);

buf.release();
buf2.release();
}

@Test
public void testWriteUtf8InvalidEndOnTrailingSurrogate() {
String surrogateString = new StringBuilder(2)
.append('\uDC00')
.toString();
ByteBuf buf = releaseLater(Unpooled.buffer(16));
ByteBuf buf = Unpooled.buffer(16);
buf.writeBytes(surrogateString.getBytes(CharsetUtil.UTF_8));
ByteBuf buf2 = releaseLater(Unpooled.buffer(16));
ByteBuf buf2 = Unpooled.buffer(16);
ByteBufUtil.writeUtf8(buf2, surrogateString);

assertEquals(buf, buf2);

buf.release();
buf2.release();
}

@Test
public void testWriteUsAsciiString() {
AsciiString usAscii = new AsciiString("NettyRocks");
ByteBuf buf = releaseLater(Unpooled.buffer(16));
ByteBuf buf = Unpooled.buffer(16);
buf.writeBytes(usAscii.toString().getBytes(CharsetUtil.US_ASCII));
ByteBuf buf2 = releaseLater(Unpooled.buffer(16));
ByteBuf buf2 = Unpooled.buffer(16);
ByteBufUtil.writeAscii(buf2, usAscii);

assertEquals(buf, buf2);

buf.release();
buf2.release();
}

@Test
public void testWriteUtf8Wrapped() {
String usAscii = "Some UTF-8 like äÄ∏ŒŒ";
ByteBuf buf = unreleasableBuffer(releaseLater(Unpooled.buffer(16)));
ByteBuf buf = unreleasableBuffer(Unpooled.buffer(16));
assertWrapped(buf);
buf.writeBytes(usAscii.getBytes(CharsetUtil.UTF_8));
ByteBuf buf2 = unreleasableBuffer(releaseLater(Unpooled.buffer(16)));
ByteBuf buf2 = unreleasableBuffer(Unpooled.buffer(16));
assertWrapped(buf2);
ByteBufUtil.writeUtf8(buf2, usAscii);

assertEquals(buf, buf2);

buf.release();
buf2.release();
}

private static void assertWrapped(ByteBuf buf) {
Expand Down
13 changes: 8 additions & 5 deletions buffer/src/test/java/io/netty/buffer/ByteProcessorTest.java
Expand Up @@ -16,7 +16,6 @@

package io.netty.buffer;

import static io.netty.util.ReferenceCountUtil.releaseLater;
import static org.junit.Assert.assertEquals;
import io.netty.util.ByteProcessor;
import io.netty.util.CharsetUtil;
Expand All @@ -26,8 +25,8 @@
public class ByteProcessorTest {
@Test
public void testForward() {
final ByteBuf buf = releaseLater(
Unpooled.copiedBuffer("abc\r\n\ndef\r\rghi\n\njkl\0\0mno \t\tx", CharsetUtil.ISO_8859_1));
final ByteBuf buf =
Unpooled.copiedBuffer("abc\r\n\ndef\r\rghi\n\njkl\0\0mno \t\tx", CharsetUtil.ISO_8859_1);
final int length = buf.readableBytes();

assertEquals(3, buf.forEachByte(0, length, ByteProcessor.FIND_CRLF));
Expand All @@ -41,12 +40,14 @@ public void testForward() {
assertEquals(24, buf.forEachByte(21, length - 21, ByteProcessor.FIND_LINEAR_WHITESPACE));
assertEquals(28, buf.forEachByte(24, length - 24, ByteProcessor.FIND_NON_LINEAR_WHITESPACE));
assertEquals(-1, buf.forEachByte(28, length - 28, ByteProcessor.FIND_LINEAR_WHITESPACE));

buf.release();
}

@Test
public void testBackward() {
final ByteBuf buf = releaseLater(
Unpooled.copiedBuffer("abc\r\n\ndef\r\rghi\n\njkl\0\0mno \t\tx", CharsetUtil.ISO_8859_1));
final ByteBuf buf =
Unpooled.copiedBuffer("abc\r\n\ndef\r\rghi\n\njkl\0\0mno \t\tx", CharsetUtil.ISO_8859_1);
final int length = buf.readableBytes();

assertEquals(27, buf.forEachByteDesc(0, length, ByteProcessor.FIND_LINEAR_WHITESPACE));
Expand All @@ -60,5 +61,7 @@ public void testBackward() {
assertEquals(5, buf.forEachByteDesc(0, 9, ByteProcessor.FIND_CRLF));
assertEquals(2, buf.forEachByteDesc(0, 6, ByteProcessor.FIND_NON_CRLF));
assertEquals(-1, buf.forEachByteDesc(0, 3, ByteProcessor.FIND_CRLF));

buf.release();
}
}

0 comments on commit 0bc30a1

Please sign in to comment.