0

I'm looking for a UDP library for Java that works garbage free. The reason is that i'm developing a daemon application for a real-time system. My requirement is latency of 5 micro-sec per request (from arrival to my socket until the response gets to destination).

There are a couple of messaging services who support UDP and produce no garbage (Tibco FTL & Aeron), but both require that all communicating components will use the flatform.

My situation is that I have no control over the other components, all I know is that i'm going to get UDP messages to my socket and I need to handle them without producing any garbage.

Will appreciate any ideas :)

Sam Mason
  • 15,216
  • 1
  • 41
  • 60
bost
  • 38
  • 3
  • could you be more specific? it seems possible to use standard `java.net` stuff without generating garbage. just do all allocations up front and things should be fine – Sam Mason Jul 22 '20 at 12:10
  • What do you mean by "garbage free?" – NomadMaker Jul 22 '20 at 14:56
  • @NomadMaker I mean that I don't want to generate garbage in order to prevent GC "Stop The World" behavior. – bost Jul 23 '20 at 11:36
  • @SamMason do you mean pre-allocating a pool of `DatagramPacket` objects and reuse them? – bost Jul 23 '20 at 11:37
  • @SamMason According to [this thread](https://stackoverflow.com/questions/33109579/should-i-reuse-datagrampacket) you can't reuse `DatagramPacket`, but you can reuse the content (buffers), which could reduce the amount of garbage, but couldn't eliminate it. – bost Jul 23 '20 at 11:45
  • I did have a problem with garbage collection many years ago, and found that manually calling the garbage collection at intervals stopped the problem. – NomadMaker Jul 23 '20 at 13:39
  • @bost some libraries might not like `DatagramPacket`'s being reused, but `java.net` is fine with it. can post a demo in an answer if that would help – Sam Mason Jul 23 '20 at 14:36
  • @SamMason that would be great! – bost Jul 27 '20 at 07:26
  • Wanted to add a comment - if you use the DatagramChannel (nio) every call to receive(), provided the packet was from a different source than the previous one, will allocate a new SocketAddress object. As far as I can tell, there is no way to get rid of this. – dan.m was user2321368 Aug 21 '20 at 14:05

2 Answers2

1

What do you mean by no garbage ? In UDP you may lose messages, this is inherent to this protocol. If you want to have some reliability on top of UDP you need to have an additional layer to manage acks, retransmissions and etc.. This means that your producer and consumer applications have to use this layer API to send / receive messages.

EmmanuelM
  • 337
  • 1
  • 6
  • I mean that I don't want to generate garbage in order to prevent GC "Stop The World" behavior. – bost Jul 23 '20 at 11:46
  • Ok then the recommendation is to use G1GC garbage collector if you using an ORACLE JVM (Linux and Windows). – EmmanuelM Jul 23 '20 at 15:17
0

A very short example of this is just:

import java.lang.System;

public class UdpNoGc {
    public static void main(String[] args) throws Exception {
        var buf = new byte[1024];
        var pkt = new java.net.DatagramPacket(buf, buf.length);

        try (var sock = new java.net.DatagramSocket(4321)) {
            for (;;) {
                sock.receive(pkt);

                System.out.write(buf, 0, pkt.getLength());

                System.gc();
            }
        }
    }
}

the explicit System.gc() call is just there so you can run with:

java "-Xlog:gc,heap*" UdpNoGc

and see that it doesn't allocate anything after startup. Note that writing code that doesn't allocate is somewhat difficult in Java, it might be easier using another language that provides more support for this.

Sam Mason
  • 15,216
  • 1
  • 41
  • 60
  • Thank you @SamMason, that's very helpful! Unfortunately I'm limited to Java, so hopefully this solution will do the trick. – bost Jul 28 '20 at 05:58