Deallocating direct buffers in Applets?

Hi there.

I am using BufferUtil.newFloatBuffer and BufferUtil.newIntBuffer within an Applet. The Applet is written to handle ‘init’, ‘start’, ‘stop’ and ‘destroy’ methods appropriately. For example, in ‘destroy’, the application cleanly winds down and destroys the context and even nullifies some objects.

Unfortunately, the direct buffers do not appear to get deallocated after ‘destroy’ and there doesn’t appear to be a way to re-use this memory.

The upshot of this is that visiting the application more than once eventually causes an ‘out of direct memory’ error which means the browser has to be completely restarted.

What is the recommended approach please?

Problem is on Mac OS X 10.5.2, JOGL 1.1.0 (Safari 3.1.1 and Firefox 3.0b5).

Thanks in advance!

Everything referenced from static fields will not be cleared upon stop()/destroy(). Make sure you either do not use static fields or nullify those references.

If that doesn't help, you can only resort to sun.misc.Unsafe, but you really have to know what you are doing there. Just malloc-ing your own block of bytes and changing the pointer of a java.nio.Buffer will very likely crash the VM upon the next Full GC, so you have to prevent that Buffer to be GCed... ever. Later you can Unsafe.free(pntr) to release that memory again. (Still keeping that Buffer around!)

That’s very helpful - thank you very much!