我在日志猫中得到了无尽的GC_FOR_ALLOC,如下所示:
32ms, total 32ms
07-01 12:23:28.946 14905-14905/com.dodosocial.lowongan D/dalvikvm﹕ GC_FOR_ALLOC freed 1994K, 32% free 18349K/26896K, paused 15ms, total 15ms
07-01 12:23:29.116 14905-14905/com.dodosocial.lowongan D/dalvikvm﹕ GC_FOR_ALLOC freed 3006K, 32% free 18381K/26896K, paused 20ms, total 20ms
07-01 12:23:29.376 14905-14905/com.dodosocial.lowongan D/dalvikvm﹕ GC_FOR_ALLOC freed 4671K, 33% free 18284K/26896K, paused 17ms, total 17ms
07-01 12:23:29.496 14905-14905/com.dodosocial.lowongan D/dalvikvm﹕ GC_FOR_ALLOC freed 1898K, 32% free 18379K/26896K, paused 18ms, total 18ms
07-01 12:23:29.606 14905-14905/com.dodosocial.lowongan D/dalvikvm﹕ GC_FOR_ALLOC freed 1994K, 32% free 18349K/26896K, paused 17ms, total 17ms
07-01 12:23:29.806 14905-14905/com.dodosocial.lowongan D/dalvikvm﹕ GC_FOR_ALLOC freed 3006K, 32% free 18381K/26896K, paused 15ms, total 16ms
07-01 12:23:30.086 14905-14905/com.dodosocial.lowongan D/dalvikvm﹕ GC_FOR_ALLOC freed 4671K, 33% free 18283K/26896K, paused 17ms, total 19ms
07-01 12:23:30.206 14905-14905/com.dodosocial.lowongan D/dalvikvm﹕ GC_FOR_ALLOC freed 2035K, 33% free 18242K/26896K, paused 14ms, total 15ms
07-01 12:23:30.316 14905-14905/com.dodosocial.lowongan D/dalvikvm﹕ GC_FOR_ALLOC freed 1994K, 33% free 18212K/26896K, paused 11ms, total 12ms
07-01 12:23:30.486 14905-14905/com.dodosocial.lowongan D/dalvikvm﹕ GC_FOR_ALLOC freed 3007K, 33% free 18245K/26896K, paused 17ms, total 17ms
07-01 12:23:30.736 14905-14905/com.dodosocial.lowongan D/dalvikvm﹕ GC_FOR_ALLOC freed 4359K, 33% free 18146K/26896K, paused 17ms, total 18ms
07-01 12:23:30.856 14905-14905/com.dodosocial.lowongan D/dalvikvm﹕ GC_FOR_ALLOC freed 1898K, 33% free 18242K/26896K, paused 16ms, total 17ms
07-01 12:23:30.966 14905-14905/com.dodosocial.lowongan D/dalvikvm﹕ GC_FOR_ALLOC freed 1994K, 33% free 18212K/26896K, paused 15ms, total 15ms
07-01 12:23:31.136 14905-14905/com.dodosocial.lowongan D/dalvikvm﹕ GC_FOR_ALLOC freed 3006K, 33% free 18245K/26896K, paused 20ms, total 21ms
07-01 12:23:31.396 14905-14905/com.dodosocial.lowongan D/dalvikvm﹕ GC_FOR_ALLOC freed 4359K, 33% free 18146K/26896K, paused 17ms, total 17ms
07-01 12:23:31.516 14905-14905/com.dodosocial.lowongan D/dalvikvm﹕ GC_FOR_ALLOC freed 1898K, 33% free 18242K/26896K, paused 17ms, total 17ms
07-01 12:23:31.636 14905-14905/com.dodosocial.lowongan D/dalvikvm﹕ GC_FOR_ALLOC freed 1994K, 33% free 18212K/26896K, paused 19ms, total 20ms
07-01 12:23:31.816 14905-14905/com.dodosocial.lowon
找出错误后,我发现这是因为我以编程方式修改了ImageView的高度。
这是我使用的代码:
MyActivity.java
private ImageView mImageUserProfile;
private Bitmap mBitmapIcon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageUserProfile = (ImageView)findViewById(R.id.login_icon);
mImageUserProfile.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mBitmapIcon = CommUtils.getUserProfileBitmap();
if(mBitmapIcon!=null) {
setupIcon();
}
}
});
}
private void setupIcon() {
int bmHeight = mBitmapIcon.getHeight();
int bmWidth = mBitmapIcon.getWidth();
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)mImageUserProfile.getLayoutParams();
layoutParams.height = layoutParams.width*bmHeight/bmWidth;
mImageUserProfile.setLayoutParams(layoutParams);
mImageUserProfile.setImageBitmap(mBitmapIcon);
}
删除了如下所示的更改高度的代码后,GC_FOR_ALLOC的内容消失了
private void setupIcon() {
int bmHeight = mBitmapIcon.getHeight();
int bmWidth = mBitmapIcon.getWidth();
/*
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)mImageUserProfile.getLayoutParams();
layoutParams.height = layoutParams.width*bmHeight/bmWidth;
mImageUserProfile.setLayoutParams(layoutParams);
*/
mImageUserProfile.setImageBitmap(mBitmapIcon);
}
最佳答案
就像@GabeSechan在对您的问题的评论中所说的那样,由于未删除布局侦听器,导致了无限循环。
通过更改ImageView
的高度,可以使它的尺寸无效,从而导致布局不合格。系统将重新布局,然后重新触发GlobalLayoutListener
,然后重新触发代码以更改高度...这将导致其他布局...等等。
如果您只记得在更改布局之前删除监听器,则应该没问题。
使用您的代码看起来像这样:
mImageUserProfile
.getViewTreeObserver()
.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mBitmapIcon = CommUtils.getUserProfileBitmap();
if(mBitmapIcon!=null) {
mImageUserProfile
.getViewTreeObserver()
.removeGlobalOnLayoutListener(this);
setupIcon();
}
}
});