Possible cause for performance issue.

Hey everyone, I have a 2D tile based game where a character can shoot a couple different weapons at stationary enemies. Pretty much everything is stored in ArrayLists, bullets, enemies, collidable tiles, etc…but for some reason when i run my character around holding down the fire button for the machine gun, after a while the game begins to lag, and I cant quite figure out why. The bullets are removed after they hit the wall so im just lost as to what could be the problem.

Sorry, I do realize that I am giving very little information, but if you all could just post some common causes of this or what you think it could be then i could check it out, because otherwise it would be a large amount of code to paste here…

Any comments are appreciated.

Print the sizes of your lists and check if the values make sense.

Hmm yea, I tried that, everything seemed to be in perfect order, the bullets list showed no bullets when there were none, etc ,etc…however I have noticed that the performance only goes down after a while, it seems to happen around 2-3 minutes into the game (rough guess)

Use a profiler. For example https://visualvm.dev.java.net/

do you load the image again every time you create a bullet? maybe you could use a cache.

maybe you could use a cache.

Nah. Just keep a reference to that image… and that’s it.

You may try to load them directly from file into an arraylist, using a cached file memory (ImageIO.read()). That’s if your sprites aren’t very big much faster and frees (not “freezes” !) the ram. You may also feel ImageIO slower than JAI. Well, it can rather cool down the memory by freeing the pic every time it’s no more needed (e.g. the character puts its weapon off).

use a cache.


protected Map<String/Integer, Image> cache = new HashMap<String/Integer, Image>();

Use either a String/Integer or any object you like as the key, and an Image/BufferedImage for the image.

When you want to get it:


public Image/BufferedImage getImage(String/Integer key) {
 if (cache.containsKey(key)) {
  return cache.get(key);
 }
 Image img = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource(key));
 cache.put(key, img);
 return img;

Try something similar to the above.

Do you do anything fancy except firing bullets when holding down the fire button? If you’re using a KeyListener, you’re going to be firing a whole lot of key events. And you’re sure “dead” bullets aren’t referenced somewhere, in your render or logic code? The behaviour you describe fits perfectly with something like that.

I’m assuming you only load resources once.

Sorry guys for not replying for a while, but thanks for the replies.

I am pretty sure I am only loading the image once, and then just referencing it throughout the project.

And yes I am using a KeyListener and a switch statement, and if W, or w.e is pushed I set that bool value to true and in my run method I do the logic for character movement or w.e is required. I was thinking it had something to do with the bullets also because it would only happen after a while of shooting the machine gun, but i cant find anywhere that the bullets are still there, especially when i printed out the size, it was at 0 when it was suppose to be, etc…

[s]Ok well I cant seem to find anything that is out of ordinary, but what is weird is, I have created a new game, this time a sidescroller but it uses the same tile engine as the past game and is getting that random stuttering…So it seems like it would be the tile engine but I do not see anything that would cause it to lose performance over time…

But the weirdest thing is, that when the stuttering is happening, I dont seem to actually lose performance, it just stutters…For example, my FPS counter never goes down.

(My FPS counter is very basic, adds to the frame variable everytime it loops and if the elapsed time equals 1 second, it records the number of FPS as the fps…)

any ideas?[/s]

Seems my FPS counter is flawed, so ima try and fix that… :-X ???