I'm currently developing my first game in Java language.

The game is an endless runner with obstacles to avoid and enemies to kill. Unfortunatelly im still not experience enough and many times im just improvising. I have no idea if my method of loading of obstacles is good or bad in terms of performance (how fast a game runs, how much fps).

Currently my code is designed in a way that I have a level class, and in level constructor I create all obstacles that will be used in that level. The obstacles are shown in the game window randomly. When an obstacle is needed, the state of obstacle is reset and the obstacle is rendered. When obstacle go offscreen rendering and update funtions are stopped. The obstacle object is still on the heap memory ready for reuse. So there is no creating objects on the fly and then removing the references.

I have no idea if its good for how fast the game is running or not good or irrelevant? How about garbage collector is it good practise or not? I code it that way beacuse it was simplest for me because the obstacle objects are actually pretty complicated and it would be more difficult for me to code it the way of creating them on the fly randomly. And there is probably 40 or 50 objects allways ready for use? The objects are referenced through ArrayList of ArrayLists.

So is this acceptable design or complete garbage? What is your thoughts?

closed as unclear what you're asking by Scant Roger, Neil, psr, user22815, Bart van Ingen Schenau Jan 1 '16 at 16:26

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 5
    You should try different implementations and profile them. Profiling will not only show you which implementation is the fastest, you will also see which part of your code consumes the most time. – MrSmith42 Dec 21 '15 at 21:31
  • 3
    This sounds like a fairly ordinary practice to me, whether it's acceptable or not is something you'll have to ask your profiling tools. – Ixrec Dec 21 '15 at 21:35
  • As usual, we're talking about performance vs memory usage. You can usually optimize performance but at the cost of higher memory usage. Unless we're talking about very expensive objects (with nothing shareable between these instances), you'll almost never have to worry about consuming too much memory. Examples of expensive objects would be images or large data structures. – Neil Dec 22 '15 at 8:13

You should implement both options, and profile your game

(More information about profiling: JVMMonitor)

The basic idea is:

-Create objects at runtime: Better use of memory (Might not be less memory used), but slower code (Less FPS).

-Preload Objects: Faster code (More FPS), but worse use of memory (Might not be more memory used, but you use that memory for more time).

IMHO, usually the best idea is to preload the objects and images. Still, you should test your case and decide what is best for your project.


Object reuse is the core feature of an object pool.

If seeded while loading the level they can allow for expensive object initialsation without intoducing any performance spikes during gameplay.

Where the pool size is tuned to suit gameplay and platform requirements they usually improve realtime performance.

But remember to profile, it is important to test any asumptions.

Not the answer you're looking for? Browse other questions tagged or ask your own question.