[英]Out of memory condition for a simple activity
我有一個這樣做的活動:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
ListView lv = (ListView) findViewById(R.id.ListList);
lv.setItemsCanFocus(true);
lv.setOnItemClickListener(this);
adapter = new ListAdapter(this, lv, model, R.layout.simple, this);
lv.setAdapter(adapter);
}
我的列表適配器類如下所示。
public static class ListAdapter extends ArrayAdapter<ListItem>
public ListAdapter(Activity activity, ListView lv, List<ListItem> model, int resource, OnActionClickListener onActionClickListener) {
super(activity, android.R.layout.simple_list_item_1, model);
....
}
如果我進行方向更改(大約20次),它將耗盡內存,並給我這樣的錯誤:
12-22 17:33:44.977: E/dalvikvm-heap(29145): 1536000-byte external allocation too large for this process.
如果我沒有在onCreate(過去的setContentView)中設置其余代碼,則不會有問題。
我猜我正在(無意中)堅持該活動的一個實例,但我不確定如何。
有任何想法嗎?
tia。
因此,導致問題的原因是“ lv.setAdapter(adapter);”。 -如果我注釋掉該行,則不會有問題...我在onDestroy()中嘗試了lv.setAdapter(null)-並沒有幫助。 我還將此內容(http://stackoverflow.com/questions/1147172/what-android-tools-and-methods-work-best-to-find-memory-resource-leaks)也添加到onDestroy()中-似乎什么都沒有幫助。
因此,我將其范圍縮小到最小。
這是主要的TestAppActivity.java文件。
package com.junk.test;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class TestAppActivity extends Activity {
protected ArrayList<ListItem> model = new ArrayList<ListItem>();
protected ListAdapter adapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv = (ListView) findViewById(R.id.ListList);
lv.setItemsCanFocus(true);
adapter = new ListAdapter(this, lv, model);
lv.setAdapter(adapter);
if (savedInstanceState == null) {
Log.i("test", "@@ start");
new Thread(new Runnable() {
@Override
public void run() {
try {
boolean change = true;
Thread.sleep(4 * 1000);
for (int ii = 0; ii < 200; ii++) {
setRequestedOrientation(change ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE : ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Thread.sleep(4 * 1000);
change = !change;
Log.i("test", "@@ change: " + (ii+1));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
Log.i("test", "@@ complete!");
}
}
}).start();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
// doesn't make any difference.
// ListView lv = (ListView) findViewById(R.id.ListList);
// lv.setAdapter(null);
// adapter.clear();
// adapter = null;
unbindDrawables(findViewById(R.id.ListList));
System.gc();
}
private void unbindDrawables(View view) {
if (view == null) { return; }
if (view.getBackground() != null) {
view.getBackground().setCallback(null);
}
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
unbindDrawables(((ViewGroup) view).getChildAt(i));
}
if (view instanceof AdapterView) {
} else {
((ViewGroup) view).removeAllViews();
}
}
}
public static class ListItem {
public int x;
}
public static class ListAdapter extends ArrayAdapter<ListItem> implements View.OnClickListener {
public ListAdapter(Activity activity, ListView lv, List<ListItem> model) {
super(activity.getApplicationContext(), android.R.layout.simple_list_item_1, model);
}
@Override
public void onClick(View v) {
}
}
}
這是main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/player_background">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<ListView
android:id="@+id/ListList"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:choiceMode="singleChoice"
android:cacheColorHint="#00000000"
android:dividerHeight="3dp"
android:overScrollFooter="@null" />
</LinearLayout>
而且它仍然在泄漏-大約需要110左右的方向更改,但最終會產生分配錯誤!
Eclipse內存分析器工具可以幫助您找到根本原因。 通常的情況是在應用程序啟動后立即進行堆轉儲,然后進行一些方向更改,使用DDMS強制GC,然后進行另一個堆轉儲。 之后,您可以比較堆轉儲,查看是否有剩余活動或其他活動。 “ GC根目錄路徑”工具可能顯示丟失的引用,該引用阻止收集剩余的對象。
檢查此問題以獲取有關MAT的更多參考。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.