大家好:
最近研究netty中遇到一个奇怪的问题,
客户端采用tcp工具模拟,服务端是netty编写,在handler中循环向客户端发送数据,一段时间后:
客户端讲收不到任何数据:
服务端任然在正常发送数据,并且当我把客户端断开连接后,服务器并无响应,并且发送数据不报任何错误,服务器任然能正常发送错误,但实际上客户端已经断开连接了。而且发送数据的furture监听方法也不在执行!
服务端代码如下:
while(true){
String srcData = new String(bytes, "UTF-8");
if(!ctx.channel().isActive() )return ;
sendData(srcData);
}
/**
* 发送GPS数据至客户端
* @throws ClosedChannelException
*/
private void sendData(String srcData) {
//System.out.println(ctx.channel().isActive());
ChannelFuture future = ctx.channel().writeAndFlush(srcData);
boolean is = future.isSuccess();
//System.out.println(future.isSuccess());
/**
* 监听发送状态..
*/
//future.awaitUninterruptibly(5 * 1000);
future.addListener(new ChannelFutureListener() {
public void operationComplete(ChannelFuture future) {
if (future.isSuccess()) {
logger.debug(future.channel() + ",成功发送GPS数据.");
} else {
Channel channel = future.channel();
Throwable cause = future.cause();
logger.error("当前channel[{"+channel+"}]发送GPS数据包失败.",cause);
ctx.channel().close();
}
};
});
}
并且发送数据全部累积在netty的发送队列中,导致内存溢出:
2015-05-17 20:57:55.434 INFO (MessageExecutor.java:80)-5s转发客户端[/192.168.220.1:59294] 137 条,累计发送 594595 条
2015-05-17 20:58:36.360 WARN (SingleThreadEventExecutor.java:114)-Unexpected exception from an event executor:
java.lang.OutOfMemoryError: Java heap space
2015-05-17 20:58:36.360 ERROR (SenderHandler.java:67)-系统异常信息!
java.lang.OutOfMemoryError: Java heap space
at java.nio.ByteBuffer.wrap(ByteBuffer.java:350)
at java.lang.StringCoding$StringDecoder.decode(StringCoding.java:137)
at java.lang.StringCoding.decode(StringCoding.java:173)
at java.lang.String.(String.java:443)
at java.lang.String.(String.java:515)
at com.palmgo.datasource.kafka.MessageExecutor.consumer(MessageExecutor.java:159)
at com.palmgo.datasource.handler.SenderHandler.channelActive(SenderHandler.java:32)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelActive(AbstractChannelHandlerContext.java:183)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelActive(AbstractChannelHandlerContext.java:169)
at io.netty.channel.ChannelInboundHandlerAdapter.channelActive(ChannelInboundHandlerAdapter.java:64)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelActive(AbstractChannelHandlerContext.java:183)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelActive(AbstractChannelHandlerContext.java:169)
at io.netty.channel.ChannelInboundHandlerAdapter.channelActive(ChannelInboundHandlerAdapter.java:64)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelActive(AbstractChannelHandlerContext.java:183)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelActive(AbstractChannelHandlerContext.java:169)
at io.netty.channel.ChannelInboundHandlerAdapter.channelActive(ChannelInboundHandlerAdapter.java:64)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelActive(AbstractChannelHandlerContext.java:183)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelActive(AbstractChannelHandlerContext.java:169)
at io.netty.channel.DefaultChannelPipeline.fireChannelActive(DefaultChannelPipeline.java:817)
at io.netty.channel.AbstractChannel$AbstractUnsafe.register0(AbstractChannel.java:454)
at io.netty.channel.AbstractChannel$AbstractUnsafe.access$100(AbstractChannel.java:378)
at io.netty.channel.AbstractChannel$AbstractUnsafe$1.run(AbstractChannel.java:424)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:357)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:357)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:111)
at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137)
at java.lang.Thread.run(Thread.java:662)
2015-05-17 20:58:39.728 WARN (DefaultChannelPipeline.java:1035)-An exceptionCaught() event was fired, and it reached at the tail of the pipeline. It usually means the last handler in the pipeline did not handle the exception.
java.lang.OutOfMemoryError: Java heap space
请问netty 这种情况该如何处理呢?