I accidentally posted that last one before I was done.

I have read copious posts on this, but no solution.  After using MAT
extensively on a simple ApiDemo list view sample, I am ABSOLUTELY
convinced that the ListView/ListActivity is leaking memory.  I say
this because I can see every instance I've created persisting in
memory -- regardless of forcing a garbage collection.  To repeat copy
the code below into a project, create another simple Activity and go
back and forth between the two a few times.  Then in DDMS force a GC
and dump the heap hprof file.  Open the OQL tab in MAT and type the
following query "select * from com.<package name>.TmpTestActivity" and
run... you'll notice multiple instances of TmpTestActivity in memory.

Here's my code:


import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class TmpTestActivity extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setListAdapter(new MyListAdapter(this));
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        final MyListAdapter mla =
(MyListAdapter)getListView().getAdapter();
        mla.onDestroy();

        // here I tried setting the activity's contained ListView's
onItemClickListener to null
        // since it references an anonymous non-static inner private
class ... see source for ListActivity
        getListView().setOnItemClickListener(null);
    }

    // Here I made the simple Adapter a static inner class to avoid
implicitly referencing the outer class
    private static class MyListAdapter extends BaseAdapter {

        // I also created an onDestroy function to release the
reference to the outer class's context
        public void onDestroy()
        {
                mContext = null;
        }

        public MyListAdapter(Context context) {
            mContext = context;
        }

        public int getCount() {
            return mStrings.length;
        }

        @Override
        public boolean areAllItemsEnabled() {
            return false;
        }

        @Override
        public boolean isEnabled(int position) {
            return !mStrings[position].startsWith("-");
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup
parent) {
            TextView tv;
            if (convertView == null) {
                tv = (TextView) LayoutInflater.from(mContext).inflate(
 
android.R.layout.simple_expandable_list_item_1, parent, false);
            } else {
                tv = (TextView) convertView;
            }
            tv.setText(mStrings[position]);
            return tv;
        }

        private Context mContext;

        private String[] mStrings = {
                "----------",
                "----------",
                "Abbaye de Belloc",
                "Abbaye du Mont des Cats",
                "Abertam",
                "----------",
                "Abondance",
                "----------",
                "Ackawi",
                "Acorn",
                "Adelost",
                "Affidelice au Chablis",
                "Afuega'l Pitu",
                "Airag",
                "----------",
                "Airedale",
                "Aisy Cendre",
                "----------",
                "Allgauer Emmentaler",
                "Alverca",
                "Ambert",
                "American Cheese",
                "Ami du Chambertin",
                "----------",
                "----------",
                "Anejo Enchilado",
                "Anneau du Vic-Bilh",
                "Anthoriro",
                "----------",
                "----------"
        };
    }
}

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