For a while now, I've been seriously bothered by random CPU spikes in some levels of my game. They don't appear to have a pattern to them...without warning, my FPS will shoot down from 50 to about 10 or 20 for a few seconds, then jump back up. It's really annoying.
After testing a lot of possibilities for what could be causing it, I've come to the conclusion that it has to be the garbage collector. The objects that are causing the strain are little red blips that I use to simulate a laser beam that bounces off of mirrors and things. Needless to say, it takes hundreds of these little objects every second to create the appearance of one contiguous beam. To save CPU, I set the lasers to only fire the hundreds of little beams if the main character is relatively close to them, but even then, there are hundreds of objects being created and destroyed every second. As a result, I theorize, my computer has to keep running the garbage collector, which badly hurts the framerate.
Does anyone have any suggestions on how to make my program use memory more efficiently?