How to make it more efficient?

I create a clone of asteroids, but a few more bells and whistles.

I currently have an ArrayList<Asteroid> that contains all the asteroids on the screen. Each of them has a Vector associated with it, and extends my genereic GameObject class, which handles drawing and updating and other common things that have a common game object.

Every time I destroy an asteroid, I create a new Asteroid object and add it to ArrayList<Asteroid> ... In this case, there is a noticeable lag, since I also create explosion particles, and I believe that this is GC.

My idea was that instead of creating new objects on the fly, I could pre-create a pool and just reuse them.

Is this the right idea? And what is the most organized and effective way to do this?

Any other ideas would be great. Just trying to reduce the creation of all these objects, because it definitely causes a noticeable lag. Thanks!

+6
source share
4 answers

Creating a pool of objects and reusing them is a good idea. I also think that you can switch from ArrayList to Vector , because vectors are optimized for random indexing, that you will do a lot using the pool.

Since you say that every time you destroy an asteroid, you add a new one, it seems that you are working with a constant number of asteroids. This way you can create a pool with a constant number of members.

+5
source

(1) View the design of your objects with the Flyweight template. This is a pattern commonly used for objects with repeating characteristics. A sample Java code is available here: http://en.wikipedia.org/wiki/Flyweight_pattern

(2) If you already know how many objects you will use, then consideration includes creating your object and another initialization process on the download page.

+5
source

Java is usually very good for hosting new objects as well as for creating GC objects that were recently created, so I would not immediately assume that combining will improve the situation. Are you sure you are not creating any other β€œgarbage” that can cause a full GC (such a GC that pauses the program for extended periods of time)?

You can make sure that it is indeed a GC that causes the problem you are observing by turning on the β€œverbose GC log” (Google has several command line arguments for the JVM to do this, which activate it with different levels of detail) ...

+4
source

I believe that your particle effects are the culprits of slowing down rather than creating an object.

Game developers, as a rule, try to make their graphics fast, but at the same time many performance compromises were performed in scripts. This is very important: the performance of creating and storing game objects in most cases is negligible compared to the hit from computing their physics and drawing their graphics.

Try to reduce the number and graphic complexity (especially if they have transparency , the effects of a very fast stack to insanity) of your particles.

+4
source

Source: https://habr.com/ru/post/910804/


All Articles