diff --git a/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java b/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java index 7c572e3ffc..57f59d4c43 100644 --- a/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java +++ b/buffer/src/main/java/io/netty/buffer/CompositeByteBuf.java @@ -1583,8 +1583,11 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf { } freed = true; - for (Component c: components) { - c.freeIfNecessary(); + int size = components.size(); + // We're not using foreach to avoid creating an iterator. + // see https://github.com/netty/netty/issues/2642 + for (int i = 0; i < size; i++) { + components.get(i).freeIfNecessary(); } if (leak != null) {