標籤:
參考:http://1002878825-qq-com.iteye.com/blog/1735356
出現情況:
1.使用大圖片,沒有及時recycle。
4.0後bitmap.option 中有 inPurgeable 記憶體不足可以回收,之前版本必須自己回收
解決:1.option 減小圖片載入 2.使用軟引用,及時recycle
2.listview的convertView 以及未用holder機制的運用
原因:contentView 系統已經緩衝
解決:1.holder 判斷 contentView是會否為空白
2.提高順滑度listview lazy loading data ----- 參考資料:api demo list 13 滑動時不賦值
public class List13 extends ListActivity implements ListView.OnScrollListener { private TextView mStatus; private boolean mBusy = false; /** * Will not bind views while the list is scrolling * */ private class SlowAdapter extends BaseAdapter { private LayoutInflater mInflater; public SlowAdapter(Context context) { mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } /** * The number of items in the list is determined by the number of speeches * in our array. * * @see android.widget.ListAdapter#getCount() */ public int getCount() { return mStrings.length; } /** * Since the data comes from an array, just returning the index is * sufficent to get at the data. If we were using a more complex data * structure, we would return whatever object represents one row in the * list. * * @see android.widget.ListAdapter#getItem(int) */ public Object getItem(int position) { return position; } /** * Use the array index as a unique id. * * @see android.widget.ListAdapter#getItemId(int) */ public long getItemId(int position) { return position; } /** * Make a view to hold each row. * * @see android.widget.ListAdapter#getView(int, android.view.View, * android.view.ViewGroup) */ public View getView(int position, View convertView, ViewGroup parent) { TextView text; if (convertView == null) { text = (TextView)mInflater.inflate(android.R.layout.simple_list_item_1, parent, false); } else { text = (TextView)convertView; } if (!mBusy) { text.setText(mStrings[position]); // Null tag means the view has the correct data text.setTag(null); } else { text.setText("Loading..."); // Non-null tag means the view still needs to load it‘s data text.setTag(this); } return text; } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.list_13); mStatus = (TextView) findViewById(R.id.status); mStatus.setText("Idle"); // Use an existing ListAdapter that will map an array // of strings to TextViews setListAdapter(new SlowAdapter(this)); getListView().setOnScrollListener(this); } public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { } public void onScrollStateChanged(AbsListView view, int scrollState) { switch (scrollState) { case OnScrollListener.SCROLL_STATE_IDLE: mBusy = false; int first = view.getFirstVisiblePosition(); int count = view.getChildCount(); for (int i=0; i<count; i++) { TextView t = (TextView)view.getChildAt(i); if (t.getTag() != null) { t.setText(mStrings[first + i]); t.setTag(null); } } mStatus.setText("Idle"); break; case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL: mBusy = true; mStatus.setText("Touch scroll"); break; case OnScrollListener.SCROLL_STATE_FLING: mBusy = true; mStatus.setText("Fling"); break; } }View Code
3.單個頁面,橫豎屏切換N次後
解決:1.去除xml中相關設定,改在程式中設定背景圖(放在onCreate()方法中):
2. 跟上面方法相似,直接把xml設定檔載入成view 再放到一個容器裡,然後直接調用 this.setContentView(View view)方法,避免xml的重複載入
3.Activity中添加了 android:configChanges屬性,目的是當所指定屬性(Configuration Changes)發生改變時,通知程式調用 onConfigurationChanged()函數
解決通用方法:
1.SoftReference 和 WeakReference的使用
2.壓縮圖片參考解決bitmap章節
3.修改虛擬機器參數 VMRuntime.getRuntime().setTargetHeapUtilization(0.75);
4.不要為Context長期儲存引用(生命週期結束處理掉)
5.盡量用ApplicationContext
6.記憶體回收行程並不保證能準確回收記憶體,這樣在使用自己需要的內容時,主要生命週期和及時釋放掉不需要的對象。盡量在Activity的生命週期結束時,在onDestroy中把我們做引用的其他對象做釋放,比如:cursor.close()。
android 記憶體溢出