This is about ByteBuffers, but I’m posting it there because it’s most important when working with lwjgl that makes extensive use of direct buffers.
This little test program shows the problem of direct buffer memory overhead.
import java.nio.*;
public class TestMem {
public static void main(String[] args) {
ByteBuffer[] bufs = new ByteBuffer[100000];
for (int i = 0; i < bufs.length; i++)
bufs[i] = ByteBuffer.allocateDirect(1);
try {
Thread.sleep(60000);
} catch (Exception e) {
e.printStackTrace();
}
bufs = null;
System.gc();
try {
Thread.sleep(10000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Running this program and watching memory use in e.g. ‘top’ on linux shows that the program sits on about 400mb of memory! This is against the theoretical 100k that I’m actually allocating. Changing this to indirect buffers solves the problem (still ~50-100 bytes overhead on each ByteBuffer though), but lwjgl don’t like indirect buffers.
Only possible solution as I see it is to allocate one big chunk and slice()'ing it up as needed, in effect doing your own memory management.
I already submitted a RFE to sun, but I doubt they will improve this, as they already state that direct buffers should only be used for large, long-lived buffers.
Just FYI.
- elias