Android - 增长堆(碎片案例) - 字节分配.. 不加载任何位图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10635633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - Grow Heap (Frag Case) - Byte Allocation.. Not Loading any bitmaps
提问by Ashwin
This happens when the app loads from Splash screen to Main page. It happens only on the device not on simulator:
当应用程序从启动画面加载到主页时会发生这种情况。它只发生在设备上而不是模拟器上:
05-17 08:10:16.627: I/dalvikvm-heap(14021): Grow heap (frag case) to 20.580MB for 2424256-byte allocation
05-17 08:10:16.666: D/dalvikvm(14021): GC_FOR_ALLOC freed 1K, 3% free 21000K/21511K, paused 21ms
05-17 08:10:16.697: D/dalvikvm(14021): GC_CONCURRENT freed 116K, 3% free 20885K/21511K, paused 2ms+2ms
05-17 08:10:16.720: D/dalvikvm(14021): GC_FOR_ALLOC freed 44K, 4% free 20841K/21511K, paused 10ms
05-17 08:10:16.728: I/dalvikvm-heap(14021): Grow heap (frag case) to 24.533MB for 4310896-byte allocation
I used Ecplise MAT - the byte allocation resolved - Android.Graphics.Bitmap $preloaded images...
我使用了 Ecplise MAT - 字节分配已解决 - Android.Graphics.Bitmap $preloaded 图像...
The device I am using is Google Nexus Prime, Android 4.0
我使用的设备是 Google Nexus Prime,Android 4.0
Has anyone encountered the same? Can someone throw some expertise....
有没有人遇到同样的情况?有人可以抛出一些专业知识......
采纳答案by Amokrane Chentir
You are probably trying to decode a very big Bitmap
which results in an OutOfMemory
exception. It means that the operation you are trying to achieve is exceeding the VM Budget allowed for each application on your device, in terms of heap memory consumption (which appears to be 24 MB on your device, probably more on your emulator which is why it doesn't happen there!).
您可能正在尝试解码Bitmap
导致OutOfMemory
异常的非常大的内容。这意味着您尝试实现的操作超出了设备上每个应用程序允许的 VM 预算,就堆内存消耗而言(在您的设备上似乎是 24 MB,在您的模拟器上可能更多,这就是为什么它没有不会发生在那里!)。
Try to sample your Bitmap
by a factor of two for instance:
例如,尝试以Bitmap
两倍的系数对您进行采样:
BitmapFactory.Options o = new BitmapFactory.Options();
o.inSampleSize = 2;
Bitmap b = BitmapFactory.decodeFile(pathToBitmap, o);
回答by hjm
I have experienced the same issue and found a solution to it.
我遇到了同样的问题并找到了解决方案。
Are you loading your bitmaps from resource? if so try placing them in corresponding drawable folders instead of keeping them in "drawable" or "drawable-mdpi".
你是从资源加载你的位图吗?如果是这样,请尝试将它们放在相应的可绘制文件夹中,而不是将它们保存在“drawable”或“drawable-mdpi”中。
When you have your image resources in plain drawable folder, to the system it means drawable-mdpi. Therefore devices with high or extra high dpi (Galaxy Nexus I believe is Extra high) will expand that resource to match the dpi of the device.
当您在普通 drawable 文件夹中拥有图像资源时,对于系统而言,它意味着 drawable-mdpi。因此,具有高或超高 dpi(我认为 Galaxy Nexus 是超高)的设备将扩展该资源以匹配设备的 dpi。
If you only have 1 size for your resources, put them in drawable-nodpi and it will be used as is. However some of your layouts may be affected by this, so I kept certain images in the drawable folder (like button images etc.) and moved backgrounds and other bigger resources into drawable-nodpi.
如果您的资源只有 1 个尺寸,请将它们放在 drawable-nodpi 中,它将按原样使用。但是,您的某些布局可能会受到此影响,因此我将某些图像保留在 drawable 文件夹中(如按钮图像等),并将背景和其他更大的资源移动到 drawable-nodpi 中。
Hope this helps ;)
希望这可以帮助 ;)