我用以下方法通过电话号码拉取联系人姓名和缩略图.
当它运行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_CONCURRENT freed 322K, 6% free 15200K/16071K, paused 1ms+11ms10-16 22:05:40.005: D/dalvikvm(29171): GC_FOR_ALLOC freed 1428K, 12% free 14170K/16071K, paused 15ms10-16 22:05:40.080: D/dalvikvm(29171): GC_CONCURRENT freed 70K, 12% free 14303K/16071K, paused 1ms+8ms10-16 22:05:40.230: D/dalvikvm(29171): GC_CONCURRENT freed 305K, 10% free 14476K/16071K, paused 2ms+1ms10-16 22:05:40.315: D/dalvikvm(29171): GC_EXPliCIT freed 169K, 11% free 14348K/16071K, paused 4ms+12ms10-16 22:05:40.660: D/dalvikvm(29171): GC_CONCURRENT freed 329K, 10% free 14495K/16071K, paused 1ms+3ms10-16 22:05:40.850: D/dalvikvm(29171): GC_CONCURRENT freed 543K, 11% free 14416K/16071K, paused 1ms+1ms10-16 22:05:40.980: D/dalvikvm(29171): GC_CONCURRENT freed 380K, 10% free 14497K/16071K, paused 1ms+4ms10-16 22:05:41.135: D/dalvikvm(29171): GC_CONCURRENT freed 521K, 11% free 14440K/16071K, paused 2ms+2ms10-16 22:05:41.375: D/dalvikvm(29171): GC_CONCURRENT freed 406K, 11% free 14444K/16071K, paused 1ms+2ms10-16 22:05:41.470: D/dalvikvm(29171): GC_CONCURRENT freed 270K, 10% free 14583K/16071K, paused 2ms+2ms10-16 22:05:41.680: D/dalvikvm(29171): GC_CONCURRENT freed 290K, 9% free 14774K/16071K, paused 2ms+4ms10-16 22:05:41.760: D/dalvikvm(29171): GC_EXPliCIT freed 98K, 9% free 14749K/16071K, paused 3ms+2ms10-16 22:05:41.900: D/dalvikvm(29171): GC_CONCURRENT freed 199K, 7% free 14973K/16071K, paused 1ms+3ms10-16 22:05:42.070: D/dalvikvm(29171): GC_CONCURRENT freed 277K, 7% free 15086K/16071K, paused 1ms+3ms10-16 22:05:42.335: D/dalvikvm(29171): GC_CONCURRENT freed 416K, 6% free 15158K/16071K, paused 1ms+3ms10-16 22:05:42.430: D/dalvikvm(29171): GC_CONCURRENT freed 1235K, 11% free 14315K/16071K, paused 1ms+2ms10-16 22:05:42.525: D/dalvikvm(29171): GC_CONCURRENT freed 452K, 11% free 14364K/16071K, paused 2ms+3ms10-16 22:05:42.925: D/dalvikvm(29171): GC_CONCURRENT freed 371K, 11% free 14445K/16071K, paused 2ms+1ms10-16 22:05:43.045: D/dalvikvm(29171): GC_CONCURRENT freed 495K, 11% free 14335K/16071K, paused 2ms+2ms10-16 22:05:43.190: D/dalvikvm(29171): GC_CONCURRENT freed 295K, 11% free 14431K/16071K, paused 2ms+3ms10-16 22:05:43.315: D/dalvikvm(29171): GC_EXPliCIT freed 236K, 11% free 14329K/16071K, paused 2ms+3ms10-16 22:05:43.575: D/dalvikvm(29171): GC_CONCURRENT freed 316K, 11% free 14436K/16071K, paused 1ms+1ms10-16 22:05:43.660: D/dalvikvm(29171): GC_CONCURRENT freed 281K, 10% free 14585K/16071K, paused 1ms+2ms10-16 22:05:43.905: D/dalvikvm(29171): GC_CONCURRENT freed 309K, 9% free 14754K/16071K, paused 2ms+3ms10-16 22:05:44.070: D/dalvikvm(29171): GC_CONCURRENT freed 270K, 7% free 14968K/16071K, paused 2ms+2ms10-16 22:05:44.300: D/dalvikvm(29171): GC_CONCURRENT freed 265K, 7% free 15103K/16071K, paused 1ms+4ms10-16 22:05:44.495: D/dalvikvm(29171): GC_CONCURRENT freed 400K, 6% free 15176K/16071K, paused 2ms+5ms10-16 22:05:44.695: D/dalvikvm(29171): GC_CONCURRENT freed 447K, 6% free 15137K/16071K, paused 3ms+6ms10-16 22:05:44.780: D/dalvikvm(29171): GC_EXPliCIT freed 354K, 7% free 15037K/16071K, paused 2ms+3ms10-16 22:05:45.075: D/dalvikvm(29171): GC_CONCURRENT freed 422K, 7% free 15104K/16071K, paused 1ms+2ms10-16 22:05:45.140: D/AbsListVIEw(29171): Get MotionRecognitionManager10-16 22:05:45.340: D/dalvikvm(29171): GC_CONCURRENT freed 293K, 6% free 15252K/16071K, paused 2ms+6ms10-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@41a600c810-16 22:05:45.530: D/dalvikvm(29171): GC_CONCURRENT freed 849K, 8% free 14799K/16071K, paused 2ms+7ms10-16 22:05:45.720: D/dalvikvm(29171): GC_CONCURRENT freed 394K, 8% free 14908K/16071K, paused 3ms+5ms10-16 22:05:45.940: D/dalvikvm(29171): GC_CONCURRENT freed 452K, 8% free 14845K/16071K, paused 2ms+3ms10-16 22:05:46.085: D/dalvikvm(29171): GC_CONCURRENT freed 374K, 8% free 14917K/16071K, paused 1ms+3ms10-16 22:05:46.335: D/dalvikvm(29171): GC_CONCURRENT freed 457K, 8% free 14892K/16071K, paused 1ms+2ms10-16 22:05:46.390: D/dalvikvm(29171): GC_EXPliCIT freed 148K, 8% free 14870K/16071K, paused 1ms+3ms10-16 22:05:46.550: D/dalvikvm(29171): GC_CONCURRENT freed 415K, 8% free 14844K/16071K, paused 2ms+4ms10-16 22:05:46.680: D/dalvikvm(29171): GC_CONCURRENT freed 223K, 7% free 15046K/16071K, paused 2ms+3ms10-16 22:05:46.825: D/AbsListVIEw(29171): Get MotionRecognitionManager10-16 22:05:46.965: D/dalvikvm(29171): GC_CONCURRENT freed 346K, 6% free 15124K/16071K, paused 1ms+3ms10-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@41a49d9010-16 22:05:47.095: D/dalvikvm(29171): GC_CONCURRENT freed 755K, 9% free 14758K/16071K, paused 2ms+5ms10-16 22:05:47.375: D/dalvikvm(29171): GC_CONCURRENT freed 416K, 8% free 14835K/16071K, paused 2ms+5ms10-16 22:05:47.565: D/dalvikvm(29171): GC_CONCURRENT freed 415K, 8% free 14888K/16071K, paused 2ms+5ms10-16 22:05:47.975: D/dalvikvm(29171): GC_CONCURRENT freed 475K, 8% free 14814K/16071K, paused 2ms+3ms10-16 22:05:48.105: D/dalvikvm(29171): GC_CONCURRENT freed 411K, 8% free 14867K/16071K, paused 2ms+3ms10-16 22:05:48.155: D/dalvikvm(29171): GC_EXPliCIT freed 114K, 9% free 14760K/16071K, paused 2ms+4ms10-16 22:05:48.280: D/dalvikvm(29171): GC_CONCURRENT freed 284K, 8% free 14902K/16071K, paused 1ms+3ms10-16 22:05:48.435: D/dalvikvm(29171): GC_CONCURRENT freed 332K, 7% free 15000K/16071K, paused 3ms+4ms10-16 22:05:48.655: D/dalvikvm(29171): GC_CONCURRENT freed 415K, 7% free 14985K/16071K, paused 1ms+2ms10-16 22:05:48.805: D/dalvikvm(29171): GC_CONCURRENT freed 451K, 7% free 15002K/16071K, paused 3ms+5ms10-16 22:05:49.150: D/dalvikvm(29171): GC_CONCURRENT freed 417K, 7% free 14985K/16071K, paused 1ms+2ms
解决方法:
您正在迭代整个结果集,为所有缩略图创建位图,然后抛弃除最后一个之外的所有结果.如果你只想要最后一个,只需处理最后一个.
总结以上是内存溢出为你收集整理的android – GC在代码下运行时会对GC_CONCURRENT感到厌烦全部内容,希望文章能够帮你解决android – GC在代码下运行时会对GC_CONCURRENT感到厌烦所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)