在 android 上分配超过 512mb 的内存

Allocate more than 512mb ram on android

我正在编写一个小 android 应用程序,它会填充设备的 ram 以进行测试。 但是我不允许分配超过 512MB。 错误日志显示:

Throwing OutOfMemoryError "Failed to allocate a 1000012 byte allocation with 229520 free bytes and 224KB until OOM"

Alloc partial concurrent mark sweep GC freed 6(192B) AllocSpace objects, 0(0B) LOS objects, 0% free, 511MB/512MB 

Clamp target GC heap from 527MB to 512MB

我试图在捕捉到 oom 异常后分配更多 ram - 但没有成功。

有没有办法分配更多的内存?

不在Java。每个 Android 应用程序都有堆限制。通常,这比 512MB 低很多;我假设您已将 android:largeHeap="true" 添加到您的清单中以使其变大。

如果您需要分配更多的内存,则需要使用 NDK,因为在本机代码中进行的分配不计入 Dalvik/ART 堆限制。