当在代码下运行时,GC对GC_CONCURRENT感到厌烦

时间:2012-10-16 21:05:19

标签: android garbage-collection android-listview android-viewpager

我用以下方法通过电话号码提取联系人姓名和缩略图。 当它运行GC疯了。 Methos位于实用程序类中,并从适配器调用以填充列表视图。 List有超过20个项目/数字来查询联系人db。

我是Java和Android的新手我似乎无法弄清楚出了什么问题,或者如果没有什么问题可以优化它。

主要问题是listview位于ViewPager中,这使得水平滚动非常缓慢和生涩。

public static Contact getContact(Context ctx, String phoneNumber)
{
    String contactName = "";
    Long contactId= 0L;
    Bitmap contactPhoto =null;
    ContentResolver localContentResolver = ctx.getContentResolver();
    Cursor contactLookupCursor =  
       localContentResolver.query(
                Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, 
                Uri.encode(phoneNumber)), 
                new String[] {PhoneLookup.DISPLAY_NAME, BaseColumns._ID}, 
                null, 
                null, 
                null);
    try {
    while(contactLookupCursor.moveToNext()){
        contactName = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
        contactId = contactLookupCursor.getLong(contactLookupCursor.getColumnIndexOrThrow(BaseColumns._ID));
        Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
        InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(ctx.getContentResolver(), uri);
        contactPhoto = BitmapFactory.decodeStream(input);
        }
    }
    finally {
    contactLookupCursor.close();
    } 

    return new Contact(contactName,contactId,contactPhoto);
}

以下是来自logcat的示例

 10-16 22:05:39.745: D/dalvikvm(29171): GC_CO

NCURRENT freed 322K, 6% free 15200K/16071K, paused 1ms+11ms
10-16 22:05:40.005: D/dalvikvm(29171): GC_FOR_ALLOC freed 1428K, 12% free 14170K/16071K, paused 15ms
10-16 22:05:40.080: D/dalvikvm(29171): GC_CONCURRENT freed 70K, 12% free 14303K/16071K, paused 1ms+8ms
10-16 22:05:40.230: D/dalvikvm(29171): GC_CONCURRENT freed 305K, 10% free 14476K/16071K, paused 2ms+1ms
10-16 22:05:40.315: D/dalvikvm(29171): GC_EXPLICIT freed 169K, 11% free 14348K/16071K, paused 4ms+12ms
10-16 22:05:40.660: D/dalvikvm(29171): GC_CONCURRENT freed 329K, 10% free 14495K/16071K, paused 1ms+3ms
10-16 22:05:40.850: D/dalvikvm(29171): GC_CONCURRENT freed 543K, 11% free 14416K/16071K, paused 1ms+1ms
10-16 22:05:40.980: D/dalvikvm(29171): GC_CONCURRENT freed 380K, 10% free 14497K/16071K, paused 1ms+4ms
10-16 22:05:41.135: D/dalvikvm(29171): GC_CONCURRENT freed 521K, 11% free 14440K/16071K, paused 2ms+2ms
10-16 22:05:41.375: D/dalvikvm(29171): GC_CONCURRENT freed 406K, 11% free 14444K/16071K, paused 1ms+2ms
10-16 22:05:41.470: D/dalvikvm(29171): GC_CONCURRENT freed 270K, 10% free 14583K/16071K, paused 2ms+2ms
10-16 22:05:41.680: D/dalvikvm(29171): GC_CONCURRENT freed 290K, 9% free 14774K/16071K, paused 2ms+4ms
10-16 22:05:41.760: D/dalvikvm(29171): GC_EXPLICIT freed 98K, 9% free 14749K/16071K, paused 3ms+2ms
10-16 22:05:41.900: D/dalvikvm(29171): GC_CONCURRENT freed 199K, 7% free 14973K/16071K, paused 1ms+3ms
10-16 22:05:42.070: D/dalvikvm(29171): GC_CONCURRENT freed 277K, 7% free 15086K/16071K, paused 1ms+3ms
10-16 22:05:42.335: D/dalvikvm(29171): GC_CONCURRENT freed 416K, 6% free 15158K/16071K, paused 1ms+3ms
10-16 22:05:42.430: D/dalvikvm(29171): GC_CONCURRENT freed 1235K, 11% free 14315K/16071K, paused 1ms+2ms
10-16 22:05:42.525: D/dalvikvm(29171): GC_CONCURRENT freed 452K, 11% free 14364K/16071K, paused 2ms+3ms
10-16 22:05:42.925: D/dalvikvm(29171): GC_CONCURRENT freed 371K, 11% free 14445K/16071K, paused 2ms+1ms
10-16 22:05:43.045: D/dalvikvm(29171): GC_CONCURRENT freed 495K, 11% free 14335K/16071K, paused 2ms+2ms
10-16 22:05:43.190: D/dalvikvm(29171): GC_CONCURRENT freed 295K, 11% free 14431K/16071K, paused 2ms+3ms
10-16 22:05:43.315: D/dalvikvm(29171): GC_EXPLICIT freed 236K, 11% free 14329K/16071K, paused 2ms+3ms
10-16 22:05:43.575: D/dalvikvm(29171): GC_CONCURRENT freed 316K, 11% free 14436K/16071K, paused 1ms+1ms
10-16 22:05:43.660: D/dalvikvm(29171): GC_CONCURRENT freed 281K, 10% free 14585K/16071K, paused 1ms+2ms
10-16 22:05:43.905: D/dalvikvm(29171): GC_CONCURRENT freed 309K, 9% free 14754K/16071K, paused 2ms+3ms
10-16 22:05:44.070: D/dalvikvm(29171): GC_CONCURRENT freed 270K, 7% free 14968K/16071K, paused 2ms+2ms
10-16 22:05:44.300: D/dalvikvm(29171): GC_CONCURRENT freed 265K, 7% free 15103K/16071K, paused 1ms+4ms
10-16 22:05:44.495: D/dalvikvm(29171): GC_CONCURRENT freed 400K, 6% free 15176K/16071K, paused 2ms+5ms
10-16 22:05:44.695: D/dalvikvm(29171): GC_CONCURRENT freed 447K, 6% free 15137K/16071K, paused 3ms+6ms
10-16 22:05:44.780: D/dalvikvm(29171): GC_EXPLICIT freed 354K, 7% free 15037K/16071K, paused 2ms+3ms
10-16 22:05:45.075: D/dalvikvm(29171): GC_CONCURRENT freed 422K, 7% free 15104K/16071K, paused 1ms+2ms
10-16 22:05:45.140: D/AbsListView(29171): Get MotionRecognitionManager
10-16 22:05:45.340: D/dalvikvm(29171): GC_CONCURRENT freed 293K, 6% free 15252K/16071K, paused 2ms+6ms
10-16 22:05:45.340: D/AbsListView(29171): [unregisterDoubleTapMotionListener]
10-16 22:05:45.340: I/MotionRecognitionManager(29171):   .unregisterListener : / listener count = 0->0, listener=android.widget.AbsListView$4@41a600c8
10-16 22:05:45.530: D/dalvikvm(29171): GC_CONCURRENT freed 849K, 8% free 14799K/16071K, paused 2ms+7ms
10-16 22:05:45.720: D/dalvikvm(29171): GC_CONCURRENT freed 394K, 8% free 14908K/16071K, paused 3ms+5ms
10-16 22:05:45.940: D/dalvikvm(29171): GC_CONCURRENT freed 452K, 8% free 14845K/16071K, paused 2ms+3ms
10-16 22:05:46.085: D/dalvikvm(29171): GC_CONCURRENT freed 374K, 8% free 14917K/16071K, paused 1ms+3ms
10-16 22:05:46.335: D/dalvikvm(29171): GC_CONCURRENT freed 457K, 8% free 14892K/16071K, paused 1ms+2ms
10-16 22:05:46.390: D/dalvikvm(29171): GC_EXPLICIT freed 148K, 8% free 14870K/16071K, paused 1ms+3ms
10-16 22:05:46.550: D/dalvikvm(29171): GC_CONCURRENT freed 415K, 8% free 14844K/16071K, paused 2ms+4ms
10-16 22:05:46.680: D/dalvikvm(29171): GC_CONCURRENT freed 223K, 7% free 15046K/16071K, paused 2ms+3ms
10-16 22:05:46.825: D/AbsListView(29171): Get MotionRecognitionManager
10-16 22:05:46.965: D/dalvikvm(29171): GC_CONCURRENT freed 346K, 6% free 15124K/16071K, paused 1ms+3ms
10-16 22:05:46.965: D/AbsListView(29171): [unregisterDoubleTapMotionListener]
10-16 22:05:46.965: I/MotionRecognitionManager(29171):   .unregisterListener : / listener count = 0->0, listener=android.widget.AbsListView$4@41a49d90
10-16 22:05:47.095: D/dalvikvm(29171): GC_CONCURRENT freed 755K, 9% free 14758K/16071K, paused 2ms+5ms
10-16 22:05:47.375: D/dalvikvm(29171): GC_CONCURRENT freed 416K, 8% free 14835K/16071K, paused 2ms+5ms
10-16 22:05:47.565: D/dalvikvm(29171): GC_CONCURRENT freed 415K, 8% free 14888K/16071K, paused 2ms+5ms
10-16 22:05:47.975: D/dalvikvm(29171): GC_CONCURRENT freed 475K, 8% free 14814K/16071K, paused 2ms+3ms
10-16 22:05:48.105: D/dalvikvm(29171): GC_CONCURRENT freed 411K, 8% free 14867K/16071K, paused 2ms+3ms
10-16 22:05:48.155: D/dalvikvm(29171): GC_EXPLICIT freed 114K, 9% free 14760K/16071K, paused 2ms+4ms
10-16 22:05:48.280: D/dalvikvm(29171): GC_CONCURRENT freed 284K, 8% free 14902K/16071K, paused 1ms+3ms
10-16 22:05:48.435: D/dalvikvm(29171): GC_CONCURRENT freed 332K, 7% free 15000K/16071K, paused 3ms+4ms
10-16 22:05:48.655: D/dalvikvm(29171): GC_CONCURRENT freed 415K, 7% free 14985K/16071K, paused 1ms+2ms
10-16 22:05:48.805: D/dalvikvm(29171): GC_CONCURRENT freed 451K, 7% free 15002K/16071K, paused 3ms+5ms
10-16 22:05:49.150: D/dalvikvm(29171): GC_CONCURRENT freed 417K, 7% free 14985K/16071K, paused 1ms+2ms

2 个答案:

答案 0 :(得分:3)

您正在遍历整个结果集,为所有缩略图创建位图,然后将所有结果丢弃,除了最后一个。如果你只想要最后一个,只需处理最后一个。

答案 1 :(得分:2)

一个选项是在Activity启动时进行光标查找,并将缩略图和电话号码保存到对象,然后在缓存中列出这些对象。

然后,当您显示ListView时,您可以从本地缓存中读取,而不是每次都为每个项目执行数据库查找。


创建一个返回所有联系人的查询,然后查询此游标结果中的电话号码,从而查询该行,也不是更好吗?