tags:

views:

81

answers:

3

In the Lunar Lander example (Provided by Google's Android site), there is a main loop in a method called "run()". Here it is below:

        @Override
        public void run() {
            while (mRun) {
                Canvas c = null;
                try {
                    c = mSurfaceHolder.lockCanvas(null);
                    synchronized (mSurfaceHolder) {
                        if (mMode == STATE_RUNNING) updatePhysics();
                        doDraw(c);
                    }
                } finally {
                    // do this in a finally so that if an exception is thrown
                    // during the above, we don't leave the Surface in an
                    // inconsistent state
                    if (c != null) {
                        mSurfaceHolder.unlockCanvasAndPost(c);
                    }
                }
            }
        }

So now, my question is this: Isn't it bad to keep nulling c every loop and thereby causing more garbage collection? Maybe I don't understand this well enough, but why must it be nulled?

+1  A: 

It's not bad at all, it ensures that the Canvas object has a 0 or null reference when it is recreated. It is good programming practice.

Also notice the finally statement ensuring that if a valid Canvas object exists that it is cleaned up. If it's null then resetting it to null does not have any drawbacks. There is no penalty or additional clean up needed. Remember an object has an address, and a reference to null ensures its not pointing to some valid object that may be lurking around (hasn't been cleaned up).

Besides the garbage collector is not called when you initialize a variable to null. It simply means that the object does not refer to anything that may be currently in memory.

JonH
Doesn't the recreation of objects in a loop cause the garbage collection though?
Eugene
@Eugene - if you are instantiating and allocating memory to an object then yes. You will force a periodic garbage collection to release that memory. The android site calls these 'hiccups' in your application. But assigning a null to an object is not the same thing.
JonH
Okay, I think I understand from everyone's answers. Thank you for your help!
Eugene
What is a 0 reference (in the context of Java)?
Steve Kuo
Steve sorry 0 was not a good example, my point was to try to explain null as if it was a type like an integer. If that could was using an integer to run a total, you could reset the total back to 0.
JonH
A: 

Assigning c to null initially won't cause the garbage collector to run. C didn't point to an object, hence there's nothing to collect. All you're doing is ensuring that it points to nothing. Additionally, there is likely other references to the canvas object that c points to meaning that just because c no longer points to it, doesn't necessarily mean it's ready to be collected.

Chris Thompson
+1  A: 

Hi,

If you're referring to the line Canvas c = null; - it's not related to GC at all. It is initialization of the local Canvas reference c so the code could compile.

ognian