question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Garbage generated in EpollDatagramChannel

See original GitHub issue

I am developing a high performance UDP client on Linux using Netty 4.1 with native transport, and DatagramSocketAddress objects are the main source of allocations in the JVM. The native code EpollDatagramChannel creates a DatagramSocketAddress object for each received UDP datagram: https://github.com/netty/netty/blob/fa8f967852c04fd99ae15ee2bd1047595a97417e/transport-native-unix-common/src/main/c/netty_unix_socket.c#L363 Those allocations are quite heavy, as internally, char[], String, Inet4Address, InetAddressHolder, InetSocketAddressHolder, InetAddress[], byte[] objects are getting generated when constructing the object.

On the client side, if the socket is connected, the address cannot change and does not have to be re-instanciated every time. What do you think about caching the sender address in EpollDatagramChannel and allocating it only if needed? That is what is done in DatagramChannelImpl from the JDK, which considerably reduces GC: https://github.com/frohoff/jdk8u-jdk/blob/master/src/windows/native/sun/nio/ch/DatagramChannelImpl.c#L179

That can easily be reproduced using any echo client/server UDP example configured to be using native transport.

Expected behavior

EpollDatagramChannel caches the DatagramSocketAddress object when receiving a datagram

Actual behavior

EpollDatagramChannel allocates one DatagramSocketAddress object per incoming datagram

Steps to reproduce

Simple UDP client configured with native transport

Netty version

4.1

JVM version (e.g. java -version)

openjdk version “1.8.0_191” OpenJDK Runtime Environment (build 1.8.0_191-8u191-b12-0ubuntu0.16.04.1-b12) OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)

OS version (e.g. uname -a)

Ubuntu 16.04 x86_64 kernel 4.15.0-43-generic

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
normanmaurercommented, Jan 30, 2019

@estaban PTAL https://github.com/netty/netty/pull/8806. I think this actually fixes it in an easier way. Also I was thinking of maybe introducing a ChannelOption that would allow to set that if used in connected mode we would just fire the ByteBuf though the ChannelPipeline and not wrap it at all in a DatagramPacket. This would reduce GC even more. WDYT ?

0reactions
estabancommented, Jan 30, 2019

@normanmaurer Thanks for the fix, it is much simpler than the one I proposed. When I looked into that option, I was concerned about race conditions between connect calls (potentially several of them?) and reads (using the cached address), but I missed that they are all done on the event loop thread, so that should not be a problem.

Firing a ByteBuf when reading UDP datagrams would be a great idea!

Read more comments on GitHub >

github_iconTop Results From Across the Web

real-logic/Aeron - Gitter
Java due to some limitations on DatagramChannel.receive may generate some garbage if the receiving side has more than 1 source sending to it....
Read more >
Index (Netty API Reference (4.0.56.Final))
Creates a new receive buffer whose capacity is probably large enough to read all inbound data and small enough not to waste its...
Read more >
Native Reference Guide - Quarkus
The garbage collectors available for Quarkus users are currently Serial GC and ... Optionally, the native build process can generate reports that show...
Read more >
MINA 2.2.1 released posted on july, 24, 2022 - Apache MINA
DIRMINA-966 NIO Datagram messages can get duplicated when unable to be sent by the underlying DatagramChannel; DIRMINA-1014 SocketAcceptor doesn't unbind ...
Read more >
JamaicaVM 8.3 User Manual - aicas GmbH
9.7.3 Configuring a Realtime Garbage Collector . ... opment environment on which the Jamaica Builder runs to generate code for the.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found