gpt4 book ai didi

Android 应用程序堆大小

转载 作者:行者123 更新时间:2023-11-29 17:54:57 24 4
gpt4 key购买 nike

Android 如何确定应将特定数量的堆分配给应用程序?

例如,如果应用程序大小为 13 mb,并且应用程序加载于

具有 ram 512 的 MDPI 设备为其分配 20mb 的堆大小

如果加载在

HDPI 设备 512 mb ram 它分配 50mb ram

如果加载在

HDPI 设备 1MB ram 仍然分配 50mb ram

为了XHDPI 设备 1gb Ram 它分配 68mb

为了

XXHDPI 设备 2GB ram 它分配 134mb 的堆大小

我的问题是

  • Android 如何为应用程序计算和分配大小?
  • 此分配是取决于 RAM 大小或设备类型(如 mdpi、hdpi 等)还是取决于两者?
  • 是否取决于操作系统版本?
  • Android 操作系统使用任何特定公式计算和分配内存给应用程序?

最佳答案

我会看一下 developer docs .

Allocating and Reclaiming App Memory

Here are some facts about how Android allocates then reclaims memory from your app:

The Dalvik heap for each process is constrained to a single virtual memory range. This defines the logical heap size, which can grow as it needs to (but only up to a limit that the system defines for each app). The logical size of the heap is not the same as the amount of physical memory used by the heap. When inspecting your app's heap, Android computes a value called the Proportional Set Size (PSS), which accounts for both dirty and clean pages that are shared with other processes—but only in an amount that's proportional to how many apps share that RAM. This (PSS) total is what the system considers to be your physical memory footprint. For more information about PSS, see the Investigating Your RAM Usage guide. The Dalvik heap does not compact the logical size of the heap, meaning that Android does not defragment the heap to close up space. Android can only shrink the logical heap size when there is unused space at the end of the heap. But this doesn't mean the physical memory used by the heap can't shrink. After garbage collection, Dalvik walks the heap and finds unused pages, then returns those pages to the kernel using madvise. So, paired allocations and deallocations of large chunks should result in reclaiming all (or nearly all) the physical memory used. However, reclaiming memory from small allocations can be much less efficient because the page used for a small allocation may still be shared with something else that has not yet been freed.

Restricting App Memory

To maintain a functional multi-tasking environment, Android sets a hard limit on the heap size for each app. The exact heap size limit varies between devices based on how much RAM the device has available overall. If your app has reached the heap capacity and tries to allocate more memory, it will receive an OutOfMemoryError.

In some cases, you might want to query the system to determine exactly how much heap space you have available on the current device—for example, to determine how much data is safe to keep in a cache. You can query the system for this figure by calling getMemoryClass(). This returns an integer indicating the number of megabytes available for your app's heap. This is discussed further below, under Check how much memory you should use.

只是为了提取你想要的信息:

  1. 确切的堆大小限制因设备而异,具体取决于多少设备总体上可用的 RAM。

  2. 您可以深入研究源代码以查看堆内存的大小已计算,但我不确定您将从中获得什么。

  3. 堆内存可能会因操作系统版本而异,因为会有服务和操作系统资源使用方面的许多差异会影响从中取出后设备有多少 RAM 可用总 RAM。

如果您真的想深入了解内存堆,我会使用内存调试工具 detailed here .

关于Android 应用程序堆大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20073288/

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