Question

I'm currently working on a game engine in java, however I'm having preformance issues when allocating large sets of objects on the heap, for example

public class GLParticleSystem { 

  private GLParticle[] particles = new GLParticle[2000];
  private int numberOfParticles;

  public GLParticleSystem(numberOfParticles) {
     this.numberOfParticles = numberOfParticles;
  }

  public void init() {
    for (int i = 0; i < numberOfParticles; i++) {
        particles[i] = new GLParticle();
    }
  }

}

The above piece of code will suffer a massive frame drop upon initiation, due to shear level of allocation, I was wondering if there is something I am missing or some text on solving this issue.

Update

Requested data members of my GLParticle class.

public class GLParticle {

    private GLSpriteSheet image = null;
    private float x;
    private float y;
    private float vX;
    private float vY;
    private float alpha;
    private float alphaStep;
    private boolean isDead;
    private long startTime;
    private long lifeTime;
    private final float u = 480f;
    private final float v = 504f;

}

Thanks Gary

Was it helpful?

Solution

You can create ten separate arrays of 2000 for each member of GLParticle, and use Flyweight Pattern. In this implementation, each GLParticle would be passed its index into the GLParticleSystem's arrays, along with an instance of GLParticleSystem that contains the arrays. This is slightly more cumbersome, but this is the first pattern to try when having lots of fine-grained objects becomes too expensive in terms of the CPU cycles.

OTHER TIPS

You are making new objects in your init, you might find Object Pooling to be helpful. Instead of creating objects every time you want one you make a large set to begin with. Then whenever you need an object you take a preallocated object.

http://en.wikipedia.org/wiki/Object_pool_pattern

private static final float U = 480f; and V would help a tiny bit.

Look at FutureTask for doing such things.

While I am somewhat surprised that you have performance issues with allocating only 2000 objects, I am sorry to say that there is no simple fix for your problems. Java, as it lacks C/C# struct types, is not a very efficient language in this usecase when compared to those languages. There are 3 solutions for your problem.

1) Preallocate objects before you need them. While it works this is not a great solution as you have to manually return unused objects to a pool of preallocated objects.

2) Given that GLParticle is small object (56bytes) it can be rewritten, if your usage allows it, as GLParticles class which stores data for a fixed amout of GLParticle objects.

class GLParticles {
   private static final float u = 480f, v = 504f;

   private GLSpriteSheet[] images;
   private float[] x, y, vX, vY, alpha, alphaStep;
   private boolean[] isDead;
   private long[] startTime, lifeTime;

   GLParticles( int size ) {
      // allocate arrays here...
   }
}

This is both more space efficient and far faster to allocate (and collect).

3) Try to optimize size of nursery and thread local GC pools if JVM which are you using has those.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top