Floatbuffer memory leak

So i’ve been trying to fix memory leaks within my code and it seems the cause of the entire issue is BufferUtils.createFloatBuffer(); when this is called it creates a float buffer and appears to leave it in memory , my test of just having a for loop call that ended up hogging memory. Is there a method of deleting the floatbuffer once it’s purpose has been served as it’s at the core of my code.

Try System.gc(); I suppose.

One of the ways is to dereference it. To do this, you have to make sure that no variables are pointing to it.

System.gc() won’t do anything as the underlying memory is held off heap. Have a read of this (has good explanation of what going on)

and this

[quote=“lcass,post:1,topic:57523”]
The LWJGL recommendation for performance sensitive code is explicit allocation management (memAlloc/memFree methods in org.lwjgl.system.MemoryUtil) and stack allocations (org.lwjgl.system.MemoryStack). See #4 and #5 in this post for details.

Even if you explicitly invoke the Cleaner of buffers allocated via ByteBuffer.allocateDirect, performance is going to suffer in tight loops (allocateDirect itself is slow, objects always escape so more GC pressure). See this benchmark.

Stacks seem perfect for this role , it’s strange that these aren’t really mentioned in any of the default LWJGL tutorials as it’s really useful. Thanks for the links i’ll implement them soon.

I striped down the buffer utility class from JM3 and so far it worked pretty well, you can take a look here

I think the method i’m going to go for instead is to still use floatbuffers however entirely leave them to the few VBO’s I have and instead modify actual vertex data and pass it down to the VBOs. This basically means recoding the graphics engine but it isn’t that bad , I couldn’t really find any tutorials on linking stacks with lwjgl and if anyone has any please let me know.

[quote=“lcass,post:7,topic:57523”]
Not sure what you’re looking for exactly. It’s very simple:

// Method 1: Explicit malloc/free, useful for "big" buffers and custom lifecycles 
FloatBuffer buffer = memAllocFloat(100);
// ..use buffer..
memFree(buffer); // buffer goes away (doesn't have to be in the same method)

// Method 2: Stack allocation, useful for "small" buffers, local-only
try ( MemoryStack stack = stackPush() ) {
	FloatBuffer buffer = stack.mallocFloat(100);
	// ..use buffer..
} // buffer goes away

[quote=“Spasi,post:8,topic:57523”]

Ahh thanks , so just entirely ignore BufferUtils and do it manually. I’ll work on my method first and then implement this with it.

Tries to fix memory leak , ends up rewriting entire graphics engine ;D