Issue with DirectByteBuffers

G

gkrasnow

I am working on a system where we continually jump between native code
and java code. I have a place where I use NewDirectByteBuffer in the
native code to create a data buffer I can use to pass data back and
forth from java.

I am finding that under certain circumstances I call
NewDirectByteBuffer() with an address and size. I can then call
GetDirectBufferCapacity() and see that the size is indeed the size I
used in my NewDirectByteBuffer() call. However, when I then pass the
jobject across the JNI boundary back into java and I look at the
capacity() on the java.nio.ByteBuffer, the capacity reported is
smaller then what I requested. Has anyone else run into this issue?
Thanks.

- Greg
 
Z

Zig

I am working on a system where we continually jump between native code
and java code. I have a place where I use NewDirectByteBuffer in the
native code to create a data buffer I can use to pass data back and
forth from java.

I am finding that under certain circumstances I call
NewDirectByteBuffer() with an address and size. I can then call
GetDirectBufferCapacity() and see that the size is indeed the size I
used in my NewDirectByteBuffer() call. However, when I then pass the
jobject across the JNI boundary back into java and I look at the
capacity() on the java.nio.ByteBuffer, the capacity reported is
smaller then what I requested. Has anyone else run into this issue?
Thanks.

- Greg


Can you post a SSCCE?

What VM, platform & architecture are you using? Is your buffer being
allocated within the VM's process space, or by some other memory allocator?

How much smaller is the VM capacity than native capacity? If it's 8 bytes
or less, you may have an alignment issue in your pointer arithmetic.

If you are using view buffers around your ByteBuffer, you could still be
doing something wierd with alignments.

Also, inside the VM, buffer capacities are limited to 2^31-1. I think JNI
allows you to go bigger, as you may be able to do fancy things like create
a ByteBuffer up to 2^33-1 bytes and promote it to an IntBuffer before
handing it back to the VM. Though, I have no idea how well VMs support
those kinds of promotions, so you should probably be staying under 2 GB.

HTH,

-Zig
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top