Stutter in Android Game (Garbage Collection?)
<p>To optimize my game I made it grid based. Objects lye in a grid square. Thus to do rendering I gather all objects in the regions visible to the camera and put them in a list by layer:</p>

<pre><code>public ArrayList&lt;Entity&gt; queryRect(OBB2D rect)
{
    neighbourQueryObjs.clear();
    ArrayList&lt;Region&gt; reg = determineNeighbourRegions(rect);
    for(int i = 0; i &lt; Entity.MAX_LAYERS; ++i)
    {
        for(Region r : reg)
        {
            for(Entity e : r.getStatic(i))
            {
                if(!neighbourQueryObjs.contains(e) &amp;&amp; e.getRect().overlaps(rect))
                {
                    neighbourQueryObjs.add(e);
                }
            }

            for(Entity e : r.getDynamic(i))
            {
                if(!neighbourQueryObjs.contains(e) &amp;&amp; e.getRect().overlaps(rect))
                {
                    neighbourQueryObjs.add(e);
                }
            }
        }
    }

    return neighbourQueryObjs;

}
</code></pre>

<p>This works great. The game is very fast. I call this evey frame
The problem is that every 1 or 2 seconds or so, the game just freezes for about 2-300 ms.</p>

<p>I suspect it is garbage collection. Could that be it?</p>

<p>Is there a way I could preallocate this stuff so it never has to use GC? I dont mind wasting memory, memory is no problem.</p>

<p>Here is my game loop, could that be it:</p>

<pre><code>public void run() {
        Canvas canvas;

        long beginTime;     // the time when the cycle begun
        long timeDiff;      // the time it took for the cycle to execute
        int sleepTime;      // ms to sleep (&lt;0 if we're behind)
        int framesSkipped;  // number of frames being skipped 

        sleepTime = 0;

        while (running) 
        {
            canvas = null;
            // try locking the canvas for exclusive pixel editing
            // in the surface
            try 
            {

                // we have to make sure that the surface has been created
                // if not we wait until it gets created
                if (!holder.getSurface ().isValid())
                    continue;
                canvas = this.holder.lockCanvas();
            synchronized (holder) 
            {
                    beginTime = System.currentTimeMillis();
                    framesSkipped = 0;  // resetting the frames skipped
                    // update game state
                    update();
                    // render state to the screen
                    // draws the canvas on the panel
                    display(canvas);
                    // calculate how long did the cycle take
                    timeDiff = System.currentTimeMillis() - beginTime;
                    // calculate sleep time
                    sleepTime = (int)(FRAME_PERIOD - timeDiff);

                    if (sleepTime &gt; 0) 
                    {
                        // if sleepTime &gt; 0 we're OK
                        try 
                        {
                            // send the thread to sleep for a short period
                            // very useful for battery saving
                            Thread.sleep(sleepTime);
                        } catch (InterruptedException e) {}
                    }

                    while (sleepTime &lt; 0 &amp;&amp; framesSkipped &lt; MAX_FRAME_SKIPS) 
                    {
                        // we need to catch up
                        // update without rendering
                        update();
                        // add frame period to check if in next frame
                        sleepTime += FRAME_PERIOD;
                        framesSkipped++;
                    }
                }
            } 
            finally 
            {
                // in case of an exception the surface is not left in
                // an inconsistent state
                if (canvas != null) 
                {
                    holder.unlockCanvasAndPost(canvas);
                }
            }   // end finally
        }
    }
    protected void onDrawTransformed(GraphicsContext g)
    {   
        OBB2D view = g.getCamera().getCamRect(getWidth(), getHeight());

        ArrayList&lt;Entity&gt; ents = world.queryRect(view);
        for(Entity e : ents)
        {
            e.draw(g);
        }
        //city.draw(g);
        //vehicle.draw(g);
    }
</code></pre>

<p>What could I do to stop the stutter?</p>

<p>Thanks</p>
