garbage-free java.nio

676 views
Skip to first unread message

John Hening

unread,
Mar 11, 2018, 4:25:32 PM3/11/18
to mechanical-sympathy
Hello,

recently I am interested in non-blokcing java api for networking. It seems to be great. However, I would like to implement garbage-free solution. I am trying to do it only for learning purpose (I know that I don't implement a "better" solution). Especially, my solution is going to be garbage-free.

How to start? I see that jdk implementation is not garbage-free. And what? My only idea is to implement a small native library (only for Linux) and implement something like "facade" and a lot of stuff around that in Java (but, garbage-free).
I suppose that it can be very hard to integrate it with selectors for example (if possible). But, I don't see another solution and this is why I wrote here. What do you think? What is the best approach?

Martin Thompson

unread,
Mar 12, 2018, 5:14:56 AM3/12/18
to mechanical-sympathy
There are challenges with implementing a communications system (messaging or RPC) on top of NIO with Java but it can be done. If you want to learn about this you could study code bases that have done this. Aeron and Netty are two examples. For reference Aeron is garbage free once connections are established regardless of the number of messages exchanged. To achieve this you have to do some ugly things with NIO to work around its weaknesses.

Michael Guyver

unread,
Mar 12, 2018, 10:38:41 AM3/12/18
to mechanica...@googlegroups.com
On Sunday, March 11, 2018 at 8:25:32 PM UTC, John Hening wrote:
recently I am interested in non-blokcing java api for networking. It seems to be great. However, I would like to implement garbage-free solution. I am trying to do it only for learning purpose (I know that I don't implement a "better" solution). Especially, my solution is going to be garbage-free.

 Hi John,

I've looked into this as part of a toy project and as Martin says you end up with JNI. At that point you could just as easily hand-off the networking layer to something like libev and be done with it, depending on your needs. 

Of course, a 0-GC TLS (client) in Java is a real ask. ;)

Regards

Mike

Roman Leventov

unread,
Mar 12, 2018, 11:25:42 AM3/12/18
to mechanica...@googlegroups.com
one.nio.net package of one-nio library (https://github.com/odnoklassniki/one-nio/tree/master/src/one/nio/net) is basically a reimplementation of java.nio. It does less locking, and, I suppose, allocates less garbage. You may find it useful.

On 12 March 2018 at 15:38, Michael Guyver <michael...@gmail.com> wrote:
On Sunday, March 11, 2018 at 8:25:32 PM UTC, John Hening wrote:
recently I am interested in non-blokcing java api for networking. It seems to be great. However, I would like to implement garbage-free solution. I am trying to do it only for learning purpose (I know that I don't implement a "better" solution). Especially, my solution is going to be garbage-free.

 Hi John,

I've looked intot this as part of a toy project and as Martin says you end up with JNI. At that point you could just as easily hand-off the networking layer to something like libev and be done with it, depending on your needs. 

Of course, a 0-GC TLS (client) in Java is a real ask. ;)

Regards

Mike

--
You received this message because you are subscribed to the Google Groups "mechanical-sympathy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mechanical-sympathy+unsub...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

John Hening

unread,
Mar 12, 2018, 3:22:09 PM3/12/18
to mechanical-sympathy
Thanks! :)

John Hening

unread,
Mar 18, 2018, 2:44:44 PM3/18/18
to mechanica...@googlegroups.com
Ok, I looked into possible approaches of implementation garbage-free nio. Let's focus on network communication. For example, for arriving conneciton I have to make an instance of socket (my own implementation) that represents a connection. Socket is an object and it is not so easy to allocate it off-heap (if possible, is it?). Possibilities I see:

1. Just pooling objects.
     It is kind of solution but it doesn't seem to be approperiate here.
2. Just treat  a socket (and other objects) as simple struct. We can be cunning and allocate chunk of memory with Unsafe.allocateMemory and use it as structs storage. But, in that case JVM is not aware of socket object and it is not pure :]

3. Extend JNI interface and try to allocate  natively with malloc (it doesn't seems to be very tricky- I could take pattern from alloc_object (jni.cpp). Obviously, it is not a solution, it is a workaround. (But, so instructive! :) )

What do you think?


Reply all
Reply to author
Forward
0 new messages