How to deal with temporalily needed vectors/rotiations?

in my engine, i need lots of vectors to store some values during calculations.

i could:

a) create lots of local vector3fs -> bad because of gc

b) use private final Vector3f tmpX =new Vector3f()-members. -> no gc problem, but will increase memory usage because the variables take up space no matter if they are really needed



how do you solve  it? i think i'm going to use javolution and its object pool.

Before using pools check that those objects really are a performance problem. The gc can handle little objects that are only temporarily used.

we are talking about 10-100 vectors (depending on complexity of what it's doing) per entity in the game and per frame. i heard rumors about java7 being able to understand local objects and that it's able to eliminate the object allocation if the object does not leave the scope (aka is returned). is this true?

(not that it matters, i return the vectors)



currently i use method b), and i'm running into memory problems (30k entities -> 300mb)

Are these vectors really all needed simultaneously?  Perhaps you can make them static.

and only create them when they are needed(don't init them where they are defined)



and do you need them all, or are some ony temporary storage for calcs?

making them static could work, as long as i don't make recursive calls… which i think i don't do.


and only create them when they are needed(don't init them where they are defined)



if would lead to rather ugly if(vector==null) {vector = new ...}--blocks everywhere. and the condiiton would only be true once.

Would it have the same effect as Javoluion if you recycle yourself vector objects ?



For example, you create vectors thanks to a static method Vector3f.getNewVector3f(x,y,z) and you make the constructor private.

That method returns an unused vector from a list. If the list is empty, it returns a new vector by calling the constructor.

When you want to cast a vector off, you call its recycle method so it is added to the list.

That's a bit trivial and maybe you already discarded that solution

Would it have the same effect as Javoluion if you recycle yourself vector objects ?

yes.

That's a bit trivial and maybe you already discarded that solution

i considered an even simpler one:
before every tick, consider every vector recycled. it's a bit faster.
however, making allmost all temp vectors static did work, and contains even less overhead.