Quick Cross Post

I think this forum is better suited for this question than the Java World Wind forum:

http://forum.worldwindcentral.com/showthread.php?t=18725

I think the question basically boils down to: “If I want to have JWW use textures in resident video card memory, should I not use the JOGL Texture object?”

The JWW post covers the rest of my confusion…

I’m very interested in this too and on a more general note, when are buffers allocated by BufferUtil released please?

I haven’t done much thinking on this, but as far as I’ve heard if you’re using direct buffers, the garbage collector is unaware of those resources. I’m not an expert and can’t even remember where I’ve heard that, so any clarification would be nice.

It is actually slightly more complex than that.

The direct-memory is freed when no more direct ByteBuffers refer to it. (Like normal GC behaviour)

The problem however is that the DirectByteBuffer objects are rather small (like a few bytes), so the heap won’t fill much, and the GC won’t get triggered as soon as you might like. Releasing all your DirectByteBuffers that might be refering to massive amounts of memory, will only shrink the heap-usage by a few hundred bytes.

Further, there is this bug (?) in the Sun impl. of sun.misc.Bits, where the code is rougly:


static long total_allocated = 0;
static long max_allocated = 64 * 1024 * 1024;

public static long malloc(int bytes)
{
   if(total_allocated + bytes > max_allocated)
      System.gc(); // PROBLEM
   if(total_allocated + bytes > max_allocated)
      throw new OutOfMemoryExzception();

   total_allocated += bytes;
   return Unsafe.malloc(bytes);
}

public static void free(long base, int bytes)
{
   total_allocated -= bytes;
   Unsafe.free(base);
}

The problem is that the DirectByteBuffers are tracked by Soft/WeakReferences, and they get enqueued after a gc() so the 2nd gc() will clean them up - and free the memory.

No such luck when you’re only calling System.gc() once (which is btw a hint to the VM…).