[zeromq-dev] Zero Copy in Java jzmq

Trevor Bernard trevor.bernard at gmail.com
Fri Feb 15 18:01:54 CET 2013

> I'm thinking of extending jzmq to optionally allow the zero-copy
> technique using the concepts outlined by Martin Thompson here:

That's awesome

> I'm not 100% sure I'll do it, I need some more experimentation first,
> but is there any interest or words of advice if someone's tried this
> already?

I'd love for send/recv to have a ByteBuffer API.

Some resources and general suggestions:

Have a peak at zmq_msg_t in zmq.h.

Defines ZMTP/2.0 Spec:
* http://rfc.zeromq.org/spec:15

Also depending on the size of the msg, it may be allocated on the stack or heap.
* http://api.zeromq.org/3-2:zmq-msg-init-size

At some point you'll have to access a malloc'ed array from Java/JNI.
* http://docs.oracle.com/javase/6/docs/technotes/guides/jni/spec/functions.html#nio_support

Something like this would do the trick. This wraps a native pointer in
a ByteBufer

void *data = ...
jobject bb = (*env)->NewDirectByteBuffer(env, (void*) data, sizeof(zmq_msg_t));

Here is another option: Use a library like javolution to do your mapping:


class Message extends Struct {
   Unsigned8 _ = new Unsigned8(32);
    Message() {
        setByteBuffer(Message.nativeBuffer(), 0);
    private static native ByteBuffer nativeBuffer();

Something along those lines.

Hope this helps.


More information about the zeromq-dev mailing list