我应该重用DatagramPacket吗?

时间:2015-10-13 18:04:07

标签: java udp datagram object-pooling

我正在构建一个基于UDP的应用程序,它接收和发送多个数据包。

我可以为每个发送构建一个新的DatagramPacket,或者在我的应用程序的生命周期中回收一个实例。

  • 重用DatagramPacket有什么好处吗? (例如记忆分配)
  • 有任何潜在的问题吗? (例如线程安全)

1 个答案:

答案 0 :(得分:1)

看来你做不到。我只是尝试两次发送相同的DatagramPacket,但出现以下现象:

  • 数据包仅发送一次
  • Netty在下面记录警告

因此,您可以重复使用传递给DatagramPacket构造函数(主要是缓冲区)的参数,而不必重用DatagramPacket

  

2018-10-18 09:29:11.103警告5468 --- [nioEventLoopGroup-2-1]   i.n.c.AbstractChannelHandlerContext:无法将诺言标记为   失败,因为它已经失败了:   DefaultChannelPromise @ 748a9b0e(失败:   java.lang.IndexOutOfBoundsException:srcIndex:0),未通知原因:   java.lang.IndexOutOfBoundsException:srcIndex:0           在io.netty.buffer.UnsafeByteBufUtil.setBytes(UnsafeByteBufUtil.java:519)           在io.netty.buffer.PooledUnsafeDirectByteBuf.setBytes(PooledUnsafeDirectByteBuf.java:260)           在io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1080)           在io.netty.channel.nio.AbstractNioChannel.newDirectBuffer(AbstractNioChannel.java:481)           在io.netty.channel.socket.nio.NioDatagramChannel.filterOutboundMessage(NioDatagramChannel.java:308)           在io.netty.channel.AbstractChannel $ AbstractUnsafe.write(AbstractChannel.java:877)           在io.netty.channel.DefaultChannelPipeline $ HeadContext.write(DefaultChannelPipeline.java:1391)           在io.netty.channel.AbstractChannelHandlerContext.invokeWrite0(AbstractChannelHandlerContext.java:738)           在io.netty.channel.AbstractChannelHandlerContext.invokeWrite(AbstractChannelHandlerContext.java:730)           在io.netty.channel.AbstractChannelHandlerContext.access $ 1900(AbstractChannelHandlerContext.java:38)           在io.netty.channel.AbstractChannelHandlerContext $ AbstractWriteTask.write(AbstractChannelHandlerContext.java:1081)           在io.netty.channel.AbstractChannelHandlerContext $ WriteAndFlushTask.write(AbstractChannelHandlerContext.java:1128)           在io.netty.channel.AbstractChannelHandlerContext $ AbstractWriteTask.run(AbstractChannelHandlerContext.java:1070)           在io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)           在io.netty.util.concurrent.SingleThreadEventExecutor.runAllTask​​s(SingleThreadEventExecutor.java:404)           在io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:446)           在io.netty.util.concurrent.SingleThreadEventExecutor $ 5.run(SingleThreadEventExecutor.java:884)           在io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)           在java.lang.Thread.run(Thread.java:748)

     

io.netty.util.IllegalReferenceCountException:refCnt:0,减量:1           在io.netty.buffer.AbstractReferenceCountedByteBuf.release0(AbstractReferenceCountedByteBuf.java:100)   〜[netty-buffer-4.1.29.Final.jar!/:4.1.29.Final]           在io.netty.buffer.AbstractReferenceCountedByteBuf.release(AbstractReferenceCountedByteBuf.java:84)   〜[netty-buffer-4.1.29.Final.jar!/:4.1.29.Final]           在io.netty.util.ReferenceCountUtil.release(ReferenceCountUtil.java:88)   〜[netty-common-4.1.29.Final.jar!/:4.1.29.Final]           在io.netty.channel.DefaultAddressedEnvelope.release(DefaultAddressedEnvelope.java:101)   〜[netty-transport-4.1.29.Final.jar!/:4.1.29.Final]           在io.netty.util.ReferenceCountUtil.release(ReferenceCountUtil.java:88)   〜[netty-common-4.1.29.Final.jar!/:4.1.29.Final]           在io.netty.channel.AbstractChannel $ AbstractUnsafe.write(AbstractChannel.java:884)   〜[netty-transport-4.1.29.Final.jar!/:4.1.29.Final]           在io.netty.channel.DefaultChannelPipeline $ HeadContext.write(DefaultChannelPipeline.java:1391)   〜[netty-transport-4.1.29.Final.jar!/:4.1.29.Final]           在io.netty.channel.AbstractChannelHandlerContext.invokeWrite0(AbstractChannelHandlerContext.java:738)   [netty-transport-4.1.29.Final.jar!/:4.1.29.Final]           在io.netty.channel.AbstractChannelHandlerContext.invokeWrite(AbstractChannelHandlerContext.java:730)   [netty-transport-4.1.29.Final.jar!/:4.1.29.Final]           在io.netty.channel.AbstractChannelHandlerContext.access $ 1900(AbstractChannelHandlerContext.java:38)   [netty-transport-4.1.29.Final.jar!/:4.1.29.Final]           在io.netty.channel.AbstractChannelHandlerContext $ AbstractWriteTask.write(AbstractChannelHandlerContext.java:1081)   [netty-transport-4.1.29.Final.jar!/:4.1.29.Final]           在io.netty.channel.AbstractChannelHandlerContext $ WriteAndFlushTask.write(AbstractChannelHandlerContext.java:1128)   [netty-transport-4.1.29.Final.jar!/:4.1.29.Final]           在io.netty.channel.AbstractChannelHandlerContext $ AbstractWriteTask.run(AbstractChannelHandlerContext.java:1070)   [netty-transport-4.1.29.Final.jar!/:4.1.29.Final]           在io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)   [netty-common-4.1.29.Final.jar!/:4.1.29.Final]           在io.netty.util.concurrent.SingleThreadEventExecutor.runAllTask​​s(SingleThreadEventExecutor.java:404)   [netty-common-4.1.29.Final.jar!/:4.1.29.Final]           在io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:446)   [netty-transport-4.1.29.Final.jar!/:4.1.29.Final]           在io.netty.util.concurrent.SingleThreadEventExecutor $ 5.run(SingleThreadEventExecutor.java:884)   [netty-common-4.1.29.Final.jar!/:4.1.29.Final]           在io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)   [netty-common-4.1.29.Final.jar!/:4.1.29.Final]           在java.lang.Thread.run(Thread.java:748)[na:1.8.0_181]