Object reuse

Currently i .removeFromParent when i no longer need an object.  Which can occur quite often as i create and destroy missles all the time in my space game.  But i'm wondering if it wouldn't be better to store them in a resource pool and enable/disable them via CULL_ALWAYS/CULL_NEVER when i need them again.



thoughts welcome :wink:

I would say definitely  :slight_smile:

I use this for my ObjectPooling:



http://captiveimagination.com/svn/public/jcommon/trunk/src/org/jcommon/pool/ObjectPool.java

as long as your object creations do not trigger a major garbage collection, there should be no problem. i dynamically create lots of particle effects and dungeon parts and discard them later without any noticable effect on the performance. the garbage collections are done within <=0.02 seconds several times a minute, and i don't notice them during play.



i suggest using a profiler and measure how many garbage is actually created. a few mb every few seconds are almost nothing for current cpus, and i doubt you will reach these values. pooling will reduce the garbage times, but might cause some reeeeally weird and probably hard to trace bugs if the pool gets corrupted, plus its more complex.


HamsterofDeath said:

as long as your object creations do not trigger a major garbage collection, there should be no problem. i dynamically create lots of particle effects and dungeon parts and discard them later without any noticable effect on the performance. the garbage collections are done within <=0.02 seconds several times a minute, and i don't notice them during play.


The reason for my question is because i saw hicups when i implemented particles for my missles. Every time a missle was created a saw a hesitation. I'm not so worried about the garbage collection but more about the creation of particles and such. I was able to get rid of the hesitations by reducing the number of particles for every missle to about 20. I can understand that 300 from the example code was abit over the top. But it was enough to wonder about object reuse ;)

i think for particle effects it is a good idea to create a pool, they need quite a few cpu cycles when being created.

Something like this seems a common way to handle a particle effect pool, the pool increases when a lot of effects are shown at the same time.



            // search for an inactive effect and return it
            // create a new effect, if all are already active
        public ParticleMesh getExplosion() {
           for (int x = 0, tSize = explosions.size(); x < tSize; x++) {
                ParticleMesh e = explosions.get(x);
                if (!e.isActive()) {
                    return e;
                }
            }
            return createExplosion();
        }

             // create an effect and add it to the pool
             private ParticleMesh createExplosion() {
            ParticleMesh pMesh = ParticleFactory.buildParticles("explosion", 80);
                .....
            explosions.add(pMesh);
            return pMesh;
        }


I'd suggest checking your particle mesh warmup times on those effects.

I also have been having some slowdown on particle creation.  I noticed the bigger the warmup time, the more slowdown on creation.  Is there a best practice for warmup times?  How much is too much?  Or will switching to a pooled method make it a moot point?  I haven't tried a pool yet, thats my next step.

hmm, tried a Pool for my particles but when reusing a particle object i see it light up where it was previously used for a fraction of a second.


try calling updateGeometricState(0) (or maybe only updateWorldVector) after attaching the particle effect to the new object.


EDIT: nm, ParticleMesh.isActive was always returning true.