gpt4 book ai didi

android - 巨大的内存( native 堆)增加设置布局

转载 作者:太空狗 更新时间:2023-10-29 13:35:14 24 4
gpt4 key购买 nike

当我期望增加约 0.1 MB 时,我发现 native 堆使用量增加了 6 MB

我是如何得出这些数字的:

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

setContentView(R.layout.my_layout);

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

showMemoryStats 在哪里:

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/test(18647): before setting layout

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

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

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

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

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

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

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

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 freed 9K, 49% free 2917K/5639K, external 2700K/3371K, paused 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 freed 2K, 48% free 2933K/5639K, external 5963K/6027K, paused 24ms

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

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

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

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

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

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

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

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

数学:

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

我检查了我的布局文件并添加了所有使用的可绘制对象的大小,总大小为 124 kb -> 0.121094 mb

那么到底是什么导致 native 堆增加 6 MB?

我可以放置布局文件,如果有人要它,不知道它是否有意义...只是一堆带有 id 和可绘制对象的布局,并且如前所述,我已经检查了可绘制对象的大小。

提前致谢...

编辑:就我而言,这是解决方案 - 为了总结响应和评论:我将图像存储在“可绘制”文件夹中并使用 hdpi 设备。解决方案:将图像放在文件夹 drawable-hdpi 中。原因:系统正在从“可绘制”中拉伸(stretch)我的图像以匹配 hdpi 分辨率,尽管它们已经是 hdpi 分辨率。这使它们占用的内存比应有的多 2 倍。

最佳答案

嗯,位图占用了大量的空间。

每个像素需要 4 个字节!

所以 100 万像素的图像是 4MB!我假设您正在使用许多较小的图像,但它们很快就会加起来。

解决此问题的一种方法是以较低的分辨率加载位图(在大多数情况下,您不会注意到任何差异,因为它们无论如何都不会以最大分辨率显示)。为此,查看 BitmapFactory.Options.inSampleSize

引自android

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.

Here is a great link to explain it better .

关于android - 巨大的内存( native 堆)增加设置布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11314023/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com