Saturday, May 28, 2011

The lazy coder vs Garbage Collection

Automated Garbage Collection has made me lazy.

I'm building a libgdx android gamelet for the past few days and I've intentionally left a few dynamically created primitives in my game loop to hammer out the logic first.

Game performance wasn't enough to irritate me until I started adding Strings for status display updates. Boom, jerky gameplay every 20-30 seconds! LogCat was showing GC triggering every 20 odd seconds for as much as 300 milliseconds. Close to 8k objects were being recycled. Ouch.

Went through the code and weeded out for(int i=.......) codes. Removed variable creations in critical functions. Replaced Strings with StringBuffers(I finally realized libgdx BitmapFont.draw uses CharSequence interface instead of String object.. d'oh.

Game is looking good now. No more GC triggering for the past few minutes.