I was souping up the nio entry in the Java glossary.
I wondered if people had rules of thumb about when direct buffers or
memory mapping might give better performance.
I would have though memory mapping would only apply to random access,
but Sun hints otherwise.
I would guess direct buffers are for when you do a lot of skipping?

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
> I wondered if people had rules of thumb about when direct buffers or
> memory mapping might give better performance.
My "rule of thumb" would be: "If in doubt, don't do it." Whenever i used
memory mapping, it proved to be no large gain in performance, sometimes on
the contrary, actually.

Signature
You can't run away forever,
But there's nothing wrong with getting a good head start.
--- Jim Steinman, "Rock and Roll Dreams Come Through"
> I was souping up the nio entry in the Java glossary.
>
[quoted text clipped - 3 lines]
> I would have though memory mapping would only apply to random access,
> but Sun hints otherwise.
It reduces memory when multiple processes use the same file. As I
understand it, Solaris/Linux versions of Sun's JRE used to memory map
jars. In the latest builds of Mustang that is no longer true, reducing
memory consumption and improving performance. Because you cannot cache
parts of a page independently, you may well (perhaps probably) end up
throwing away huge quantities of physical memory, which is a bad thing.
> I would guess direct buffers are for when you do a lot of skipping?
Direct allocated buffers are dangerous things. There is no way unmap
them. You are at the mercy of the garbage collector, which doesn't take
into consideration the direct buffer memory. However, bulk operating
systems operations on them should be faster.
So only use them if *both* it is performance critical and you will not
be reallocating buffers.
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Roedy Green - 21 Oct 2005 22:55 GMT
>Direct allocated buffers are dangerous things. There is no way unmap
>them. You are at the mercy of the garbage collector, which doesn't take
>into consideration the direct buffer memory. However, bulk operating
>systems operations on them should be faster.
thanks Thomas. I have added your wisdom to the entry at
http://mindprod.com/jgloss/nio.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
Thomas Hawtin - 23 Oct 2005 01:46 GMT
There is something else worth noting about NIO buffers that isn't often
mentioned. Read and writing individual bytes goes through a lot of
indirection, even though what you are trying to achieve is very simple.
For Sun's HotSpot you need to make sure that the method with your inner
loop is very small. If it isn't then the access code wont be inlined and
it will run very slowly.
Cameron Purdy did some IOless NIO benchmarks:
http://www.jroller.com/page/cpurdy/20040406
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/