当 Android 给出 OutOfMemory 异常?

When Android gives OutOfMemory Exception?

我正在阅读 Android 中的 OutOfMemory 错误,当 Android 运行 内存不足时出现。我们是否知道如果我的应用程序消耗了一些 x MB 内存,那么它会出现 OutOfMemory 错误? Android 是否根据设备和总物理内存为每个应用程序分配特定数量的内存?

例如,我有一台安装了 2GB RAM 的设备,OS 占用了 500MB,其他应用占用了 500MB。现在我的应用程序有 1048MB 内存到 运行。那么在这种特殊情况下,当系统给出 OutOfMemory 时?

每个应用程序都有一些可用于堆分配的内存限制。它因不同的手机而异(您也可以在清单中增加它)。 This answer 对此提供了非常详细的说明,给出了某些模型和设置的具体数字。

至于如何确定:

it tends to be based more on screen resolution, as higher-resolution screens tend to want to manipulate larger bitmaps, so Google makes heap size recommendations that, hopefully, device manufacturers will abide by. CommonsWare

google 本身Here

最能说明您要查找的内容


要获得您可以使用多少内存,您可以这样做:

ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
int memoryClass = manager.getMemoryClass();
Log.v("onCreate", "memoryClass:" + Integer.toString(memoryClass));

您的 OutOfMemory 异常是由堆大小引起的,与 RAM 无关。

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.

这里有一个有用的 link : https://developer.android.com/training/articles/memory.html

希望对你有帮助:)