我如何改进我的逻辑以减少内存消耗和快速 UI,因为它在滚动时由于 GC 调用而滞后。
我使用 Eclipse MAT 发现了 3 个嫌疑人:
问题嫌疑人1
“”加载的一个“android.graphics.Bitmap”实例占用500,464(14.70%)字节。内存在“”加载的“byte[]”的一个实例中累积。
问题嫌疑人2
2906个“java.lang.Class”实例,由“”加载,占用1121048(32.92%)字节。
最大实例:
•class android.text.Html$HtmlParser @ 0xb4ef4448 - 126,720 (3.72%) bytes.
•class org.apache.harmony.security.fortress.Services @ 0xb4ee7b48 - 80,120 (2.35%) bytes.
•class libcore.icu.TimeZones @ 0xb4e0d2b0 - 69,144 (2.03%) bytes.
•class java.lang.ref.FinalizerReference @ 0xb4e07ef0 - 59,080 (1.74%) bytes.
•class com.android.internal.R$styleable @ 0xb4e2ddd0 - 57,192 (1.68%) bytes.
•class android.R$styleable @ 0xb4e74e78 - 52,896 (1.55%) bytes.
•class android.view.View @ 0xb4e2c228 - 46,344 (1.36%) bytes.
问题嫌疑人3
8,040 个“java.lang.String”实例,由“”加载,占用 514,408 (15.11%) 个字节。
我的适配器代码
public class SomeAdapter extends BaseAdapter{
private static int objectCounter;
private static Context context;
private final List<Data> dataList;
private final UserData userData = DatabaseManager.userData;
private LayoutInflater inflater;
private ViewHolder viewHolder;
// Declared Typeface Global as to prevent repeated call from ViewHolder
private static Typeface typefaceB;
private static Typeface typefaceR;
private static int id; // selector | color Resource ID
static class ViewHolder {
public TextView tv1;
public TextView tv2;
public TextView tv3;
public ImageView iv1;
private void setView() {
tv1.setTypeface(typefaceB);
tv2.setTypeface(typefaceR);
iv1.setBackgroundResource(id);
tv1.setBackgroundResource(id);
tv2.setBackgroundResource(id);
tv3.setBackgroundResource(id);
tv2.setTextSize(18);
tv1.setTextColor(Color.parseColor("#3D3D3D"));
tv2.setTextColor(Color.parseColor("#3D3D3D"));
tv3.setTextColor(Color.parseColor("#3D3D3D"));
}
}
public SomeAdapter(Context context, final List<Data> dataList) {
objectCounter++;
System.out.println("SomeAdapter " + objectCounter) ;
int user = Login.getUserId();
this.id = userData.getUserLastColor(user);
this.context = context.getApplicationContext();
this.dataList = dataList;
this.typefaceB = Typeface.createFromAsset(this.context.getAssets(), "fonts/RobotoSlab-Bold.ttf");
this.typefaceR = Typeface.createFromAsset(this.context.getAssets(), "fonts/RobotoSlab-Regular.ttf");
inflater = LayoutInflater.from(this.context);
}
@Override
public int getCount() {
return dataList.size();
}
@Override
public Object getItem(int position) {
return dataList.get(position);
}
@Override
public long getItemId(int position) {
return dataList.get(position).get_some_id();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Data data = dataList.get(position);
if(convertView == null || convertView.getTag() == null) {
convertView = inflater.inflate(R.layout.some_layout, null);
viewHolder = new ViewHolder();
viewHolder.tv1 = (TextView) convertView.findViewById(R.id.tv1);
viewHolder.tv2 = (TextView) convertView.findViewById(R.id.tv3);
viewHolder.tv3 = (TextView) convertView.findViewById(R.id.tv3);
viewHolder.iv1 = (ImageView) convertView.findViewById(R.id.iv1);
viewHolder.setView();
convertView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.tv1.setText("Text 1");
viewHolder.tv2.setText(Html.fromHtml("Text 2"));
if (data.updated_time != null && !data.updated_time.isEmpty()) {
viewHolder.tv3.setText(UtilFunction.formateDate(
"MMM dd, yyyy, h:mm aa",
UtilFunction.gmtToLocal(data.updated_time)));
} else {
viewHolder.tv3.setText(UtilFunction.formateDate(
"MMM dd, yyyy, h:mm aa",
UtilFunction.gmtToLocal(data.created_time)));
}
return convertView;
}
}
日志猫
10-08 16:22:23.328: D/dalvikvm(13167): GC_CONCURRENT freed 253K, 11% free 8003K/8903K, paused 18ms+3ms, total 45ms
10-08 16:22:23.453: I/Choreographer(13167): Skipped 42 frames! The application may be doing too much work on its main thread.
10-08 16:22:31.929: D/dalvikvm(13167): GC_CONCURRENT freed 240K, 10% free 8195K/9095K, paused 3ms+5ms, total 37ms
10-08 16:22:36.179: D/dalvikvm(13167): GC_CONCURRENT freed 439K, 12% free 8219K/9287K, paused 3ms+15ms, total 55ms
10-08 16:22:40.695: D/dalvikvm(13167): GC_CONCURRENT freed 423K, 12% free 8203K/9287K, paused 13ms+5ms, total 46ms
10-08 16:22:42.695: D/dalvikvm(13167): GC_CONCURRENT freed 438K, 12% free 8213K/9287K, paused 13ms+5ms, total 47ms
10-08 16:22:43.515: D/dalvikvm(13167): GC_CONCURRENT freed 423K, 12% free 8223K/9287K, paused 23ms+5ms, total 67ms
10-08 16:22:54.921: D/dalvikvm(13167): GC_CONCURRENT freed 433K, 12% free 8212K/9287K, paused 13ms+6ms, total 51ms
10-08 16:22:55.421: D/dalvikvm(13167): GC_CONCURRENT freed 415K, 12% free 8217K/9287K, paused 13ms+5ms, total 58ms
10-08 16:22:56.367: D/dalvikvm(13167): GC_CONCURRENT freed 422K, 12% free 8217K/9287K, paused 2ms+4ms, total 32ms
10-08 16:22:56.898: D/dalvikvm(13167): GC_CONCURRENT freed 446K, 12% free 8219K/9287K, paused 1ms+4ms, total 32ms
10-08 16:22:57.500: D/dalvikvm(13167): GC_CONCURRENT freed 441K, 12% free 8213K/9287K, paused 13ms+5ms, total 52ms
10-08 16:22:58.851: D/dalvikvm(13167): GC_CONCURRENT freed 416K, 12% free 8217K/9287K, paused 16ms+5ms, total 58ms
10-08 16:22:59.398: D/dalvikvm(13167): GC_CONCURRENT freed 453K, 12% free 8220K/9287K, paused 2ms+14ms, total 45ms
10-08 16:22:59.750: D/dalvikvm(13167): GC_CONCURRENT freed 415K, 12% free 8217K/9287K, paused 16ms+5ms, total 58ms
10-08 16:23:00.156: D/dalvikvm(13167): GC_CONCURRENT freed 440K, 12% free 8220K/9287K, paused 13ms+15ms, total 67ms
10-08 16:23:00.671: D/dalvikvm(13167): GC_CONCURRENT freed 439K, 12% free 8225K/9287K, paused 12ms+14ms, total 50ms
10-08 16:23:01.000: D/dalvikvm(13167): GC_CONCURRENT freed 442K, 12% free 8206K/9287K, paused 12ms+15ms, total 50ms
10-08 16:23:01.796: D/dalvikvm(13167): GC_CONCURRENT freed 423K, 12% free 8220K/9287K, paused 12ms+17ms, total 66ms
10-08 16:23:02.851: D/dalvikvm(13167): GC_CONCURRENT freed 422K, 12% free 8208K/9287K, paused 12ms+4ms, total 46ms
谢谢你的时间...