Android的位图内存问题 - 错误:内存在8294416字节分配 [英] Android Bitmap Memory Issue - Error: Out of memory on a 8294416-byte allocation

查看:510
本文介绍了Android的位图内存问题 - 错误:内存在8294416字节分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个应用程序,经过一个故事。故事包含了场景,它包含通过ImageViews显示多个JPEG和PNG文件。我创建了ImageView的,并在其通过以下功能添加到布局:

 私人ImageView的newImage(展中展)
{
    ImageView的IV =新ImageView的(这一点);
    字符串文件路径= comin.generateFilePath(显示);
    Log.i(TAG,newImage,文件路径=+文件路径+ID =+ show.id);
    WeakReference的<位图> BMP =新的WeakReference<位图>(scaleBitmap(文件路径)); //增加13年4月1日
    // BMP位= scaleBitmap(文件路径);
    Log.i(TAG,newImage,宽度=+ bmp.get()的getWidth()+,高度=+ bmp.get()的getHeight());
    iv.setImageBitmap(bmp.get());
    iv.setScaleType(ImageView.ScaleType.FIT_XY);
    iv.setId(show.id);

    //设置可见
    如果(show.visible)
        iv.setVisibility(View.VISIBLE);
    其他
        iv.setVisibility(View.INVISIBLE);

    //组维度
    INT宽度=(app.getWidth()* show.dimX)/ 100;
    INT身高=(app.getHeight()* show.dimY)/ 100;
    RelativeLayout.LayoutParams RLP =新RelativeLayout.LayoutParams(宽,高);
    iv.setLayoutParams(RLP);

    返回IV;
}
 

在现场结束后,我再清除所有除了一个它没有形象,只会侦听触摸事件。在ImageViews的

 私人无效clearScene(布尔clearTouch)
{
    RelativeLayout的父=(RelativeLayout的)findViewById(R.id.relative_layout);
    logMemory(clearScene,START);

    诠释计数= parent.getChildCount();
    对(INT一个计数=  -  1 a取代; = 0; A--)
    {
        INT的id = parent.getChildAt(一).getId();
        字符串消息=clearScene ID =+编号+,数=+数+,A =+ A;
        如果(ID == TOUCH_VIEW_ID和放大器;&安培;!clearTouch)
        {
            消息=信息+,忽略了触摸屏的意见;
        }
        其他
        {
            unbindView(parent.getChildAt(一));
            parent.getChildAt(一).clearAnimation();
            parent.removeView(parent.getChildAt(一));
            消息=信息+,unbindView,clearAnimation,removeView;
        }
        Log.i(TAG,消息);
        logMemory(clearScene,清零,查看);
    }

    handler.removeCallbacksAndMessages(空); //阻止任何未来的触发计划任务
    logMemory(clearScene,前拾荒者);
    System.gc()的;
    logMemory(clearScene,拾荒者后);
}
 

正如你所看到的,我所说的unbindView()方法,尝试并清除任何类型的可能的内存泄漏。

 私人无效unbindView(查看视图)
{
    字符串消息=unbindView,ID =+ view.getId();
    尝试{view.setOnClickListener(NULL);}赶上(的Throwable mayHappen){};
    尝试{view.setOnCreateContextMenuListener(NULL);}赶上(的Throwable mayHappen){};
    尝试{view.setOnFocusChangeListener(NULL);}赶上(的Throwable mayHappen){};
    尝试{view.setOnKeyListener(NULL);}赶上(的Throwable mayHappen){};
    尝试{view.setOnLongClickListener(NULL);}赶上(的Throwable mayHappen){};
    尝试{view.setOnClickListener(NULL);}赶上(的Throwable mayHappen){};

    如果(view.getBackground()!= NULL)
    {
        消息=信息+,setBackgroundCallback(空);
        view.getBackground()setCallback(空)。
    }

    如果(查看的instanceof ImageView的)
    {
        如果(((ImageView的)视图).getDrawable()!= NULL)
        {
            如果(((ImageView的)视图).getDrawable()的instanceof BitmapDrawable)
            {
                消息=信息+,回收位图;
                。((BitmapDrawable)((ImageView的)视图).getDrawable())getBitmap()循环();
            }
            消息=信息+,setDrawableCallback(空);
            ((ImageView的)视图).getDrawable()setCallback(空)。
        }
        消息=信息+,setImageBitmap(空),setBackgroundDrawable(空);
        ((ImageView的)视图).setImageBitmap(空);
        ((ImageView的)视图).setBackgroundDrawable(空);
    }
    Log.i(TAG,消息);
}
 

一旦所有的ImageViews是无界的,然后我跑了clearScene功能的垃圾收集器。

*** EDIT(13年4月2日)确定,一些研究和反复试验后,我改变了我的记忆记录。以下是相关的方法:

在onResume(),我设置一个布尔为true取决于Android的版本(以确定其是否使用本机或者Java内存位图),然后我初始化为0旧内存值默认值;

  @覆盖
保护无效onResume()
{
    super.onResume();

    如果(android.os.Build.VERSION.SDK_INT> = 11)debugJavaMemory = TRUE;
    否则debugNativeMemory = TRUE;
    availableJavaMemoryOld = 0;
    availableNativeMemoryOld = 0;
    dialogDebug();
}
 

这是我的记录功能。它记录根据目前Android版本是否是使用本地或Java的内存来存储位图。

 私人无效logMemory(字符串callingFunction)
{
    如果(debugJavaMemory)
    {
        长最大值=调用Runtime.getRuntime()maxMemory()/ 1024。
        长期使用=调用Runtime.getRuntime()totalMemory()/ 1024。
        长期用=最大值 - 使用;
        长变化=可用 -  availableJavaMemoryOld;
        如果(availableJavaMemoryOld!= 0)
            Log.i(TAG_MEMORY,jMEM男:+ MAX +,U+使用+,答:+提供+C:+改变+,+ callingFunction);
        availableJavaMemoryOld =可用;
    }
    否则,如果(debugNativeMemory)
    {
        长最大值= Debug.getNativeHeapSize()/ 1024;
        长期使用= Debug.getNativeHeapAllocatedSize()/ 1024;
        长期用=最大值 - 使用;
        长变化=可用 -  availableNativeMemoryOld;
        如果(availableNativeMemoryOld!= 0)
            Log.i(TAG_MEMORY,NMEM男:+ MAX +,U+使用+,答:+提供+C:+改变+,+ callingFunction);
        availableNativeMemoryOld =可用;
    }
}
 

如果你看我的日志我从运行Android 10仿真器(2.3.3)收集,垃圾收集器似乎是工作完美!我发现很奇怪,唯一的事情是,马克斯本机内存可以动态增加!?

注:M:(最大内存),U(使用的内存),A:(可用内存),C(可用内存的变化),函数调用日志功能

  2月4日至3号:34:20.758:I / MEMORY(1045):NMEM M:18336,U:13287,A:5049,C:-2549,loadScene,增加的图像B:0,S:0,ID:0
2月4日至3日:34:21.267:I / MEMORY(1045):NMEM M:21568,U:21391,答:177,C:-4872,loadScene,增加的图像B:0,S:0,ID:1
2月4日至3日:34:35.227:I / MEMORY(1045):NMEM M:21568,U:13288,A:8280,C:8103,clearScene,清零,查看ID:1
2月4日至3日:34:35.267:I / MEMORY(1045):NMEM M:21568,U:5184,答:16384,C:8104,clearScene,清零,查看ID:0
2月4日至3日:34:35.327:I / MEMORY(1045):NMEM M:21568,U:4977,答:16591,C:207,clearScene,拾荒者后
2月4日至3日:34:36.067:I / MEMORY(1045):NMEM M:21568,U:13082,A:8486,C:-8105,loadScene,增加的图像B:1,S:0,ID:0
2月4日至3日:34:47.177:I / MEMORY(1045):NMEM M:21568,U:4979,答:16589,C:8103,clearScene,清零,查看ID:0
2月4日至3日:34:47.277:I / MEMORY(1045):NMEM M:21568,U:4978,答:16590,C:1,clearScene,拾荒者后
2月4日至3日:34:47.997:I / MEMORY(1045):NMEM M:21568,U:13082,A:8486,C:-8104,loadScene,增加的图像B:1,S:1,ID:0
2月4日至3日:34:49.307:I / MEMORY(1045):NMEM M:26224,U:21186,A:5038,C:-3448,loadScene,增加的图像B:1,S:1,ID:1
2月4日至3日:34:50.597:I / MEMORY(1045):NMEM M:34328,U:29290,A:5038,C:0,loadScene,增加的图像B:1,S:1,ID:2
2月4日至3日:34:51.839:I / MEMORY(1045):NMEM M:42432,U:37395,A:5037,C:-1,loadScene,增加的图像B:1,S:1,ID:3
2月4日至3日:35:00.3​​77:I / MEMORY(1045):NMEM M:42432,U:29293,答:13139,C:8102,clearScene,已清除视图id:3
2月4日至3日:35:00.3​​87:I / MEMORY(1045):NMEM M:42432,U:21189,答:21243,C:8104,clearScene,清零,查看ID:2
2月4日至3日:35:00.3​​97:I / MEMORY(1045):NMEM M:42432,U:13085,答:29347,C:8104,clearScene,清零,查看ID:1
2月4日至3日:35:00.407:I / MEMORY(1045):NMEM M:42432,U:4982,答:37450,C:8103,clearScene,清零,查看ID:0
2月4日至3日:35:00.449:I / MEMORY(1045):NMEM M:42432,U:4978,答:37454,C:4,clearScene,拾荒者后
2月4日至3日:35:01.217:I / MEMORY(1045):NMEM M:42432,U:13083,答:29349,C:-8105,loadScene,增加的图像B:1,S:2,ID:0
2月4日至3日:35:23.458:I / MEMORY(1045):NMEM M:42432,U:4979,答:37453,C:8104,clearScene,清零,查看ID:0
2月4日至3日:35:23.508:I / MEMORY(1045):NMEM M:42432,U:4978,答:37454,C:1,clearScene,拾荒者后
2月4日至3日:35:24.777:I / MEMORY(1045):NMEM M:42432,U:13082,答:29350,C:-8104,loadScene,增加的图像B:1,S:3,ID:0
2月4日至3日:35:25.719:I / MEMORY(1045):NMEM M:42432,U:21186,答:21246,C:-8104,loadScene,增加的图像B:1,S:3,ID:1
2月4日至3日:35:26.457:I / MEMORY(1045):NMEM M:42432,U:29291,答:13141,C:-8105,loadScene,增加的图像B:1,S:3,ID:2
2月4日至3日:35:55.008:I / MEMORY(1045):NMEM M:42432,U:21187,答:21245,C:8104,clearScene,清零,查看ID:2
2月4日至3日:35:55.027:I / MEMORY(1045):NMEM M:42432,U:13083,答:29349,C:8104,clearScene,清零,查看ID:1
2月4日至3日:35:55.037:I / MEMORY(1045):NMEM M:42432,U:4979,答:37453,C:8104,clearScene,清零,查看ID:0
2月4日至3日:35:55.077:I / MEMORY(1045):NMEM M:42432,U:4978,答:37454,C:1,clearScene,拾荒者后
2月4日至3日:35:55.829:I / MEMORY(1045):NMEM M:42432,U:13083,答:29349,C:-8105,loadScene,增加的图像B:1,S:4,ID:0
2月4日至3日:35:56.427:I / MEMORY(1045):NMEM M:42432,U:21187,答:21245,C:-8104,loadScene,增加的图像B:1,S:4,ID:1
2月4日至3日:36:00.257:I / MEMORY(1045):NMEM M:42432,U:13083,答:29349,C:8104,clearScene,清零,查看ID:1
2月4日至3日:36:00.278:I / MEMORY(1045):NMEM M:42432,U:4979,答:37453,C:8104,clearScene,清零,查看ID:0
2月4日至3日:36:00.3​​18:I / MEMORY(1045):NMEM M:42432,U:4978,答:37454,C:1,clearScene,拾荒者后
2月4日至3日:36:01.687:I / MEMORY(1045):NMEM M:42432,U:13083,答:29349,C:-8105,loadScene,增加的图像B:1,S:5,ID:0
2月4日至3日:36:06.437:I / MEMORY(1045):NMEM M:42432,U:4979,答:37453,C:8104,clearScene,清零,查看ID:0
2月4日至3日:36:06.487:I / MEMORY(1045):NMEM M:42432,U:4978,答:37454,C:1,clearScene,拾荒者后
2月4日至3日:36:07.777:I / MEMORY(1045):NMEM M:42432,U:13082,答:29350,C:-8104,loadScene,增加的图像B:1,S:6,ID:0
2月4日至3日:36:09.297:I / MEMORY(1045):NMEM M:42432,U:4978,答:37454,C:8104,clearScene,清零,查看ID:0
2月4日至3日:36:09.337:I / MEMORY(1045):NMEM M:42432,U:4978,答:37454,C:0,clearScene,拾荒者后
2月4日至3日:36:10.727:I / MEMORY(1045):NMEM M:42432,U:13082,答:29350,C:-8104,loadScene,增加的图像B:1,S:7,ID:0
2月4日至3日:36:20.947:I / MEMORY(1045):NMEM M:42432,U:4978,答:37454,C:8104,clearScene,清零,查看ID:0
2月4日至3日:36:20.987:I / MEMORY(1045):NMEM M:42432,U:4978,答:37454,C:0,clearScene,拾荒者后
2月4日至3日:36:22.367:I / MEMORY(1045):NMEM M:42432,U:13082,答:29350,C:-8104,loadScene,增加的图像B:1,S:8,ID:0
2月4日至3日:36:23.317:I / MEMORY(1045):NMEM M:42432,U:21187,答:21245,C:-8105,loadScene,增加的图像B:1,S:8,ID:1
2月4日至3日:36:23.887:I / MEMORY(1045):NMEM M:42432,U:29291,答:13141,C:-8104,loadScene,增加的图像B:1,S:8,ID:2
2月4日至3日:36:26.507:I / MEMORY(1045):NMEM M:42432,U:21187,答:21245,C:8104,clearScene,清零,查看ID:2
2月4日至3日:36:26.517:I / MEMORY(1045):NMEM M:42432,U:13083,答:29349,C:8104,clearScene,清零,查看ID:1
2月4日至3日:36:26.527:I / MEMORY(1045):NMEM M:42432,U:4979,答:37453,C:8104,clearScene,清零,查看ID:0
2月4日至3日:36:26.577:I / MEMORY(1045):NMEM M:42432,U:4978,答:37454,C:1,clearScene,拾荒者后
2月4日至3日:36:27.797:I / MEMORY(1045):NMEM M:42432,U:13083,答:29349,C:-8105,loadScene,增加的图像B:1,S:9,ID:0
2月4日至3日:36:29.248:I / MEMORY(1045):NMEM M:42432,U:4979,答:37453,C:8104,clearScene,清零,查看ID:0
2月4日至3日:36:29.287:I / MEMORY(1045):NMEM M:42432,U:4978,答:37454,C:1,clearScene,拾荒者后
2月4日至3日:36:30.547:I / MEMORY(1045):NMEM M:42432,U:13082,答:29350,C:-8104,loadScene,增加的图像B:1,S:10,ID:0
2月4日至3日:36:32.177:I / MEMORY(1045):NMEM M:42432,U:4978,答:37454,C:8104,clearScene,清零,查看ID:0
2月4日至3日:36:32.228:I / MEMORY(1045):NMEM M:42432,U:4978,答:37454,C:0,clearScene,拾荒者后
2月4日至3日:36:33.467:I / MEMORY(1045):NMEM M:42432,U:13082,答:29350,C:-8104,loadScene,增加的图像B:1,S:11,ID:0
2月4日至3日:36:33.977:I / MEMORY(1045):NMEM M:42432,U:21187,答:21245,C:-8105,loadScene,增加的图像B:1,S:11,ID:1
2月4日至3日:36:34.527:I / MEMORY(1045):NMEM M:42432,U:29291,答:13141,C:-8104,loadScene,增加的图像B:1,S:11,ID:2
2月4日至3日:36:35.058:I / MEMORY(1045):NMEM M:42432,U:37395,A:5037,C:-8104,loadScene,增加的图像B:1,S:11,ID:3
2月4日至3日:36:41.260:I / MEMORY(1045):NMEM M:42432,U:29291,答:13141,C:8104,clearScene,已清除视图id:3
2月4日至3日:36:41.267:I / MEMORY(1045):NMEM M:42432,U:21187,答:21245,C:8104,clearScene,清零,查看ID:2
2月4日至3日:36:41.278:I / MEMORY(1045):NMEM M:42432,U:13083,答:29349,C:8104,clearScene,清零,查看ID:1
2月4日至3日:36:41.287:I / MEMORY(1045):NMEM M:42432,U:4979,答:37453,C:8104,clearScene,清零,查看ID:0
2月4日至3日:36:41.337:I / MEMORY(1045):NMEM M:42432,U:4979,答:37453,C:0,clearScene,拾荒者后
2月4日至3日:36:42.618:I / MEMORY(1045):NMEM M:42432,U:13083,答:29349,C:-8104,loadScene补充图像b:2,S:0,ID:0
2月4日至3日:37:00.550:I / MEMORY(1045):NMEM M:42432,U:4979,答:37453,C:8104,clearScene,清零,查看ID:0
2月4日至3日:37:00.587:I / MEMORY(1045):NMEM M:42432,U:4979,答:37453,C:0,clearScene,拾荒者后
2月4日至3日:37:01.817:I / MEMORY(1045):NMEM M:42432,U:13083,答:29349,C:-8104,loadScene补充图像b:2,S:1,ID:0
2月4日至3日:37:03.347:I / MEMORY(1045):NMEM M:42432,U:4979,答:37453,C:8104,clearScene,清零,查看ID:0
2月4日至3日:37:03.397:I / MEMORY(1045):NMEM M:42432,U:4979,答:37453,C:0,clearScene,拾荒者后
2月4日至3日:37:04.607:I / MEMORY(1045):NMEM M:42432,U:13083,答:29349,C:-8104,loadScene补充图像b:2,S:2,ID:0
2月4日至3日:37:05.258:I / MEMORY(1045):NMEM M:42432,U:21187,答:21245,C:-8104,loadScene补充图像b:2,S:2,ID:1
2月4日至3日:37:07.677:I / MEMORY(1045):NMEM M:42432,U:13083,答:29349,C:8104,clearScene,清零,查看ID:1
2月4日至3日:37:07.687:I / MEMORY(1045):NMEM M:42432,U:4979,答:37453,C:8104,clearScene,清零,查看ID:0
2月4日至3日:37:07.737:I / MEMORY(1045):NMEM M:42432,U:4979,答:37453,C:0,clearScene,拾荒者后
2月4日至3日:37:09.017:I / MEMORY(1045):NMEM M:42432,U:13083,答:29349,C:-8104,loadScene补充图像b:2,S:3,ID:0
2月4日至3日:37:10.327:I / MEMORY(1045):NMEM M:42432,U:21187,答:21245,C:-8104,loadScene补充图像b:2,S:3,ID:1
2月4日至3日:37:13.330:I / MEMORY(1045):NMEM M:42432,U:13083,答:29349,C:8104,clearScene,清零,查看ID:1
2月4日至3日:37:13.337:I / MEMORY(1045):NMEM M:42432,U:4979,答:37453,C:8104,clearScene,清零,查看ID:0
2月4日至3日:37:13.387:I / MEMORY(1045):NMEM M:42432,U:4979,答:37453,C:0,clearScene,拾荒者后
2月4日至3日:37:14.697:I / MEMORY(1045):NMEM M:42432,U:13083,答:29349,C:-8104,loadScene补充图像b:2,S:4,ID:0
2月4日至3日:37:16.037:I / MEMORY(1045):NMEM M:42432,U:21187,答:21245,C:-8104,loadScene补充图像b:2,S:4,ID:1
2月4日至3日:37:17.367:I / MEMORY(1045):NMEM M:42432,U:29292,答:13140,C:-8105,loadScene补充图像b:2,S:4,ID:2
2月4日至3日:37:19.087:I / MEMORY(1045):NMEM M:42432,U:37396,A:5036,C:-8104,loadScene补充图像b:2,S:4,ID:3
2月4日至3日:37:44.347:I / MEMORY(1045):NMEM M:42432,U:29293,答:13139,C:8103,clearScene,已清除视图id:3
2月4日至3日:37:44.357:I / MEMORY(1045):NMEM M:42432,U:21189,答:21243,C:8104,clearScene,清零,查看ID:2
2月4日至3日:37:44.367:I / MEMORY(1045):NMEM M:42432,U:13085,答:29347,C:8104,clearScene,清零,查看ID:1
2月4日至3日:37:44.377:I / MEMORY(1045):NMEM M:42432,U:4981,答:37451,C:8104,clearScene,清零,查看ID:0
2月4日至3日:37:44.437:I / MEMORY(1045):NMEM M:42432,U:4979,答:37453,C:2,clearScene,拾荒者后
2月4日至3日:37:45.667:I / MEMORY(1045):NMEM M:42432,U:13083,答:29349,C:-8104,loadScene补充图像b:2,S:5,ID:0
2月4日至3日:37:46.917:I / MEMORY(1045):NMEM M:42432,U:21188,答:21244,C:-8105,loadScene补充图像b:2,S:5,ID:1
2月4日至3日:37:48.167:I / MEMORY(1045):NMEM M:42432,U:29292,答:13140,C:-8104,loadScene补充图像b:2,S:5,ID:2
2月4日至3日:37:49.807:I / MEMORY(1045):NMEM M:42432,U:37396,A:5036,C:-8104,loadScene补充图像b:2,S:5,ID:3
2月4日至3日:38:08.239:I / MEMORY(1045):NMEM M:42432,U:29293,答:13139,C:8103,clearScene,已清除视图id:3
2月4日至3日:38:08.258:I / MEMORY(1045):NMEM M:42432,U:21189,答:21243,C:8104,clearScene,清零,查看ID:2
2月4日至3日:38:08.267:I / MEMORY(1045):NMEM M:42432,U:13086,答:29346,C:8103,clearScene,清零,查看ID:1
2月4日至3日:38:08.277:I / MEMORY(1045):NMEM M:42432,U:4982,答:37450,C:8104,clearScene,清零,查看ID:0
2月4日至3日:38:08.317:I / MEMORY(1045):NMEM M:42432,U:4979,答:37453,C:3,clearScene,拾荒者后
2月4日至3日:38:09.527:I / MEMORY(1045):NMEM M:42432,U:13083,答:29349,C:-8104,loadScene补充图像b:2,S:6,ID:0
 

如前所述,存储器被清洗后的ImageView的是未结合的<!/ P>

下面是我从我破碎的Nexus 7运行Android 17(4.2)收集的日志。这真的很难理解如何内存被用在这里。这是从来没有一致的!有时,当我加载ImageView的内存不分配。有时,当我打开一个新的ImageView内存才会被清除。

注:M:(最大内存),U(使用的内存),A:(可用内存),C(可用内存的变化),函数调用日志功能

  04-02 22:37:05.866:I / MEMORY(15827):jMEM M:65536,U:21300,答:44236,C:0,loadScene,新增图像B :0,S:0,ID:0
04-02 22:37:06.136:I / MEMORY(15827):jMEM M:65536,U:29444,答:36092,C:-8144,loadScene,增加的图像B:0,S:0,ID:1
04-02 22:37:19.806:I / MEMORY(15827):jMEM M:65536,U:29432,答:36104,C:12,clearScene,清零,查看ID:1
04-02 22:37:19.806:I / MEMORY(15827):jMEM M:65536,U:29432,答:36104,C:0,clearScene,清零,查看ID:0
04-02 22:37:19.836:I / MEMORY(15827):jMEM M:65536,U:29432,答:36104,C:0,clearScene,拾荒者后
04-02 22:37:20.116:I / MEMORY(15827):jMEM M:65536,U:37536,答:28000,C:-8104,loadScene,增加的图像B:1,S:0,ID:0
04-02 22:37:28.456:I / MEMORY(15827):jMEM M:65536,U:37532,答:28004,C:4,clearScene,清零,查看ID:0
04-02 22:37:28.486:I / MEMORY(15827):jMEM M:65536,U:37532,答:28004,C:0,clearScene,拾荒者后
04-02 22:37:28.756:I / MEMORY(15827):jMEM M:65536,U:37532,答:28004,C:0,loadScene,增加的图像B:1,S:1,ID:0
04-02 22:37:29.226:I / MEMORY(15827):jMEM M:65536,U:37532,答:28004,C:0,loadScene,增加的图像B:1,S:1,ID:1
04-02 22:37:29.706:I / MEMORY(15827):jMEM M:65536,U:45636,答:19900,C:-8104,loadScene,增加的图像B:1,S:1,ID:2
04-02 22:37:30.156:I / MEMORY(15827):jMEM M:65536,U:53740,答:11796,C:-8104,loadScene,增加的图像B:1,S:1,ID:3
04-02 22:37:38.676:I / MEMORY(15827):jMEM M:65536,U:53732,答:11804,C:8,clearScene,已清除视图id:3
04-02 22:37:38.676:I / MEMORY(15827):jMEM M:65536,U:53732,答:11804,C:0,clearScene,清零,查看ID:2
04-02 22:37:38.676:I / MEMORY(15827):jMEM M:65536,U:53732,答:11804,C:0,clearScene,清零,查看ID:1
04-02 22:37:38.676:I / MEMORY(15827):jMEM M:65536,U:53732,答:11804,C:0,clearScene,清零,查看ID:0
04-02 22:37:38.696:I / MEMORY(15827):jMEM M:65536,U:53732,答:11804,C:0,clearScene,拾荒者后
04-02 22:37:38.966:I / MEMORY(15827):jMEM M:65536,U:53732,答:11804,C:0,loadScene,增加的图像B:1,S:2,ID:0
04-02 22:37:48.156:I / MEMORY(15827):jMEM M:65536,U:53732,答:11804,C:0,clearScene,清零,查看ID:0
04-02 22:37:48.216:I / MEMORY(15827):jMEM M:65536,U:37532,答:28004,C:16200,clearScene,拾荒者后
04-02 22:37:48.676:I / MEMORY(15827):jMEM M:65536,U:37532,答:28004,C:0,loadScene,增加的图像B:1,S:3,ID:0
04-02 22:37:49.256:I / MEMORY(15827):jMEM M:65536,U:37532,答:28004,C:0,loadScene,增加的图像B:1,S:3,ID:1
04-02 22:37:49.716:I / MEMORY(15827):jMEM M:65536,U:45636,答:19900,C:-8104,loadScene,增加的图像B:1,S:3,ID:2
04-02 22:38:07.426:I / MEMORY(15827):jMEM M:65536,U:45632,答:19904,C:4,clearScene,清零,查看ID:2
04-02 22:38:07.426:I / MEMORY(15827):jMEM M:65536,U:45632,答:19904,C:0,clearScene,清零,查看ID:1
04-02 22:38:07.426:I / MEMORY(15827):jMEM M:65536,U:45632,答:19904,C:0,clearScene,清零,查看ID:0
04-02 22:38:07.496:I / MEMORY(15827):jMEM M:65536,U:45632,答:19904,C:0,clearScene,拾荒者后
04-02 22:38:07.766:I / MEMORY(15827):jMEM M:65536,U:45632,答:19904,C:0,loadScene,增加的图像B:1,S:4,ID:0
04-02 22:38:08.096:I / MEMORY(15827):jMEM M:65536,U:53736,答:11800,C:-8104,loadScene,增加的图像B:1,S:4,ID:1
04-02 22:38:10.896:I / MEMORY(15827):jMEM M:65536,U:53736,答:11800,C:0,clearScene,清零,查看ID:1
04-02 22:38:10.896:I / MEMORY(15827):jMEM M:65536,U:53736,答:11800,C:0,clearScene,清零,查看ID:0
04-02 22:38:10.916:I / MEMORY(15827):jMEM M:65536,U:53736,答:11800,C:0,clearScene,拾荒者后
04-02 22:38:11.436:I / MEMORY(15827):jMEM M:65536,U:53736,答:11800,C:0,loadScene,增加的图像B:1,S:5,ID:0
04-02 22:38:19.356:I / MEMORY(15827):jMEM M:65536,U:53732,答:11804,C:4,clearScene,清零,查看ID:0
04-02 22:38:19.386:I / MEMORY(15827):jMEM M:65536,U:45632,答:19904,C:8100,clearScene,拾荒者后
04-02 22:38:19.866:I / MEMORY(15827):jMEM M:65536,U:45632,答:19904,C:0,loadScene,增加的图像B:1,S:6,ID:0
04-02 22:38:21.326:I / MEMORY(15827):jMEM M:65536,U:45632,答:19904,C:0,clearScene,清零,查看ID:0
04-02 22:38:21.356:I / MEMORY(15827):jMEM M:65536,U:21300,答:44236,C:24332,clearScene,拾荒者后
04-02 22:38:21.906:I / MEMORY(15827):jMEM M:65536,U:29404,答:36132,C:-8104,loadScene,增加的图像B:1,S:7,ID:0
04-02 22:38:24.376:I / MEMORY(15827):jMEM M:65536,U:29404,答:36132,C:0,clearScene,清零,查看ID:0
04-02 22:38:24.406:I / MEMORY(15827):jMEM M:65536,U:29404,答:36132,C:0,clearScene,拾荒者后
04-02 22:38:24.916:I / MEMORY(15827):jMEM M:65536,U:29404,答:36132,C:0,loadScene,增加的图像B:1,S:8,ID:0
04-02 22:38:25.496:I / MEMORY(15827):jMEM M:65536,U:37548,答:27988,C:-8144,loadScene,增加的图像B:1,S:8,ID:1
04-02 22:38:25.796:I / MEMORY(15827):jMEM M:65536,U:45652,答:19884,C:-8104,loadScene,增加的图像B:1,S:8,ID:2
04-02 22:38:28.356:I / MEMORY(15827):jMEM M:65536,U:45652,答:19884,C:0,clearScene,清零,查看ID:2
04-02 22:38:28.356:I / MEMORY(15827):jMEM M:65536,U:45652,答:19884,C:0,clearScene,清零,查看ID:1
04-02 22:38:28.356:I / MEMORY(15827):jMEM M:65536,U:45652,答:19884,C:0,clearScene,清零,查看ID:0
04-02 22:38:28.376:I / MEMORY(15827):jMEM M:65536,U:45652,答:19884,C:0,clearScene,拾荒者后
04-02 22:38:28.846:I / MEMORY(15827):jMEM M:65536,U:45652,答:19884,C:0,loadScene,增加的图像B:1,S:9,ID:0
04-02 22:38:29.926:I / MEMORY(15827):jMEM M:65536,U:45652,答:19884,C:0,clearScene,清零,查看ID:0
04-02 22:38:29.956:I / MEMORY(15827):jMEM M:65536,U:29400,答:36136,C:16252,clearScene,拾荒者后
04-02 22:38:30.416:I / MEMORY(15827):jMEM M:65536,U:29400,答:36136,C:0,loadScene,增加的图像B:1,S:10,ID:0
04-02 22:38:31.446:I / MEMORY(15827):jMEM M:65536,U:29400,答:36136,C:0,clearScene,清零,查看ID:0
04-02 22:38:31.476:I / MEMORY(15827):jMEM M:65536,U:21300,答:44236,C:8100,clearScene,拾荒者后
04-02 22:38:31.966:I / MEMORY(15827):jMEM M:65536,U:29404,答:36132,C:-8104,loadScene,增加的图像B:1,S:11,ID:0
04-02 22:38:32.226:I / MEMORY(15827):jMEM M:65536,U:37548,答:27988,C:-8144,loadScene,增加的图像B:1,S:11,ID:1
04-02 22:38:32.526:I / MEMORY(15827):jMEM M:65536,U:45652,答:19884,C:-8104,loadScene,增加的图像B:1,S:11,ID:2
04-02 22:38:32.816:I / MEMORY(15827):jMEM M:65536,U:53756,答:11780,C:-8104,loadScene,增加的图像B:1,S:11,ID:3
04-02 22:38:34.396:I / MEMORY(15827):jMEM M:65536,U:53756,答:11780,C:0,clearScene,已清除视图id:3
04-02 22:38:34.396:I / MEMORY(15827):jMEM M:65536,U:53756,答:11780,C:0,clearScene,清零,查看ID:2
04-02 22:38:34.396:I / MEMORY(15827):jMEM M:65536,U:53756,答:11780,C:0,clearScene,清零,查看ID:1
04-02 22:38:34.396:I / MEMORY(15827):jMEM M:65536,U:53756,答:11780,C:0,clearScene,清零,查看ID:0
04-02 22:38:34.426:I / MEMORY(15827):jMEM M:65536,U:53756,答:11780,C:0,clearScene,拾荒者后
04-02 22:38:34.886:I / MEMORY(15827):jMEM M:65536,U:53756,答:11780,C:0,loadScene补充图像b:2,S:0,ID:0
04-02 22:38:35.776:I / MEMORY(15827):jMEM M:65536,U:53756,答:11780,C:0,clearScene,清零,查看ID:0
04-02 22:38:35.816:I / MEMORY(15827):jMEM M:65536,U:21300,答:44236,C:32456,clearScene,拾荒者后
04-02 22:38:36.256:I / MEMORY(15827):jMEM M:65536,U:29404,答:36132,C:-8104,loadScene补充图像b:2,S:1,ID:0
04-02 22:38:36.976:I / MEMORY(15827):jMEM M:65536,U:29404,答:36132,C:0,clearScene,清零,查看ID:0
04-02 22:38:37.006:I / MEMORY(15827):jMEM M:65536,U:29404,答:36132,C:0,clearScene,拾荒者后
04-02 22:38:37.436:I / MEMORY(15827):jMEM M:65536,U:29404,答:36132,C:0,loadScene补充图像b:2,S:2,ID:0
04-02 22:38:37.816:I / MEMORY(15827):jMEM M:65536,U:37548,答:27988,C:-8144,loadScene补充图像b:2,S:2,ID:1
04-02 22:38:44.986:I / MEMORY(15827):jMEM M:65536,U:37532,答:28004,C:16,clearScene,清零,查看ID:1
04-02 22:38:44.986:I / MEMORY(15827):jMEM M:65536,U:37532,答:28004,C:0,clearScene,清零,查看ID:0
04-02 22:38:45.016:I / MEMORY(15827):jMEM M:65536,U:37532,答:28004,C:0,clearScene,拾荒者后
04-02 22:38:45.496:I / MEMORY(15827):jMEM M:65536,U:37532,答:28004,C:0,loadScene补充图像b:2,S:3,ID:0
04-02 22:38:45.996:I / MEMORY(15827):jMEM M:65536,U:45636,答:19900,C:-8104,loadScene补充图像b:2,S:3,ID:1
04-02 22:38:48.306:I / MEMORY(15827):jMEM M:65536,U:45636,答:19900,C:0,clearScene,清零,查看ID:1
04-02 22:38:48.306:I / MEMORY(15827):jMEM M:65536,U:45636,答:19900,C:0,clearScene,清零,查看ID:0
04-02 22:38:48.336:I / MEMORY(15827):jMEM M:65536,U:45636,答:19900,C:0,clearScene,拾荒者后
04-02 22:38:48.846:I / MEMORY(15827):jMEM M:65536,U:45636,答:19900,C:0,loadScene补充图像b:2,S:4,ID:0
04-02 22:38:49.326:I / MEMORY(15827):jMEM M:65536,U:45636,答:19900,C:0,loadScene补充图像b:2,S:4,ID:1
04-02 22:38:49.856:I / MEMORY(15827):jMEM M:65536,U:53740,答:11796,C:-8104,loadScene补充图像b:2,S:4,ID:2
04-02 22:38:50.386:I / MEMORY(15827):jMEM M:65536,U:61844,A:3692,C:-8104,loadScene补充图像b:2,S:4,ID:3
04-02 22:38:59.486:I / MEMORY(15827):jMEM M:65536,U:61832,A:3704,C:12,clearScene,已清除视图id:3
04-02 22:38:59.486:I / MEMORY(15827):jMEM M:65536,U:61832,A:3704,C:0,clearScene,清零,查看ID:2
04-02 22:38:59.486:I / MEMORY(15827):jMEM M:65536,U:61832,A:3704,C:0,clearScene,清零,查看ID:1
04-02 22:38:59.486:I / MEMORY(15827):jMEM M:65536,U:61832,A:3704,C:0,clearScene,清零,查看ID:0
04-02 22:38:59.516:I / MEMORY(15827):jMEM M:65536,U:61832,A:3704,C:0,clearScene,拾荒者后
04-02 22:38:59.936:I / MEMORY(15827):jMEM M:65536,U:61832,A:3704,C:0,loadScene补充图像b:2,S:5,ID:0
04-02 22:39:00.3​​56:I / MEMORY(15827):jMEM M:65536,U:61832,A:3704,C:0,loadScene补充图像b:2,S:5,ID:1
 

我已经通过了显示位图有效地训练
http://developer.android.com/training/displaying-bitmaps/index.html

我也一直通过以下计算器的问题:
回收的ImageView位图
<一href="http://stackoverflow.com/questions/8349958/imageview-automatically-recycle-bitmap-if-imageview-is-not-visible-within-scro">ImageView:自动循环位,如果ImageView的不(在滚动型)
可见 <一href="http://stackoverflow.com/questions/5697760/android-out-of-memory-exception-when-creating-bitmap">android - 创建位图
当内存不够的异常 位图德codeStream内存溢出的异常
检测应用程序堆大小的机器人
<一href="http://stackoverflow.com/questions/6541624/android-createbitmap-oom-when-freememory-bitmapsize-nativefreeheap-bi">android createBitmap OOM时((freeMemory&GT; bitmapSize)及及(nativeFreeHeap&LT;位图大小))
<一href="http://stackoverflow.com/questions/11357483/android-495kb-image-taking-10mb-of-heap-size">android堆大小
495KB图像拍摄10MB 的Andr​​oid ImageView的setImageBitmap
Android的内存泄漏解决方案
Android的自定义视图的位图的内存泄漏

和整个网络上很多很多的文章!

要澄清,这似乎只发生在我的Nexus 7。我没有经历过在我的仿真器或我的Galaxy Nexus的问题。我想这可能是因为,我缩放图像的事实。

请帮我检测,其中的内存泄漏!

*** EDIT(13年4月2日),请帮我找出为什么运行模拟器日志Android的10(2.3.3)显示系统分配内存和垃圾清除,然后在完全像它应该和从我的Nexus 7运行Android 17(4.2)日志显示系统随机显然分配内存和垃圾收集清理内存?

解决方案

我无法确定为什么内存分配和收集不同的平台之间的不同,但我还是能够缓解这个问题,prevent我从应用程序通过缩放图像,以适应可用内存崩溃在任何平台上。

此第一函数确定在何处的位图存储器被存储(天然或Java),然后计算剩余的可用的存储器(单位为KB)。

 专用长calcAvailableMemory()
{
    长值=调用Runtime.getRuntime()maxMemory()。
    字符串类型=;
    如果(android.os.Build.VERSION.SDK_INT&GT; = 11)
    {
        值=值/ 1024) - (调用Runtime.getRuntime()totalMemory()/ 1024)。
        TYPE =JAVA;
    }
    其他
    {
        值=值/ 1024) - (Debug.getNativeHeapAllocatedSize()/ 1024);
        TYPE =本土;
    }
    Log.i(TAG,calcAvailableMemory,大小=+价值+,键入=+型);
    返回值;
}
 

这第二个函数创建一个ImageView的附加到布局。显示对象包含不同的变量我需要初始化ImageView的如尺寸和文件路径等长maxImageMemory从第一函数导出,并随后通过意见我附着到布局给我最大数量的数量除以内存分配每个位图。

 私人ImageView的newImage(展中展,长maxImageMemory)
{
    ImageView的IV =新ImageView的(这一点);
    字符串文件路径= comin.generateFilePath(显示);
    BMP位= scaleBitmap(文件路径,maxImageMemory);
    Log.i(TAG,newImage,编号:+ show.id +,文件:+文件路径+中,x:+ bmp.getWidth()+,Y:+ bmp.getHeight());
    iv.setImageBitmap(BMP);
    iv.setScaleType(ImageView.ScaleType.FIT_XY);
    iv.setId(show.id);

    //设置可见
    如果(show.visible)
        iv.setVisibility(View.VISIBLE);
    其他
        iv.setVisibility(View.INVISIBLE);

    //组维度
    INT宽度=(app.getWidth()* show.dimX)/ 100;
    INT身高=(app.getHeight()* show.dimY)/ 100;
    RelativeLayout.LayoutParams RLP =新RelativeLayout.LayoutParams(宽,高);
    iv.setLayoutParams(RLP);

    返回IV;
}
 

此第三功能扩展将要装入的ImageView在功能2. while循环是重要的部分所创建的位图,即其中I增加样本量,直到所需要的存储器的位图是小于最大可用内存。

 私人位图scaleBitmap(字符串路径,长maxImageMemory)
{
    BitmapFactory.Options选项=新BitmapFactory.Options();
    options.inJustDe codeBounds = TRUE;
    BitmapFactory.de codeFILE(路径选择);

    INT样本= 1;
    //缩放图像的屏幕分辨率
    如果(options.outHeight&GT; app.getHeight()|| options.outWidth&GT; app.getWidth())
    {
        INT heightRatio = Math.round((浮点)options.outHeight /(浮点)app.getHeight());
        INT widthRatio = Math.round((浮点)options.outWidth /(浮点)app.getWidth());
        样品= heightRatio&LT; widthRatio? heightRatio:widthRatio;
    }

    //缩放图像以留在内存的限制
    而(calcBitmapSize(options.outWidth,options.outHeight,样品)GT; maxImageMemory)
    {
        样品++;
    }

    options.inSampleSize =样品;
    options.inPurgeable = TRUE;
    options.inInputShareable = TRUE;
    options.inJustDe codeBounds = FALSE;

    Log.i(TAG,scaleBitmap,scaleSample =+样品);
    返回BitmapFactory.de codeFILE(路径选择);
}
 

此第四函数计算存储器通过根据原分辨率的位图,以及所提出的样本大小所需的时间。

 专用长calcBitmapSize(INT宽度,高度INT,INT样品)
{
    长值=((宽/样品)*(高/样品)* 4)/ 1024;
    Log.i(TAG,calcBitmapSize,大小=+值);
    返回值;
}
 

{               {                                                               }          {                                                               } }

I am currently developing an app that goes through a story. The story contains "scenes" which contain multiple JPEG and PNG files that are displayed via ImageViews. I create the ImageView and add at it to the layout via the following function:

private ImageView newImage(Show show)
{
    ImageView iv = new ImageView(this);
    String filePath = comin.generateFilePath(show);
    Log.i(TAG, "newImage, filePath = " + filePath + " id = " + show.id);
    WeakReference<Bitmap> bmp = new WeakReference<Bitmap>(scaleBitmap(filePath)); //added 4/1/13
    //Bitmap bmp = scaleBitmap(filePath);
    Log.i(TAG, "newImage, width = " + bmp.get().getWidth() + ", height = " + bmp.get().getHeight());
    iv.setImageBitmap(bmp.get());
    iv.setScaleType(ImageView.ScaleType.FIT_XY);
    iv.setId(show.id);

    //set visibility
    if (show.visible)
        iv.setVisibility(View.VISIBLE);
    else
        iv.setVisibility(View.INVISIBLE);

    //set dimensions
    int width = (app.getWidth() * show.dimX) / 100;
    int height = (app.getHeight() * show.dimY) / 100;
    RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(width, height);
    iv.setLayoutParams(rlp);

    return iv;
}  

Once the scene is over, I then clear all of the ImageViews except for one which has no image and only serves to listen for touch events.

private void clearScene(boolean clearTouch)
{
    RelativeLayout parent = (RelativeLayout) findViewById(R.id.relative_layout);
    logMemory("clearScene, START");

    int count = parent.getChildCount();
    for (int a = count - 1; a >= 0; a--)
    {
        int id = parent.getChildAt(a).getId();
        String message = "clearScene id = " + id + ", count = " + count + ", a = " + a;
        if (id == TOUCH_VIEW_ID && !clearTouch)
        {
            message = message + ", ignore touch screen view";
        }
        else
        {
            unbindView(parent.getChildAt(a));
            parent.getChildAt(a).clearAnimation();
            parent.removeView(parent.getChildAt(a));
            message = message + ", unbindView, clearAnimation, removeView";
        }
        Log.i(TAG, message);
        logMemory("clearScene, CLEARED VIEW");
    }

    handler.removeCallbacksAndMessages(null);  //stop any future scheduled tasks from triggering
    logMemory("clearScene, BEFORE TRASH COLLECTOR");
    System.gc();
    logMemory("clearScene, AFTER TRASH COLLECTOR");
}

As you can see, I call the unbindView() method, to try and clear any type of possible memory leak.

private void unbindView(View view)
{
    String message = "unbindView, id = " + view.getId();
    try {view.setOnClickListener(null);} catch (Throwable mayHappen) {};
    try {view.setOnCreateContextMenuListener(null);} catch (Throwable mayHappen) {};
    try {view.setOnFocusChangeListener(null);} catch (Throwable mayHappen) {};
    try {view.setOnKeyListener(null);} catch (Throwable mayHappen) {};
    try {view.setOnLongClickListener(null);} catch (Throwable mayHappen) {};
    try {view.setOnClickListener(null);} catch (Throwable mayHappen) {};

    if (view.getBackground() != null)
    {
        message = message + ", setBackgroundCallback(null)";
        view.getBackground().setCallback(null);
    }

    if (view instanceof ImageView)
    {
        if (((ImageView)view).getDrawable() != null)
        {
            if (((ImageView)view).getDrawable() instanceof BitmapDrawable)
            {
                message = message + ", recycle bitmap";
                ((BitmapDrawable)((ImageView)view).getDrawable()).getBitmap().recycle();
            }
            message = message + ", setDrawableCallback(null)";
            ((ImageView)view).getDrawable().setCallback(null);
        }
        message = message + ", setImageBitmap(null), setBackgroundDrawable(null)";
        ((ImageView) view).setImageBitmap(null);
        ((ImageView) view).setBackgroundDrawable(null);
    }
    Log.i(TAG, message);
}

Once all of the ImageViews are unbounded, I then run the garbage collector in the clearScene function.

***EDIT (4/2/13) OK, after some researching and trial and error, I changed up my memory logging. Here are the relevant methods:

In onResume(), I set a Boolean to true depending on the version of Android (to determine if it uses native or Java memory for Bitmaps), and then I initialize default value of 0 for the old memory values;

@Override
protected void onResume() 
{
    super.onResume();

    if (android.os.Build.VERSION.SDK_INT >= 11) debugJavaMemory = true;
    else debugNativeMemory = true;
    availableJavaMemoryOld = 0;
    availableNativeMemoryOld = 0;
    dialogDebug();
}  

This is my logging function. It logs depending on whether the current version of Android is using native or Java memory to store Bitmaps.

private void logMemory(String callingFunction)
{
    if (debugJavaMemory)
    {
        long max = Runtime.getRuntime().maxMemory() / 1024;
        long used = Runtime.getRuntime().totalMemory() / 1024;
        long available = max - used;
        long change = available - availableJavaMemoryOld;
        if (availableJavaMemoryOld != 0)
            Log.i(TAG_MEMORY, "jMEM M:" + max + ", U:" + used + ", A:" + available + ", C:" + change + ", " + callingFunction);
        availableJavaMemoryOld = available;
    }
    else if (debugNativeMemory)
    {
        long max = Debug.getNativeHeapSize() / 1024;
        long used = Debug.getNativeHeapAllocatedSize() / 1024;
        long available = max - used;
        long change = available - availableNativeMemoryOld;
        if (availableNativeMemoryOld != 0)
            Log.i(TAG_MEMORY, "nMEM M:" + max + ", U:" + used + ", A:" + available + ", C:" + change + ", " + callingFunction);
        availableNativeMemoryOld = available;
    }
}  

If you look at my log I collect from an emulator running Android 10 (2.3.3), the garbage collector appears to be working perfectly! The only thing I noticed that was odd, was that the Max Native Memory could increase dynamically!?

Legend: M: (max memory), U: (used memory), A: (available memory), C: (change in available memory), Function that called log function

04-03 02:34:20.758: I/MEMORY(1045): nMEM M:18336, U:13287, A:5049, C:-2549, loadScene, ADDED IMAGE b:0, s:0, id:0
04-03 02:34:21.267: I/MEMORY(1045): nMEM M:21568, U:21391, A:177, C:-4872, loadScene, ADDED IMAGE b:0, s:0, id:1
04-03 02:34:35.227: I/MEMORY(1045): nMEM M:21568, U:13288, A:8280, C:8103, clearScene, CLEARED VIEW id:1
04-03 02:34:35.267: I/MEMORY(1045): nMEM M:21568, U:5184, A:16384, C:8104, clearScene, CLEARED VIEW id:0
04-03 02:34:35.327: I/MEMORY(1045): nMEM M:21568, U:4977, A:16591, C:207, clearScene, AFTER TRASH COLLECTOR
04-03 02:34:36.067: I/MEMORY(1045): nMEM M:21568, U:13082, A:8486, C:-8105, loadScene, ADDED IMAGE b:1, s:0, id:0
04-03 02:34:47.177: I/MEMORY(1045): nMEM M:21568, U:4979, A:16589, C:8103, clearScene, CLEARED VIEW id:0
04-03 02:34:47.277: I/MEMORY(1045): nMEM M:21568, U:4978, A:16590, C:1, clearScene, AFTER TRASH COLLECTOR
04-03 02:34:47.997: I/MEMORY(1045): nMEM M:21568, U:13082, A:8486, C:-8104, loadScene, ADDED IMAGE b:1, s:1, id:0
04-03 02:34:49.307: I/MEMORY(1045): nMEM M:26224, U:21186, A:5038, C:-3448, loadScene, ADDED IMAGE b:1, s:1, id:1
04-03 02:34:50.597: I/MEMORY(1045): nMEM M:34328, U:29290, A:5038, C:0, loadScene, ADDED IMAGE b:1, s:1, id:2
04-03 02:34:51.839: I/MEMORY(1045): nMEM M:42432, U:37395, A:5037, C:-1, loadScene, ADDED IMAGE b:1, s:1, id:3
04-03 02:35:00.377: I/MEMORY(1045): nMEM M:42432, U:29293, A:13139, C:8102, clearScene, CLEARED VIEW id:3
04-03 02:35:00.387: I/MEMORY(1045): nMEM M:42432, U:21189, A:21243, C:8104, clearScene, CLEARED VIEW id:2
04-03 02:35:00.397: I/MEMORY(1045): nMEM M:42432, U:13085, A:29347, C:8104, clearScene, CLEARED VIEW id:1
04-03 02:35:00.407: I/MEMORY(1045): nMEM M:42432, U:4982, A:37450, C:8103, clearScene, CLEARED VIEW id:0
04-03 02:35:00.449: I/MEMORY(1045): nMEM M:42432, U:4978, A:37454, C:4, clearScene, AFTER TRASH COLLECTOR
04-03 02:35:01.217: I/MEMORY(1045): nMEM M:42432, U:13083, A:29349, C:-8105, loadScene, ADDED IMAGE b:1, s:2, id:0
04-03 02:35:23.458: I/MEMORY(1045): nMEM M:42432, U:4979, A:37453, C:8104, clearScene, CLEARED VIEW id:0
04-03 02:35:23.508: I/MEMORY(1045): nMEM M:42432, U:4978, A:37454, C:1, clearScene, AFTER TRASH COLLECTOR
04-03 02:35:24.777: I/MEMORY(1045): nMEM M:42432, U:13082, A:29350, C:-8104, loadScene, ADDED IMAGE b:1, s:3, id:0
04-03 02:35:25.719: I/MEMORY(1045): nMEM M:42432, U:21186, A:21246, C:-8104, loadScene, ADDED IMAGE b:1, s:3, id:1
04-03 02:35:26.457: I/MEMORY(1045): nMEM M:42432, U:29291, A:13141, C:-8105, loadScene, ADDED IMAGE b:1, s:3, id:2
04-03 02:35:55.008: I/MEMORY(1045): nMEM M:42432, U:21187, A:21245, C:8104, clearScene, CLEARED VIEW id:2
04-03 02:35:55.027: I/MEMORY(1045): nMEM M:42432, U:13083, A:29349, C:8104, clearScene, CLEARED VIEW id:1
04-03 02:35:55.037: I/MEMORY(1045): nMEM M:42432, U:4979, A:37453, C:8104, clearScene, CLEARED VIEW id:0
04-03 02:35:55.077: I/MEMORY(1045): nMEM M:42432, U:4978, A:37454, C:1, clearScene, AFTER TRASH COLLECTOR
04-03 02:35:55.829: I/MEMORY(1045): nMEM M:42432, U:13083, A:29349, C:-8105, loadScene, ADDED IMAGE b:1, s:4, id:0
04-03 02:35:56.427: I/MEMORY(1045): nMEM M:42432, U:21187, A:21245, C:-8104, loadScene, ADDED IMAGE b:1, s:4, id:1
04-03 02:36:00.257: I/MEMORY(1045): nMEM M:42432, U:13083, A:29349, C:8104, clearScene, CLEARED VIEW id:1
04-03 02:36:00.278: I/MEMORY(1045): nMEM M:42432, U:4979, A:37453, C:8104, clearScene, CLEARED VIEW id:0
04-03 02:36:00.318: I/MEMORY(1045): nMEM M:42432, U:4978, A:37454, C:1, clearScene, AFTER TRASH COLLECTOR
04-03 02:36:01.687: I/MEMORY(1045): nMEM M:42432, U:13083, A:29349, C:-8105, loadScene, ADDED IMAGE b:1, s:5, id:0
04-03 02:36:06.437: I/MEMORY(1045): nMEM M:42432, U:4979, A:37453, C:8104, clearScene, CLEARED VIEW id:0
04-03 02:36:06.487: I/MEMORY(1045): nMEM M:42432, U:4978, A:37454, C:1, clearScene, AFTER TRASH COLLECTOR
04-03 02:36:07.777: I/MEMORY(1045): nMEM M:42432, U:13082, A:29350, C:-8104, loadScene, ADDED IMAGE b:1, s:6, id:0
04-03 02:36:09.297: I/MEMORY(1045): nMEM M:42432, U:4978, A:37454, C:8104, clearScene, CLEARED VIEW id:0
04-03 02:36:09.337: I/MEMORY(1045): nMEM M:42432, U:4978, A:37454, C:0, clearScene, AFTER TRASH COLLECTOR
04-03 02:36:10.727: I/MEMORY(1045): nMEM M:42432, U:13082, A:29350, C:-8104, loadScene, ADDED IMAGE b:1, s:7, id:0
04-03 02:36:20.947: I/MEMORY(1045): nMEM M:42432, U:4978, A:37454, C:8104, clearScene, CLEARED VIEW id:0
04-03 02:36:20.987: I/MEMORY(1045): nMEM M:42432, U:4978, A:37454, C:0, clearScene, AFTER TRASH COLLECTOR
04-03 02:36:22.367: I/MEMORY(1045): nMEM M:42432, U:13082, A:29350, C:-8104, loadScene, ADDED IMAGE b:1, s:8, id:0
04-03 02:36:23.317: I/MEMORY(1045): nMEM M:42432, U:21187, A:21245, C:-8105, loadScene, ADDED IMAGE b:1, s:8, id:1
04-03 02:36:23.887: I/MEMORY(1045): nMEM M:42432, U:29291, A:13141, C:-8104, loadScene, ADDED IMAGE b:1, s:8, id:2
04-03 02:36:26.507: I/MEMORY(1045): nMEM M:42432, U:21187, A:21245, C:8104, clearScene, CLEARED VIEW id:2
04-03 02:36:26.517: I/MEMORY(1045): nMEM M:42432, U:13083, A:29349, C:8104, clearScene, CLEARED VIEW id:1
04-03 02:36:26.527: I/MEMORY(1045): nMEM M:42432, U:4979, A:37453, C:8104, clearScene, CLEARED VIEW id:0
04-03 02:36:26.577: I/MEMORY(1045): nMEM M:42432, U:4978, A:37454, C:1, clearScene, AFTER TRASH COLLECTOR
04-03 02:36:27.797: I/MEMORY(1045): nMEM M:42432, U:13083, A:29349, C:-8105, loadScene, ADDED IMAGE b:1, s:9, id:0
04-03 02:36:29.248: I/MEMORY(1045): nMEM M:42432, U:4979, A:37453, C:8104, clearScene, CLEARED VIEW id:0
04-03 02:36:29.287: I/MEMORY(1045): nMEM M:42432, U:4978, A:37454, C:1, clearScene, AFTER TRASH COLLECTOR
04-03 02:36:30.547: I/MEMORY(1045): nMEM M:42432, U:13082, A:29350, C:-8104, loadScene, ADDED IMAGE b:1, s:10, id:0
04-03 02:36:32.177: I/MEMORY(1045): nMEM M:42432, U:4978, A:37454, C:8104, clearScene, CLEARED VIEW id:0
04-03 02:36:32.228: I/MEMORY(1045): nMEM M:42432, U:4978, A:37454, C:0, clearScene, AFTER TRASH COLLECTOR
04-03 02:36:33.467: I/MEMORY(1045): nMEM M:42432, U:13082, A:29350, C:-8104, loadScene, ADDED IMAGE b:1, s:11, id:0
04-03 02:36:33.977: I/MEMORY(1045): nMEM M:42432, U:21187, A:21245, C:-8105, loadScene, ADDED IMAGE b:1, s:11, id:1
04-03 02:36:34.527: I/MEMORY(1045): nMEM M:42432, U:29291, A:13141, C:-8104, loadScene, ADDED IMAGE b:1, s:11, id:2
04-03 02:36:35.058: I/MEMORY(1045): nMEM M:42432, U:37395, A:5037, C:-8104, loadScene, ADDED IMAGE b:1, s:11, id:3
04-03 02:36:41.260: I/MEMORY(1045): nMEM M:42432, U:29291, A:13141, C:8104, clearScene, CLEARED VIEW id:3
04-03 02:36:41.267: I/MEMORY(1045): nMEM M:42432, U:21187, A:21245, C:8104, clearScene, CLEARED VIEW id:2
04-03 02:36:41.278: I/MEMORY(1045): nMEM M:42432, U:13083, A:29349, C:8104, clearScene, CLEARED VIEW id:1
04-03 02:36:41.287: I/MEMORY(1045): nMEM M:42432, U:4979, A:37453, C:8104, clearScene, CLEARED VIEW id:0
04-03 02:36:41.337: I/MEMORY(1045): nMEM M:42432, U:4979, A:37453, C:0, clearScene, AFTER TRASH COLLECTOR
04-03 02:36:42.618: I/MEMORY(1045): nMEM M:42432, U:13083, A:29349, C:-8104, loadScene, ADDED IMAGE b:2, s:0, id:0
04-03 02:37:00.550: I/MEMORY(1045): nMEM M:42432, U:4979, A:37453, C:8104, clearScene, CLEARED VIEW id:0
04-03 02:37:00.587: I/MEMORY(1045): nMEM M:42432, U:4979, A:37453, C:0, clearScene, AFTER TRASH COLLECTOR
04-03 02:37:01.817: I/MEMORY(1045): nMEM M:42432, U:13083, A:29349, C:-8104, loadScene, ADDED IMAGE b:2, s:1, id:0
04-03 02:37:03.347: I/MEMORY(1045): nMEM M:42432, U:4979, A:37453, C:8104, clearScene, CLEARED VIEW id:0
04-03 02:37:03.397: I/MEMORY(1045): nMEM M:42432, U:4979, A:37453, C:0, clearScene, AFTER TRASH COLLECTOR
04-03 02:37:04.607: I/MEMORY(1045): nMEM M:42432, U:13083, A:29349, C:-8104, loadScene, ADDED IMAGE b:2, s:2, id:0
04-03 02:37:05.258: I/MEMORY(1045): nMEM M:42432, U:21187, A:21245, C:-8104, loadScene, ADDED IMAGE b:2, s:2, id:1
04-03 02:37:07.677: I/MEMORY(1045): nMEM M:42432, U:13083, A:29349, C:8104, clearScene, CLEARED VIEW id:1
04-03 02:37:07.687: I/MEMORY(1045): nMEM M:42432, U:4979, A:37453, C:8104, clearScene, CLEARED VIEW id:0
04-03 02:37:07.737: I/MEMORY(1045): nMEM M:42432, U:4979, A:37453, C:0, clearScene, AFTER TRASH COLLECTOR
04-03 02:37:09.017: I/MEMORY(1045): nMEM M:42432, U:13083, A:29349, C:-8104, loadScene, ADDED IMAGE b:2, s:3, id:0
04-03 02:37:10.327: I/MEMORY(1045): nMEM M:42432, U:21187, A:21245, C:-8104, loadScene, ADDED IMAGE b:2, s:3, id:1
04-03 02:37:13.330: I/MEMORY(1045): nMEM M:42432, U:13083, A:29349, C:8104, clearScene, CLEARED VIEW id:1
04-03 02:37:13.337: I/MEMORY(1045): nMEM M:42432, U:4979, A:37453, C:8104, clearScene, CLEARED VIEW id:0
04-03 02:37:13.387: I/MEMORY(1045): nMEM M:42432, U:4979, A:37453, C:0, clearScene, AFTER TRASH COLLECTOR
04-03 02:37:14.697: I/MEMORY(1045): nMEM M:42432, U:13083, A:29349, C:-8104, loadScene, ADDED IMAGE b:2, s:4, id:0
04-03 02:37:16.037: I/MEMORY(1045): nMEM M:42432, U:21187, A:21245, C:-8104, loadScene, ADDED IMAGE b:2, s:4, id:1
04-03 02:37:17.367: I/MEMORY(1045): nMEM M:42432, U:29292, A:13140, C:-8105, loadScene, ADDED IMAGE b:2, s:4, id:2
04-03 02:37:19.087: I/MEMORY(1045): nMEM M:42432, U:37396, A:5036, C:-8104, loadScene, ADDED IMAGE b:2, s:4, id:3
04-03 02:37:44.347: I/MEMORY(1045): nMEM M:42432, U:29293, A:13139, C:8103, clearScene, CLEARED VIEW id:3
04-03 02:37:44.357: I/MEMORY(1045): nMEM M:42432, U:21189, A:21243, C:8104, clearScene, CLEARED VIEW id:2
04-03 02:37:44.367: I/MEMORY(1045): nMEM M:42432, U:13085, A:29347, C:8104, clearScene, CLEARED VIEW id:1
04-03 02:37:44.377: I/MEMORY(1045): nMEM M:42432, U:4981, A:37451, C:8104, clearScene, CLEARED VIEW id:0
04-03 02:37:44.437: I/MEMORY(1045): nMEM M:42432, U:4979, A:37453, C:2, clearScene, AFTER TRASH COLLECTOR
04-03 02:37:45.667: I/MEMORY(1045): nMEM M:42432, U:13083, A:29349, C:-8104, loadScene, ADDED IMAGE b:2, s:5, id:0
04-03 02:37:46.917: I/MEMORY(1045): nMEM M:42432, U:21188, A:21244, C:-8105, loadScene, ADDED IMAGE b:2, s:5, id:1
04-03 02:37:48.167: I/MEMORY(1045): nMEM M:42432, U:29292, A:13140, C:-8104, loadScene, ADDED IMAGE b:2, s:5, id:2
04-03 02:37:49.807: I/MEMORY(1045): nMEM M:42432, U:37396, A:5036, C:-8104, loadScene, ADDED IMAGE b:2, s:5, id:3
04-03 02:38:08.239: I/MEMORY(1045): nMEM M:42432, U:29293, A:13139, C:8103, clearScene, CLEARED VIEW id:3
04-03 02:38:08.258: I/MEMORY(1045): nMEM M:42432, U:21189, A:21243, C:8104, clearScene, CLEARED VIEW id:2
04-03 02:38:08.267: I/MEMORY(1045): nMEM M:42432, U:13086, A:29346, C:8103, clearScene, CLEARED VIEW id:1
04-03 02:38:08.277: I/MEMORY(1045): nMEM M:42432, U:4982, A:37450, C:8104, clearScene, CLEARED VIEW id:0
04-03 02:38:08.317: I/MEMORY(1045): nMEM M:42432, U:4979, A:37453, C:3, clearScene, AFTER TRASH COLLECTOR
04-03 02:38:09.527: I/MEMORY(1045): nMEM M:42432, U:13083, A:29349, C:-8104, loadScene, ADDED IMAGE b:2, s:6, id:0  

As mentioned, the memory is cleaned after the ImageView is unbound!

Here is the log I collected from my broken Nexus 7 running Android 17 (4.2). It's really hard to understand how the memory is being used here. It's never consistent! Sometimes the memory isn't allocated when I load the ImageView. Sometimes the memory is only cleared when I load a new ImageView.

Legend: M: (max memory), U: (used memory), A: (available memory), C: (change in available memory), Function that called log function

04-02 22:37:05.866: I/MEMORY(15827): jMEM M:65536, U:21300, A:44236, C:0, loadScene, ADDED IMAGE b:0, s:0, id:0
04-02 22:37:06.136: I/MEMORY(15827): jMEM M:65536, U:29444, A:36092, C:-8144, loadScene, ADDED IMAGE b:0, s:0, id:1
04-02 22:37:19.806: I/MEMORY(15827): jMEM M:65536, U:29432, A:36104, C:12, clearScene, CLEARED VIEW id:1
04-02 22:37:19.806: I/MEMORY(15827): jMEM M:65536, U:29432, A:36104, C:0, clearScene, CLEARED VIEW id:0
04-02 22:37:19.836: I/MEMORY(15827): jMEM M:65536, U:29432, A:36104, C:0, clearScene, AFTER TRASH COLLECTOR
04-02 22:37:20.116: I/MEMORY(15827): jMEM M:65536, U:37536, A:28000, C:-8104, loadScene, ADDED IMAGE b:1, s:0, id:0
04-02 22:37:28.456: I/MEMORY(15827): jMEM M:65536, U:37532, A:28004, C:4, clearScene, CLEARED VIEW id:0
04-02 22:37:28.486: I/MEMORY(15827): jMEM M:65536, U:37532, A:28004, C:0, clearScene, AFTER TRASH COLLECTOR
04-02 22:37:28.756: I/MEMORY(15827): jMEM M:65536, U:37532, A:28004, C:0, loadScene, ADDED IMAGE b:1, s:1, id:0
04-02 22:37:29.226: I/MEMORY(15827): jMEM M:65536, U:37532, A:28004, C:0, loadScene, ADDED IMAGE b:1, s:1, id:1
04-02 22:37:29.706: I/MEMORY(15827): jMEM M:65536, U:45636, A:19900, C:-8104, loadScene, ADDED IMAGE b:1, s:1, id:2
04-02 22:37:30.156: I/MEMORY(15827): jMEM M:65536, U:53740, A:11796, C:-8104, loadScene, ADDED IMAGE b:1, s:1, id:3
04-02 22:37:38.676: I/MEMORY(15827): jMEM M:65536, U:53732, A:11804, C:8, clearScene, CLEARED VIEW id:3
04-02 22:37:38.676: I/MEMORY(15827): jMEM M:65536, U:53732, A:11804, C:0, clearScene, CLEARED VIEW id:2
04-02 22:37:38.676: I/MEMORY(15827): jMEM M:65536, U:53732, A:11804, C:0, clearScene, CLEARED VIEW id:1
04-02 22:37:38.676: I/MEMORY(15827): jMEM M:65536, U:53732, A:11804, C:0, clearScene, CLEARED VIEW id:0
04-02 22:37:38.696: I/MEMORY(15827): jMEM M:65536, U:53732, A:11804, C:0, clearScene, AFTER TRASH COLLECTOR
04-02 22:37:38.966: I/MEMORY(15827): jMEM M:65536, U:53732, A:11804, C:0, loadScene, ADDED IMAGE b:1, s:2, id:0
04-02 22:37:48.156: I/MEMORY(15827): jMEM M:65536, U:53732, A:11804, C:0, clearScene, CLEARED VIEW id:0
04-02 22:37:48.216: I/MEMORY(15827): jMEM M:65536, U:37532, A:28004, C:16200, clearScene, AFTER TRASH COLLECTOR
04-02 22:37:48.676: I/MEMORY(15827): jMEM M:65536, U:37532, A:28004, C:0, loadScene, ADDED IMAGE b:1, s:3, id:0
04-02 22:37:49.256: I/MEMORY(15827): jMEM M:65536, U:37532, A:28004, C:0, loadScene, ADDED IMAGE b:1, s:3, id:1
04-02 22:37:49.716: I/MEMORY(15827): jMEM M:65536, U:45636, A:19900, C:-8104, loadScene, ADDED IMAGE b:1, s:3, id:2
04-02 22:38:07.426: I/MEMORY(15827): jMEM M:65536, U:45632, A:19904, C:4, clearScene, CLEARED VIEW id:2
04-02 22:38:07.426: I/MEMORY(15827): jMEM M:65536, U:45632, A:19904, C:0, clearScene, CLEARED VIEW id:1
04-02 22:38:07.426: I/MEMORY(15827): jMEM M:65536, U:45632, A:19904, C:0, clearScene, CLEARED VIEW id:0
04-02 22:38:07.496: I/MEMORY(15827): jMEM M:65536, U:45632, A:19904, C:0, clearScene, AFTER TRASH COLLECTOR
04-02 22:38:07.766: I/MEMORY(15827): jMEM M:65536, U:45632, A:19904, C:0, loadScene, ADDED IMAGE b:1, s:4, id:0
04-02 22:38:08.096: I/MEMORY(15827): jMEM M:65536, U:53736, A:11800, C:-8104, loadScene, ADDED IMAGE b:1, s:4, id:1
04-02 22:38:10.896: I/MEMORY(15827): jMEM M:65536, U:53736, A:11800, C:0, clearScene, CLEARED VIEW id:1
04-02 22:38:10.896: I/MEMORY(15827): jMEM M:65536, U:53736, A:11800, C:0, clearScene, CLEARED VIEW id:0
04-02 22:38:10.916: I/MEMORY(15827): jMEM M:65536, U:53736, A:11800, C:0, clearScene, AFTER TRASH COLLECTOR
04-02 22:38:11.436: I/MEMORY(15827): jMEM M:65536, U:53736, A:11800, C:0, loadScene, ADDED IMAGE b:1, s:5, id:0
04-02 22:38:19.356: I/MEMORY(15827): jMEM M:65536, U:53732, A:11804, C:4, clearScene, CLEARED VIEW id:0
04-02 22:38:19.386: I/MEMORY(15827): jMEM M:65536, U:45632, A:19904, C:8100, clearScene, AFTER TRASH COLLECTOR
04-02 22:38:19.866: I/MEMORY(15827): jMEM M:65536, U:45632, A:19904, C:0, loadScene, ADDED IMAGE b:1, s:6, id:0
04-02 22:38:21.326: I/MEMORY(15827): jMEM M:65536, U:45632, A:19904, C:0, clearScene, CLEARED VIEW id:0
04-02 22:38:21.356: I/MEMORY(15827): jMEM M:65536, U:21300, A:44236, C:24332, clearScene, AFTER TRASH COLLECTOR
04-02 22:38:21.906: I/MEMORY(15827): jMEM M:65536, U:29404, A:36132, C:-8104, loadScene, ADDED IMAGE b:1, s:7, id:0
04-02 22:38:24.376: I/MEMORY(15827): jMEM M:65536, U:29404, A:36132, C:0, clearScene, CLEARED VIEW id:0
04-02 22:38:24.406: I/MEMORY(15827): jMEM M:65536, U:29404, A:36132, C:0, clearScene, AFTER TRASH COLLECTOR
04-02 22:38:24.916: I/MEMORY(15827): jMEM M:65536, U:29404, A:36132, C:0, loadScene, ADDED IMAGE b:1, s:8, id:0
04-02 22:38:25.496: I/MEMORY(15827): jMEM M:65536, U:37548, A:27988, C:-8144, loadScene, ADDED IMAGE b:1, s:8, id:1
04-02 22:38:25.796: I/MEMORY(15827): jMEM M:65536, U:45652, A:19884, C:-8104, loadScene, ADDED IMAGE b:1, s:8, id:2
04-02 22:38:28.356: I/MEMORY(15827): jMEM M:65536, U:45652, A:19884, C:0, clearScene, CLEARED VIEW id:2
04-02 22:38:28.356: I/MEMORY(15827): jMEM M:65536, U:45652, A:19884, C:0, clearScene, CLEARED VIEW id:1
04-02 22:38:28.356: I/MEMORY(15827): jMEM M:65536, U:45652, A:19884, C:0, clearScene, CLEARED VIEW id:0
04-02 22:38:28.376: I/MEMORY(15827): jMEM M:65536, U:45652, A:19884, C:0, clearScene, AFTER TRASH COLLECTOR
04-02 22:38:28.846: I/MEMORY(15827): jMEM M:65536, U:45652, A:19884, C:0, loadScene, ADDED IMAGE b:1, s:9, id:0
04-02 22:38:29.926: I/MEMORY(15827): jMEM M:65536, U:45652, A:19884, C:0, clearScene, CLEARED VIEW id:0
04-02 22:38:29.956: I/MEMORY(15827): jMEM M:65536, U:29400, A:36136, C:16252, clearScene, AFTER TRASH COLLECTOR
04-02 22:38:30.416: I/MEMORY(15827): jMEM M:65536, U:29400, A:36136, C:0, loadScene, ADDED IMAGE b:1, s:10, id:0
04-02 22:38:31.446: I/MEMORY(15827): jMEM M:65536, U:29400, A:36136, C:0, clearScene, CLEARED VIEW id:0
04-02 22:38:31.476: I/MEMORY(15827): jMEM M:65536, U:21300, A:44236, C:8100, clearScene, AFTER TRASH COLLECTOR
04-02 22:38:31.966: I/MEMORY(15827): jMEM M:65536, U:29404, A:36132, C:-8104, loadScene, ADDED IMAGE b:1, s:11, id:0
04-02 22:38:32.226: I/MEMORY(15827): jMEM M:65536, U:37548, A:27988, C:-8144, loadScene, ADDED IMAGE b:1, s:11, id:1
04-02 22:38:32.526: I/MEMORY(15827): jMEM M:65536, U:45652, A:19884, C:-8104, loadScene, ADDED IMAGE b:1, s:11, id:2
04-02 22:38:32.816: I/MEMORY(15827): jMEM M:65536, U:53756, A:11780, C:-8104, loadScene, ADDED IMAGE b:1, s:11, id:3
04-02 22:38:34.396: I/MEMORY(15827): jMEM M:65536, U:53756, A:11780, C:0, clearScene, CLEARED VIEW id:3
04-02 22:38:34.396: I/MEMORY(15827): jMEM M:65536, U:53756, A:11780, C:0, clearScene, CLEARED VIEW id:2
04-02 22:38:34.396: I/MEMORY(15827): jMEM M:65536, U:53756, A:11780, C:0, clearScene, CLEARED VIEW id:1
04-02 22:38:34.396: I/MEMORY(15827): jMEM M:65536, U:53756, A:11780, C:0, clearScene, CLEARED VIEW id:0
04-02 22:38:34.426: I/MEMORY(15827): jMEM M:65536, U:53756, A:11780, C:0, clearScene, AFTER TRASH COLLECTOR
04-02 22:38:34.886: I/MEMORY(15827): jMEM M:65536, U:53756, A:11780, C:0, loadScene, ADDED IMAGE b:2, s:0, id:0
04-02 22:38:35.776: I/MEMORY(15827): jMEM M:65536, U:53756, A:11780, C:0, clearScene, CLEARED VIEW id:0
04-02 22:38:35.816: I/MEMORY(15827): jMEM M:65536, U:21300, A:44236, C:32456, clearScene, AFTER TRASH COLLECTOR
04-02 22:38:36.256: I/MEMORY(15827): jMEM M:65536, U:29404, A:36132, C:-8104, loadScene, ADDED IMAGE b:2, s:1, id:0
04-02 22:38:36.976: I/MEMORY(15827): jMEM M:65536, U:29404, A:36132, C:0, clearScene, CLEARED VIEW id:0
04-02 22:38:37.006: I/MEMORY(15827): jMEM M:65536, U:29404, A:36132, C:0, clearScene, AFTER TRASH COLLECTOR
04-02 22:38:37.436: I/MEMORY(15827): jMEM M:65536, U:29404, A:36132, C:0, loadScene, ADDED IMAGE b:2, s:2, id:0
04-02 22:38:37.816: I/MEMORY(15827): jMEM M:65536, U:37548, A:27988, C:-8144, loadScene, ADDED IMAGE b:2, s:2, id:1
04-02 22:38:44.986: I/MEMORY(15827): jMEM M:65536, U:37532, A:28004, C:16, clearScene, CLEARED VIEW id:1
04-02 22:38:44.986: I/MEMORY(15827): jMEM M:65536, U:37532, A:28004, C:0, clearScene, CLEARED VIEW id:0
04-02 22:38:45.016: I/MEMORY(15827): jMEM M:65536, U:37532, A:28004, C:0, clearScene, AFTER TRASH COLLECTOR
04-02 22:38:45.496: I/MEMORY(15827): jMEM M:65536, U:37532, A:28004, C:0, loadScene, ADDED IMAGE b:2, s:3, id:0
04-02 22:38:45.996: I/MEMORY(15827): jMEM M:65536, U:45636, A:19900, C:-8104, loadScene, ADDED IMAGE b:2, s:3, id:1
04-02 22:38:48.306: I/MEMORY(15827): jMEM M:65536, U:45636, A:19900, C:0, clearScene, CLEARED VIEW id:1
04-02 22:38:48.306: I/MEMORY(15827): jMEM M:65536, U:45636, A:19900, C:0, clearScene, CLEARED VIEW id:0
04-02 22:38:48.336: I/MEMORY(15827): jMEM M:65536, U:45636, A:19900, C:0, clearScene, AFTER TRASH COLLECTOR
04-02 22:38:48.846: I/MEMORY(15827): jMEM M:65536, U:45636, A:19900, C:0, loadScene, ADDED IMAGE b:2, s:4, id:0
04-02 22:38:49.326: I/MEMORY(15827): jMEM M:65536, U:45636, A:19900, C:0, loadScene, ADDED IMAGE b:2, s:4, id:1
04-02 22:38:49.856: I/MEMORY(15827): jMEM M:65536, U:53740, A:11796, C:-8104, loadScene, ADDED IMAGE b:2, s:4, id:2
04-02 22:38:50.386: I/MEMORY(15827): jMEM M:65536, U:61844, A:3692, C:-8104, loadScene, ADDED IMAGE b:2, s:4, id:3
04-02 22:38:59.486: I/MEMORY(15827): jMEM M:65536, U:61832, A:3704, C:12, clearScene, CLEARED VIEW id:3
04-02 22:38:59.486: I/MEMORY(15827): jMEM M:65536, U:61832, A:3704, C:0, clearScene, CLEARED VIEW id:2
04-02 22:38:59.486: I/MEMORY(15827): jMEM M:65536, U:61832, A:3704, C:0, clearScene, CLEARED VIEW id:1
04-02 22:38:59.486: I/MEMORY(15827): jMEM M:65536, U:61832, A:3704, C:0, clearScene, CLEARED VIEW id:0
04-02 22:38:59.516: I/MEMORY(15827): jMEM M:65536, U:61832, A:3704, C:0, clearScene, AFTER TRASH COLLECTOR
04-02 22:38:59.936: I/MEMORY(15827): jMEM M:65536, U:61832, A:3704, C:0, loadScene, ADDED IMAGE b:2, s:5, id:0
04-02 22:39:00.356: I/MEMORY(15827): jMEM M:65536, U:61832, A:3704, C:0, loadScene, ADDED IMAGE b:2, s:5, id:1

I've been through the "Displaying Bitmaps Effectively" training at
http://developer.android.com/training/displaying-bitmaps/index.html

I've also been through the following stackoverflow questions:
Recycle ImageView's Bitmap
ImageView: automatically recycle bitmap if ImageView is not visible (within ScrollView)
android - out of memory exception when creating bitmap
Bitmap decodeStream OutOfMemory Exception
Detect application heap size in Android
android createBitmap OOM when ((freeMemory > bitmapSize) && (nativeFreeHeap < bitmap size))
android 495KB image taking 10MB of Heap size
Android ImageView setImageBitmap
Android Memory Leak solution
Android custom view Bitmap memory leak

And many many more articles across the web!

To clarify, this only seems to happen in my Nexus 7. I haven't experienced the issue on my emulator or my Galaxy Nexus. I think this may be due to the fact that I am scaling the images.

Please help me detect where the memory leak is!

***EDIT (4/2/13) Please help me identify why the logs from an emulator running Android 10 (2.3.3) show the system allocating memory and the garbage then clearing in exactly like it should, and the logs from my Nexus 7 running Android 17 (4.2) show the system allocating memory and the garbage collector clearing memory apparently at random?

解决方案

I wasn't able to determine why memory allocation and collection is different among the different platforms, but I was able to mitigate the problem and prevent my application from crashing on any platform by scaling the images down to fit available memory.

This first function determines where the Bitmap memory is being stored (native or Java), and then calculates the remaining available memory (in KB).

private long calcAvailableMemory()
{
    long value = Runtime.getRuntime().maxMemory();
    String type = "";
    if (android.os.Build.VERSION.SDK_INT >= 11)
    {
        value = value / 1024) - (Runtime.getRuntime().totalMemory() / 1024);
        type = "JAVA";
    }
    else
    {
        value = value / 1024) - (Debug.getNativeHeapAllocatedSize() / 1024);
        type = "NATIVE";
    }
    Log.i(TAG, "calcAvailableMemory, size = " + value + ", type = " + type);
    return value;
}

This second function creates an ImageView to attach to the Layout. The Show object contains different variables I need to initialize the ImageView such as dimensions and filepath etc. The long maxImageMemory is derived from the first function, and is then divided by the number of views I'm attaching to the layout giving the me maximum amount of memory to allocate each Bitmap.

private ImageView newImage(Show show, long maxImageMemory)
{
    ImageView iv = new ImageView(this);
    String filePath = comin.generateFilePath(show);
    Bitmap bmp = scaleBitmap(filePath, maxImageMemory);
    Log.i(TAG, "newImage, id:" + show.id + ", file:" + filePath + ", x:" + bmp.getWidth() + ", y:" + bmp.getHeight());
    iv.setImageBitmap(bmp);
    iv.setScaleType(ImageView.ScaleType.FIT_XY);
    iv.setId(show.id);

    //set visibility
    if (show.visible)
        iv.setVisibility(View.VISIBLE);
    else
        iv.setVisibility(View.INVISIBLE);

    //set dimensions
    int width = (app.getWidth() * show.dimX) / 100;
    int height = (app.getHeight() * show.dimY) / 100;
    RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(width, height);
    iv.setLayoutParams(rlp);

    return iv;
}  

This third function scales the Bitmap that will be loaded into the ImageView being created in function 2. The while loop is the important part, that is where I increase the sample size until the needed memory for the bitmap is less than the maximum memory available.

private Bitmap scaleBitmap(String path, long maxImageMemory)
{
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    int sample = 1;
    //Scale image to screen resolution
    if (options.outHeight > app.getHeight() || options.outWidth > app.getWidth())
    {
        int heightRatio = Math.round((float) options.outHeight / (float) app.getHeight());
        int widthRatio = Math.round((float) options.outWidth / (float) app.getWidth());
        sample = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    //Scale image to stay within memory limitations
    while (calcBitmapSize(options.outWidth, options.outHeight, sample) > maxImageMemory)
    {
        sample++;
    }

    options.inSampleSize = sample;
    options.inPurgeable = true;
    options.inInputShareable = true;
    options.inJustDecodeBounds = false;

    Log.i(TAG, "scaleBitmap, scaleSample = " + sample);
    return BitmapFactory.decodeFile(path, options);
}  

This fourth function calculates the amount of memory required by the Bitmap depending on the original resolution, as well as the proposed sample size.

private long calcBitmapSize(int width, int height, int sample)
{
    long value = ((width/sample) * (height/sample) * 4) / 1024;
    Log.i(TAG, "calcBitmapSize, size = " + value);
    return value;
}  

Lastly, I developed this function to assist in troubleshooting all of these memory problems. It logs memory utilization depending on which version of Android you are running.

private void logMemory(String callingFunction)
{
    long max = Runtime.getRuntime().maxMemory() / 1024;

    if (debugJavaMemory)
    {
        long used = Runtime.getRuntime().totalMemory() / 1024;
        long available = max - used;
        long change = available - availableJavaMemoryOld;
        if (availableJavaMemoryOld != 0)
            Log.i(TAG_MEMORY, "jMEM M:" + max + ", U:" + used + ", A:" + available + ", C:" + change + ", " + callingFunction);
        availableJavaMemoryOld = available;
    }
    else if (debugNativeMemory)
    {
        long used = Debug.getNativeHeapAllocatedSize() / 1024;
        long available = max - used;
        long change = available - availableNativeMemoryOld;
        if (availableNativeMemoryOld != 0)
            Log.i(TAG_MEMORY, "nMEM M:" + max + ", U:" + used + ", A:" + available + ", C:" + change + ", " + callingFunction);
        availableNativeMemoryOld = available;
    }
}

这篇关于Android的位图内存问题 - 错误:内存在8294416字节分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆