大存储(本机堆)增加设置布局 [英] Enormous memory (native heap) increase setting a layout

查看:203
本文介绍了大存储(本机堆)增加设置布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个增加了6 MB机堆的使用时,我预期上升约0.1 MB

I get an increase of native heap usage of 6 MB when I'm expecting an increase of about 0.1 MB

我如何来到这些数字:

Log.d("test", "before setting layout");
showMemoryStats();

setContentView(R.layout.my_layout);

Log.d("test", "after setting layout");
showMemoryStats();

其中showMemoryStats是:

Where showMemoryStats is:

public static void showMemoryStats() {
    Log.i("test", "----------------------------------------------------------------------------------------------------------------------------");
    Log.i("test", "showing memory stats in xx");
    double nativeUsage = Debug.getNativeHeapAllocatedSize(); 
    Log.i("test", "nativeUsage: " + nativeUsage);
    //current heap size 
    double heapSize =  Runtime.getRuntime().totalMemory();
    Log.i("test", "heapSize: " + heapSize);
    //amount available in heap 
    double heapRemaining = Runtime.getRuntime().freeMemory();   
    Log.i("test", "heapRemaining: " + heapRemaining);
    double memoryAvailable = Runtime.getRuntime().maxMemory() - (heapSize - heapRemaining) - nativeUsage;
    Log.i("test", "memoryAvailable: " + memoryAvailable);
    Log.i("test", "----------------------------------------------------------------------------------------------------------------------------");
}

输出:

07-03 16:50:40.127:D /测试(18647):设置布局前

07-03 16:50:40.127: D/test(18647): before setting layout

07-03 16:50:40.127:I /试验(18647): ----------------------------------------------------------------------------------------------------------------------------

07-03 16:50:40.127: I/test(18647): ----------------------------------------------------------------------------------------------------------------------------

07-03 16:50:40.127:I /试验(18647):显示在XX内存统计

07-03 16:50:40.127: I/test(18647): showing memory stats in xx

07-03 16:50:40.127:I /试验(18647):nativeUsage:5706008.0

07-03 16:50:40.127: I/test(18647): nativeUsage: 5706008.0

07-03 16:50:40.127:I /试验(18647):堆大小:4905968.0

07-03 16:50:40.127: I/test(18647): heapSize: 4905968.0

07-03 16:50:40.127:I /试验(18647):老天premaining:1815456.0

07-03 16:50:40.127: I/test(18647): heapRemaining: 1815456.0

07-03 16:50:40.127:I /试验(18647):memoryAvailable:2.4757912E7

07-03 16:50:40.127: I/test(18647): memoryAvailable: 2.4757912E7

07-03 16:50:40.127:I /试验(18647): ----------------------------------------------------------------------------------------------------------------------------

07-03 16:50:40.127: I/test(18647): ----------------------------------------------------------------------------------------------------------------------------

07-03 16:50:40.348:D / dalvikvm(18647):GC_EXTERNAL_ALLOC释放100K,49%免费2924K / 5639K,外部0K / 0K,暂停45ms

07-03 16:50:40.348: D/dalvikvm(18647): GC_EXTERNAL_ALLOC freed 100K, 49% free 2924K/5639K, external 0K/0K, paused 45ms

07-03 16:50:40.518:D / dalvikvm(18647):GC_EXTERNAL_ALLOC释放9K,49%免费2917K / 5639K,2700K外部/ 3371K,暂停51ms

07-03 16:50:40.518: D/dalvikvm(18647): GC_EXTERNAL_ALLOC freed 9K, 49% free 2917K/5639K, external 2700K/3371K, paused 51ms

07-03 16:50:40.638:D / dalvikvm(18647):GC_EXTERNAL_ALLOC释放4K,49%免费2928K / 5639K,外部3952K / 4356K,暂停51ms

07-03 16:50:40.638: D/dalvikvm(18647): GC_EXTERNAL_ALLOC freed 4K, 49% free 2928K/5639K, external 3952K/4356K, paused 51ms

07-03 16:50:40.698:D / dalvikvm(18647):GC_EXTERNAL_ALLOC释放2K,48%免费2933K / 5639K,外部5963K / 6027K,暂停24ms

07-03 16:50:40.698: D/dalvikvm(18647): GC_EXTERNAL_ALLOC freed 2K, 48% free 2933K/5639K, external 5963K/6027K, paused 24ms

07-03 16:50:40.718:D /测试(18647):设置布局后

07-03 16:50:40.718: D/test(18647): after setting layout

07-03 16:50:40.718:I /试验(18647): ----------------------------------------------------------------------------------------------------------------------------

07-03 16:50:40.718: I/test(18647): ----------------------------------------------------------------------------------------------------------------------------

07-03 16:50:40.718:I /试验(18647):显示在XX内存统计

07-03 16:50:40.718: I/test(18647): showing memory stats in xx

07-03 16:50:40.718:I /试验(18647):nativeUsage:1.2101904E7

07-03 16:50:40.718: I/test(18647): nativeUsage: 1.2101904E7

07-03 16:50:40.718:I /试验(18647):堆大小:4734944.0

07-03 16:50:40.718: I/test(18647): heapSize: 4734944.0

07-03 16:50:40.718:I /试验(18647):老天premaining:1716432.0

07-03 16:50:40.718: I/test(18647): heapRemaining: 1716432.0

07-03 16:50:40.718:I /试验(18647):memoryAvailable:1.8434016E7

07-03 16:50:40.718: I/test(18647): memoryAvailable: 1.8434016E7

07-03 16:50:40.718:I /试验(18647): ----------------------------------------------------------------------------------------------------------------------------

07-03 16:50:40.718: I/test(18647): ----------------------------------------------------------------------------------------------------------------------------

在数学:

12101856字节 - 5703424字节= 6398432字节= 6.10202 MB

12101856 bytes - 5703424 bytes = 6398432 bytes = 6.10202 mb

我去了,虽然我的布局文件,并添加使用的所有可绘制的大小,并来到124 KB的总大小 - > 0.121094 MB

I went though my layout file and added the size of all the drawables used, and came to a total size of 124 kb -> 0.121094 mb

所以,在地球上是什么原因造成本机堆,增加6万桶?

So what on earth is causing the native heap to increase 6 mb?

我可以把布局文件,如果有人问了,不知道这是否有道理?只是一堆布局与IDS和可绘,并以此表示我已经检查了可绘制的大小。

I can put the layout file, if somebody ask for it, don't know if it makes sense... just a bunch of layouts with ids and drawables, and as said I already checked the drawables for the size.

感谢很多提前...

编辑:这是解决方案,在我的情况 - 为了总结答复和意见:我将图像存储在文件夹绘制,并用华电国际设备。解决办法:把图像文件夹绘制,华电国际。原因:该系统拓展我的图片,从绘制为了配合华电国际的分辨率,虽然他们已经在华电国际的分辨率。这使他们占据了2倍以上的内存比他们应该。

推荐答案

那么,位图需要大量的空间了。

Well, Bitmaps take a huge amount of space up.

每个像素需要4个字节!

Each pixel requires 4 Bytes!

因此​​,一个100万像素的图像是4MB!我假设你正在使用许多更小的图像,但他们很快就增加了。

So a 1M pixel Image is 4MB!!! I assume you are using many smaller images, but they soon add up.

解决此问题的方法是加载位图以较低的分辨率(在大多数情况下,你不会注意到任何区别,因为他们没有被显示在最大水库反正)。看看 BitmapFactory.Options.inSampleSize 这一点。

One way around this is to load the bitmaps at a lower resolution (in most cases you wont notice any difference as they are not being displayed at max res anyway). Look into BitmapFactory.Options.inSampleSize for this.

由于Android电子报价

As quoted from android

位图占用了大量的内存,尤其是丰富的图像,例如   照片。例如,在Galaxy Nexus的相机拍摄的照片   高达2592x1936像素(5百万像素)。如果位图构   采用的是ARGB_8888(从Android 2.3以后默认值),那么   加载此图像到内存大约需要的内存(2592 * 1936年* 4 19MB   字节),立刻用尽在某些设备上的每个应用程序的限制。

Bitmaps take up a lot of memory, especially for rich images like photographs. For example, the camera on the Galaxy Nexus takes photos up to 2592x1936 pixels (5 megapixels). If the bitmap configuration used is ARGB_8888 (the default from the Android 2.3 onward) then loading this image into memory takes about 19MB of memory (2592*1936*4 bytes), immediately exhausting the per-app limit on some devices.

这里是一个伟大的链接来解释它更好

这篇关于大存储(本机堆)增加设置布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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