Last active
December 23, 2015 02:29
Trying to output 1GB of zero bytes in chunks. Seems to have a memory leak....
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.codingtony.gist; | |
import static io.netty.handler.codec.http.HttpHeaders.Names.*; | |
import static io.netty.handler.codec.http.HttpResponseStatus.*; | |
import static io.netty.handler.codec.http.HttpVersion.*; | |
import java.io.ByteArrayInputStream; | |
import java.io.FileInputStream; | |
import java.nio.ByteBuffer; | |
import com.google.common.io.ByteStreams; | |
import com.google.common.primitives.Bytes; | |
import com.ticksmith.server.common.ChunkedOutputStream; | |
import io.netty.bootstrap.ServerBootstrap; | |
import io.netty.buffer.ByteBuf; | |
import io.netty.buffer.Unpooled; | |
import io.netty.channel.ChannelFutureListener; | |
import io.netty.channel.ChannelHandlerAdapter; | |
import io.netty.channel.ChannelHandlerContext; | |
import io.netty.channel.ChannelInitializer; | |
import io.netty.channel.ChannelPipeline; | |
import io.netty.channel.EventLoopGroup; | |
import io.netty.channel.SimpleChannelInboundHandler; | |
import io.netty.channel.nio.NioEventLoopGroup; | |
import io.netty.channel.socket.SocketChannel; | |
import io.netty.channel.socket.nio.NioServerSocketChannel; | |
import io.netty.handler.codec.http.DefaultHttpContent; | |
import io.netty.handler.codec.http.DefaultHttpResponse; | |
import io.netty.handler.codec.http.FullHttpMessage; | |
import io.netty.handler.codec.http.HttpHeaders; | |
import io.netty.handler.codec.http.HttpObjectAggregator; | |
import io.netty.handler.codec.http.HttpRequestDecoder; | |
import io.netty.handler.codec.http.HttpResponseEncoder; | |
import io.netty.handler.codec.http.LastHttpContent; | |
import io.netty.handler.stream.ChunkedWriteHandler; | |
public class TestOutOfMemory { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) throws Exception { | |
EventLoopGroup bossGroup = new NioEventLoopGroup(); | |
EventLoopGroup workerGroup = new NioEventLoopGroup(); | |
try { | |
ServerBootstrap b = new ServerBootstrap(); | |
b.group(bossGroup, workerGroup); | |
b.channel(NioServerSocketChannel.class); | |
b.childHandler(new ChannelInitializer<SocketChannel>() { | |
@Override | |
protected void initChannel(SocketChannel ch) throws Exception { | |
ChannelPipeline pipeline = ch.pipeline(); | |
ChannelHandlerAdapter testChunkOutOfMemory = new SimpleChannelInboundHandler<FullHttpMessage>() { | |
@Override | |
protected void channelRead0(ChannelHandlerContext ctx, FullHttpMessage msg) throws Exception { | |
DefaultHttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK); | |
HttpHeaders.setTransferEncodingChunked(response); | |
response.headers().set(CONTENT_TYPE, "application/octet-stream"); | |
ctx.write(response); | |
ByteBuf buf = Unpooled.buffer(); | |
int GIGABYTE = (4 * 1024 * 1024); // multiply 256B = 1GB | |
for (int i = 0; i < GIGABYTE; i++) { | |
buf.writeBytes(CONTENT_256BYTES_ZEROED); | |
ctx.writeAndFlush(new DefaultHttpContent(buf.copy())); | |
buf.clear(); | |
} | |
ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT).addListener( | |
ChannelFutureListener.CLOSE); | |
} | |
}; | |
pipeline.addLast("decoder", new HttpRequestDecoder()); | |
pipeline.addLast("aggregator", new HttpObjectAggregator(65536)); | |
pipeline.addLast("encoder", new HttpResponseEncoder()); | |
pipeline.addLast("chunkedWriter", new ChunkedWriteHandler()); | |
pipeline.addLast("handler", testChunkOutOfMemory); | |
} | |
}); | |
b.bind(8888).sync().channel().closeFuture().sync(); | |
} finally { | |
bossGroup.shutdownGracefully(); | |
workerGroup.shutdownGracefully(); | |
} | |
} | |
final static byte[] CONTENT_256BYTES_ZEROED = new byte[] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, | |
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, | |
0x0, 0x0, 0x0, 0x0, 0x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