Jump to content
  • Advertisement
Sign in to follow this  
JasonWelch

creating projectiles on the fly vs on initialize

This topic is 2895 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am newer to Java and especially developing games with Java. I found that my game was crashing due to outofmemory error and after a bit of research, I found that the problem was in the fact that I was creating projectiles and objects on the fly within the game loop. For example: Vector2 pos = new Vector2(0,0); within the update method was causing pos to be created every frame and stack instead of overriding the previous.

Well, with my projectiles, I was creating 1 every .25 seconds (for faster fire rates) and storing them into an array list so they could be updated/drawn later...this was resulting in 4 * 30 * time, number of projectiles to be created. My solution for this was to create all projectiles during initialization of the object and then reuse them once they expired or 'died'. I know array lists only store references, so clearing the array list, or even setting each variable to NULL, only affects the reference pointer and not the actual object.

Does anyone else use this same method? How can do you typically manage objects that are created in loops in Java? I will want to use arrays for this type of thing but I can't figure out how to make sure the objects actually are getting deleted.

I'm rather lost. I have done a lot of research on using array lists dynamically, but I can't seem to find much as far as using them in loops in real time.

Share this post


Link to post
Share on other sites
Advertisement
First, make sure that you aren't inadvertently hanging on to a reference to the expired projectiles, causing the garbage collector to think you still need them, and not free them up. Managed languages generally have a memory profiler of some sort that you can run to make sure they're getting freed up.

Assuming that's not the case, then you're running into a limitation of the garbage collector. The purpose of the GC is to simulate a computer with infinite memory. But if you hit it too hard, it won't be able to keep up. Manually managing the memory allows you to get more performance, since you know more about the scenario and can optimize for it (the GC has to be general purpose).

In C#, we have structs, which are value types allocated on the stack, avoiding the GC. Types such as Vector2 are implemented like this. I don't think Java has an equivalent.

EDIT: I should add that you probably don't need to create all of the projectiles on initialization. Creating them in batches (maybe 1024 at a time) should be a good tradeoff.

Share this post


Link to post
Share on other sites
The way I handle projectiles is to create once and then reuse.

Once it goes out of sight just toss it out of the draw pool and back into the main pool to use again.

Share this post


Link to post
Share on other sites
you should only reuse objects when you are very limited by the garbage collector performance, as on Android for example.

Also when using a lot of allocations in a loop try to do the mon the heap not th stack.
The JVM allocats on the heap if it knows that a variable can not escape the current code block.

Also casue the bullets arn't owned by the shooter anymore you can store all bullets in on Collection.
In this case i would suggest, to use instead of an ArrayList an ArrayDeque which will be much fast for itteration and add and remove operations.

ps: as typedef struct mentioned, look at your code again and check if you arn't holding any references of the bullets, like in the draw queue

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!