10

因此,Android 面临的挑战之一是各种设备规格(尤其是设备内存)。

我已经编写了我的模型对象SoftReferences,以便以延迟加载的方式广泛使用 Java,因此 VM 可以自由修剪数据模型中当前未使用的部分,因为它认为合适,它们只是根据需要重新构建。

然而,在实践中面临的一个挑战SoftReferences是它们往往会在被弱引用后的几秒钟内被清除,而不是一直徘徊直到 VM 内存不足,因此它们在允许模型修剪方面工作良好,但它们效果不佳,因为它通常意味着内存中没有任何内容。理想情况下,在具有大量内存的设备上,您会让用户受益于将对象保存在内存中。

因此,通常SoftReferences与 LRU 机制结合使用,其中 LRU 保留指向最近引用对象的硬指针。这当然不是理想的,因为它假设您有足够的内存来存储所有这些几乎没有引用的对象。

了解什么是 LRU 的良好默认设置也带来了挑战。

在一个完美的世界中,Android 会使用它的低内存回调作为提示(所以我可以从一个小的 LRU 开始,并定期将其提升,直到低内存回调开始发生,然后将其退回以找到设备的良好价值) ,但根据我的经验,这个回调似乎与实际的 VM 内存压力不一致。

有没有人遇到过一种合理的方法来检测您的数据模型在特定设备上使用了过多的内存?

4

4 回答 4

2

如果你想确定 android 中的可用内存,那么你可以检查或进入透视图 - >ddms 2)第二件事是你可以使用内存分析器工具检查已用和未使用的内存,你还可以检查内存的可用性。

于 2013-02-04T11:08:26.427 回答
1

如果您使用 Android NDK:

#include <unistd.h>

size_t getTotalSystemMemory()
{
    long pages = sysconf(_SC_PHYS_PAGES);
    long page_size = sysconf(_SC_PAGE_SIZE);
    return pages * page_size;
}

size_t getFreeSystemMemory()
{
    long pages = sysconf(_SC_AVPHYS_PAGES);
    long page_size = sysconf(_SC_PAGE_SIZE);
    return pages * page_size;
}

于 2013-01-26T17:12:06.223 回答
0

这行得通吗?

MemoryInfo mi = new MemoryInfo();
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
activityManager.getMemoryInfo(mi);
long availableMegs = mi.availMem / 1048576L;
于 2012-10-02T19:44:08.900 回答
0

Here's a more detailed NDK answer:

Looking at the Bionic LibC source for sysconf reveals that _SC_PHYS_PAGES and _SC_AVPHYS_PAGES are implemented by reading /proc/meminfo, which is maintained by the Linux kernel. The first few lines of that file on the Lenovo Android 7.0.1 device with 4GB RAM I'm using look like this:

MemTotal:        3721064 kB
MemFree:          208108 kB
MemAvailable:    2543032 kB
  • sysconf( _SC_PHYS_PAGES) gives you the MemTotal value, in units of the page size. This seems to be the total memory available to Android for code, data, and so on. It's about 300MB less than the claimed physical RAM on the device I'm using, which may be accounted for by the screen buffer and so on.
  • sysconf( _SC_AVPHYS_PAGES) gives you the MemFree value, in the same units. This appears to be the memory that isn't in use for anything at all. On my 4GB device, it's only about 200MB.

Sadly, there is no sysconf parameter for the far more useful MemAvailable value, which is the RAM that can be made available if required. Apparently this is because MemAvailable was only added to the Linux kernel in early 2014, while the sysconf() support seems rather older than that. MemAvailable is not in a Samsung Android 5.0, but is in Amazon FireOS 5.6 (based on Android 5.1), and a Motorola Android 7.1; I don't know about 6.x.

What I'm planning to do for an NDK test harness (not a app that will be distributed), having got to the bottom of this, is use MemAvailable if it is there, and otherwise use half the physical RAM size. Since I will have to read /proc/meminfo to find out if MemAvailable is there, there's no point in having Bionic LibC read it again if I call sysconf().

于 2018-07-13T15:17:48.257 回答