LWJGL
May 24, 2013, 16:41:07 *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: LWJGL 2.9.0 released!
 
   Home   Help Search Login Register  



Pages: [1]
  Print  
Author Topic: BufferUtils.createFloatBuffer and memory problems  (Read 910 times)
hapsky
Newbie
*
Posts: 2


« on: May 10, 2012, 08:58:33 »

Hello,

I ran into this problem: suppose I have a method that gets called a few times every frame at about 100fps. Inside this method I create a new Vector3 object. The Vector3 class is my own personal implementation, and contains a private FloatBuffer.

Code:
public class Vector3
{
    ...
    private FloatBuffer mBuffer = BufferUtils.createFloatBuffer( 3 );
    ...
}

I use that float buffer to store the x y and z coordinates and to quickly return them if I need to use the vector with the opengl api.

The problem is, the garbage collector is having a hard time cleaning up my Vector3 objects, so with only that initial method creating one Vector3 object, my memory consumption skyrockets to about 500mb ( it normally only uses 40mb ). Reading about ByteBuffers on the javadoc page, I see they are treated specially from the memory point of view. They also recommend "It is therefore recommended that direct buffers be allocated primarily for large, long-lived buffers".

Is there a nice way to convert from vectors ( and matrices ) to FloatBuffers ?

Thanks.
Logged
Fool Running
Nerdus Imperius
*****
Posts: 742


« Reply #1 on: May 11, 2012, 05:48:54 »

I think most people (me included) have their Vector3 class just store 3 floats. When you need to pass it to OpenGL you then use a FloatBuffer that you have waiting; one that is created to a certain size (mine is 16 floats) that you can just stick random stuff in it any time you need. You don't ever throw away the FloatBuffer and just keep using it for whatever you need it for (making sure to flip() after putting stuff in it Wink).
Logged

Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option Grin
hapsky
Newbie
*
Posts: 2


« Reply #2 on: May 11, 2012, 11:09:43 »

That's a pretty good idea and I should have thought of it. Makes me feel silly. Thank you Smiley
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines
SMFAds for Free Forums
Valid XHTML 1.0! Valid CSS!