I am creating an Asteroids clone, but with a few more bells and whistles.
As of right now I have an ArrayList<Asteroid>
that holds all of the asteroids on screen. Each one has a Vector
associated with it and extends my genereic GameObject
class which handles the drawing and updating and other common things that each game object has in common.
That being said each time I destroy an asteroid I create a new Asteroid
object and add it to the ArrayList<Asteroid>
... There is a noticeable lag when this happens as I also create explosion particles and I assume this is the GC.
My idea was to instead of create new objects on the fly, that I can pre-create a pool of them and just re-use them.
Is this the right idea? Also what is the most organized and efficient way to go about that?
Any other ideas would be great as well. Just trying to reduce creating of all of these objects because it is definitely causing a noticeable lag. Thanks!
Creating a pool of objects and reusing them is a good idea. Also I think you could switch from ArrayList
to Vector
, because Vectors are optimized for random indexation, which you'll do a lot when using a pool.
Since you say that each time you destroy an asteroid, you add a new one, it seems that you work with a constant number of asteroids. So you could create pool with a constant number of members.
(1) Consider design your objects with a Flyweight pattern. It is a pattern commonly used for objects with repeating characteristics. A java code sample is available here: http://en.wikipedia.org/wiki/Flyweight_pattern
(2) If you already know how many objects you will be using, then consider includes your object creation and some other initialization process into a loading page.
Usually Java is very good at both allocating new objects and performing GC of objects that was recently created so I would not immediately assume that pooling will improve things a whole lot. Are you sure that you are not creating any other "garbage" that may cause full GC (the kind of GC that pause the program for extended periods of time)?
You can verify that it really is GC that is causing the problem you observe by enabling "verbose GC logging" (Google for this there are several command line arguments to the JVM that enable it with different level of detail)...
I reckon your particle effects are the culprit of the slowdown, not the object creation.
Game developers typically go to great lengths to make sure their graphics are fast, but make many performance compromises in scripting. This is for very good reason: The performance hit in creation and storage of game objects is in most cases insignificant in comparison with the hit from calculating their physics and drawing their graphics.
Try reducing the number and graphical complexity (particularly if they have transparency; the effects of that very quickly stack up to insane) of your particles.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With