开发者

My Simple ListView app is leaking memory. What am I doing wrong?‏

First off, I did post this to the android google group first but its moderated and I'm not sure how long itll take to show up there so hoping someone here can help.

I created a simple app with a ListView following the ListActivity examples I found on the net.

The app has 2 activities with the first having a button to create the second. When i hit the close button on the second activity I would like it to release its memory (or at least allow it to be garbage collected). Currently it will never release.

I must be doing something wrong here because the MyListActivity never gets released. Can anyone tell me if I am doing something wrong with the way my activities are created/destroyed? or if my usage of the ListView is wrong?

Thanks.

My App as a zip - http://www.mediafire.com/?l26o5hz2bmbwk6j

Screen Shot of Eclipse MAT showing the list activity never releasing memory - www.mediafire.com/?qr6ga0k

public class MyListActivity extends ListActivity {  
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listlayout);        
    ListAdapter ada = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, GENRES);        开发者_StackOverflow       
    setListAdapter(ada);    
}

@Override
public void onDestroy()
{                   
    super.onDestroy();
    System.gc();
}

public void ClickHandler(View target)
{
    switch (target.getId())
    {
        case R.id.LL_Btn1:
            finish();
            break;
    }
}   

private static final String[] GENRES = new String[] {
    "Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama",
    "Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller"
};} 

public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

public void ClickHandler(View target)
{
    switch (target.getId())
    {
        case R.id.M_Button01:
            Intent intent = new Intent();
            intent.setClassName(MyListActivity.class.getPackage().getName(), MyListActivity.class.getName());         
            startActivity(intent);  
            break;
    }
}}


Have you tried getting rid of System.gc() and see what happens? Calling System.gc() is merely telling the VM to initiate garbage collection - there is no guarantee when it will be done.


Not sure if this will help, and its not generally recommended to do this, but the following will kill your process in onDestroy():

System.runFinalizersOnExit(true); android.os.Process.killProcess(android.os.Process.myPid());

Put that in the onDestroy method. Now this may actually kill your main process if called from a child activity (havn't tested it).

WARNING: I generally don't recommend doing this as its a "hackly" way of doing it, but if you just need your app (or activity) to close upon exit then this will work. You still need to debug to find out why your app is staying open and eating memory.


Eclipse MAT was the cause. New Android Studio doesn't cause these problems.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜