After some more testing, it doesn't look like it has anything to do with
creating a new OnClickListener each time.  The following snippet (using the
same Layouts), produces the same leak.

Its significantly shorter, and when looking at a heap dump, from what I can
tell, an array called mActions in ViewRoot is filling with Runnables
(presumably these callbacks), but I'm no expert (and have very little
experience in heap dump analysis).

public class SetContentTest extends Activity implements OnClickListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setMode();
    }

    @Override
    public void onClick(View v) {
        setMode();
    }

    public void setMode() {
        setContentView(R.layout.main);
        ((Button) findViewById(R.id.btn_main)).setOnClickListener(this);
    }
}

Anyone have any ideas?

- Dan

On Sat, Aug 15, 2009 at 11:56 PM, Balwinder Kaur (T-Mobile USA) <
balwinder.k...@t-mobile.com> wrote:

>
> I'm curious too, but seriously you should go with the other options
> you mentioned. Creating new onClickListeners with each click is NOT a
> good idea !
>
> Balwinder Kaur
> Open Source Development Center
> ·T· · ·Mobile· stick together
>
> The views, opinions and statements in this email are those of the
> author solely in their individual capacity, and do not necessarily
> represent those of T-Mobile USA, Inc.
>
> On Aug 15, 7:25 pm, Dan Sherman <impact...@gmail.com> wrote:
> > Hey guys,
> >
> > Was curious if anyone could point our a way to avoid the following memory
> > leak.
> >
> > When switching layouts in a single activity, and assigning
> OnClickListeners
> > to buttons, each assignment looks to leak a bit of memory.
> >
> > Below is a quick example application that shows it happening with two
> > Button's and a simple function to switch which layout is currently
> active.
> > If run, you'll see the used memory jump slightly with each click between
> > them.
> >
> > I'm aware there's plenty of ways around it (using multiple activities for
> > each layout, using a single OnClickListener thats a member variable which
> > gets assigned to it each time), just curious as to where that extra
> > reference is going thats not getting collected.
> >
> > public class SetContentTest extends Activity {
> >     @Override
> >     public void onCreate(Bundle savedInstanceState) {
> >         super.onCreate(savedInstanceState);
> >         setContentView(R.layout.main);
> >         Button btn = (Button) findViewById(R.id.btn_main);
> >         btn.setOnClickListener(new OnClickListener() {
> >             @Override
> >             public void onClick(View v) {
> >                 setMode(false);
> >             }
> >         });
> >     }
> >
> >     public void setMode(boolean main) {
> >         if (main) {
> >             setContentView(R.layout.main);
> >             Button btn = (Button) findViewById(R.id.btn_main);
> >             btn.setOnClickListener(new OnClickListener() {
> >                 @Override
> >                 public void onClick(View v) {
> >                     setMode(false);
> >                 }
> >             });
> >         } else {
> >             setContentView(R.layout.other);
> >             Button btn = (Button) findViewById(R.id.btn_other);
> >             btn.setOnClickListener(new OnClickListener() {
> >                 @Override
> >                 public void onClick(View v) {
> >                     setMode(true);
> >                 }
> >             });
> >         }
> >     }
> >
> > }
> >
> > And the layouts:
> > <?xml version="1.0" encoding="utf-8"?>
> > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android";
> >     android:orientation="vertical"
> >     android:layout_width="fill_parent"
> >     android:layout_height="fill_parent"
> >     >
> >
> >     <Button
> >         android:id="@+id/btn_main"
> >         android:layout_width="wrap_content"
> >         android:layout_height="wrap_content"
> >         android:text="Click for Other"
> >         />
> > </LinearLayout>
> >
> > ------------------
> >
> > <?xml version="1.0" encoding="utf-8"?>
> > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android";
> >     android:orientation="vertical"
> >     android:layout_width="fill_parent"
> >     android:layout_height="fill_parent"
> >     >
> >
> > <Button
> >         android:id="@+id/btn_other"
> >         android:layout_width="wrap_content"
> >         android:layout_height="wrap_content"
> >         android:text="Click for Main"
> >         />
> > </LinearLayout>
> >
> > Only difference between the layouts is the name (btn_main and btn_other)
> >
> > - Dan
> >
>

--~--~---------~--~----~------------~-------~--~----~
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