"finalize" isn't guaranteed to be called.  If you want to know if an
object has been GCed, create a WeakReference to it and test that after
the GC.

But note that full GC isn't guaranteed to occur when you do "gc"
either.  And some systems require two GCs to trigger finalize (or
clear a WeakReference).

And nulling out the only visible reference doesn't necessarily "free"
an object either.  There still may be a hidden temporary pointer to
the object in the method invocation.

On Oct 18, 10:58 am, John Gaby <jg...@gabysoft.com> wrote:
> I appear to have a memory leak(s) in my application, and I am trying
> to get a handle on it by trying to understand more about garbage
> collection.  I have created the following application.
>
> Here I have a class 'MyClass', and I create an instance of that class
> in my 'onCreate'.  I then null out the pointer which should leave no
> references to that instance.
>
> In 'MyClass', I override the finalize method so that I can see when
> the object is destroyed.  When I run this program, the finalize method
> is never called, even if I call System.gc(), from within my program,
> or use the 'Cause GC' button using DDMS.  (This is the same behavior
> that I see in my full application, by the way).
>
> Can someone explain to me more about what is going on here.  I believe
> that in my main application, that I have objects that are not being
> freed.  Is there some strategy for identifying those objects?
>
> Thanks.
>
> /*
>  * Main Activity
>  */
>
> package com.gabysoft.memoryleak;
>
> import android.app.Activity;
> import android.os.Bundle;
>
> public class MemoryLeak extends Activity {
>     /** Called when the activity is first created. */
>     @Override
>     public void onCreate(Bundle savedInstanceState)
>     {
>         super.onCreate(savedInstanceState);
>
>         MyClass mc = new MyClass();
>         mc = null;
>
>         setContentView(R.layout.main);
>
>         System.gc();
>     }
>
> }
>
> /*
>  * My Class
>  */
>
> package com.gabysoft.memoryleak;
>
> import android.util.Log;
>
> public class MyClass extends Object
> {
>     MyClass()
>     {
>         Log.d("GabySoft", "MyClass::MyClass() " + this);
>     }
>
>     @Override
>     protected void finalize()
>     {
>         Log.d("Gabysoft", "MyClass::finalize() " + this);
>     }
>
> }

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to