Reputation: 1081
I learned, for I/O, Direct Byte Buffer allocates memory on native heap whereas ByteBuffer allocates it on java heap, because of which we need to copy data from java heap to native heap for I/O operation.
So my question is,
Is native Heap of Direct bytebuffer part of JVM's address space?
If not Why do OS need native heap to read data from socket? why cant it directly read data from scoket into JVM's address space? or the address space of requesting process.
My Goal: I am electronics engineer. Have started working on java recently for a project that requires minimum latency. I have various option, multithreading, jvm tuning. These questions are arose from my analyses that way i will consider those options.
Upvotes: 2
Views: 101
Reputation: 43125
Is native Heap of Direct bytebuffer part of JVM's address space?
Yes, it is.
It's just outside the address range which is used for java objects and managed by the GC.
because of which we need to copy data from java heap to native heap for I/O operation.
If you're operating on binary data you can just keep it in the DBB and iterate over its bytes with the accessor methods. That way only the bytes you access will temporarily copied. The advantage of a non-direct BB is that you can access its backing byte[]
array directly. But if you don't need that then a DBB can be faster under some circumstances.
Possible scenarios:
Use BB -> copy to native
Use DBB -> no copy needed
Use java objects -> serialize to BB -> copy to native
Use java objects -> serialize to DBB -> no extra copy needed
In those cases you will notice that using DBB reduces the copying.
Here's one where it doesn't:
Use BB -> pass underlying array to a method expecting byte[]
Use DBB -> copy to byte[] -> pass to method
In those cases the method needs to be rewritten to accept bytebuffers.
Have started working on java recently for a project that requires minimum latency. I have various option, multithreading, jvm tuning. These questions are arose from my analyses that way i will consider those options.
You really should not worry about such low-level details. Measure before you optimize. Switching from BBs to DBBs is trivial when you need it later.
Upvotes: 0