NewDirectByteBuffer and memory management
843829Feb 2 2009 — edited Feb 4 2009Hi,
Here is the scenario:
- In a callback, a Java program asks JNI to create a direct byte buffer using NewDirectByteBuffer. This byte buffer is backed by a native array allocated by a native library.
- The callback uses the returned byte buffer to read (not write).
- The callback makes a native call to destroy the native array that backed the byte buffer.
- This callback processing loop repeats...
What I see:
VM memory utilization rapidly grows to 1 GB and ultimately reaches the maximum limit of 2+ GB. (running on Linux with a 32-bit VM)
This happens within seconds, 10-15 seconds.
Each allocated byte buffer is fairly small, 10 KB at most.
100 callbacks are processed per second.
The memory is not used by the heap.
Garbage Collection is happening just fine, regularly and quickly.
I was desperate to try anything, so I tried various parameters...and:
- Xmx128m
Made a dramatic improvement, since the program can then handle 1000 callbacks per second without the VM ever consuming more than 400 MB of memory.
I went on and added this parameter:
- Xmn128m
And that made it behave as badly as before. Growing to 1+ GB within seconds.
I assume this is a defect of some sort. Has anyone experienced this? Is there a way to work around it?