[android-developers] ListView leaks memory II

2011-03-30 Thread ivan
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


Re: [android-developers] ListView leaks memory II

2011-03-30 Thread Mark Murphy
On Wed, Mar 30, 2011 at 1:18 PM, ivan istas...@gmail.com wrote:
 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.

Has onDestroy() been called on the multiple instances of
TmpTestActivity in memory?

If not, they are not leaked. Android just hasn't kicked them out of
RAM yet. That is perfectly normal behavior.

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

_The Busy Coder's Guide to *Advanced* Android Development_ Version
1.9.2 Available!

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