How to increase heap size on Android 2.3 (Gingerbr

2019-04-13 07:27发布

I am getting an OutOfMemoryError when I try to create a String larger than 4MB using StringBuilder.append().

As far as I know, StringBuilder doesn't have any limitations regarding size and so doesn't String.

So I searched for "how to increase Android heap size" and as far as I could conclude from this answer and this answer, Gingerbread is the only version in which you have no way of dealing with the heap size, since for earlier versions you use the VMRuntime class and for newer versions android:largeHeap="true" on the Manifest.

Is that assumption correct? What do you do when you're in between VMRuntime and android:largeHeap="true"?

I used Runtime.getRuntime().totalMemory()/1048576 - Runtime.getRuntime().maxMemory()/1048576 to get "5.00 MB allocated of 42.00 MB available" on the device I'm using for tests (Android 2.3.6).

1条回答
来,给爷笑一个
2楼-- · 2019-04-13 08:14

Is that assumption correct?

I cannot say whether VMRuntime ever worked. android:largeHeap is definitely something for API Level 11+.

What do you do when you're in between VMRuntime and android:largeHeap="true"?

Find a more memory-efficient way of building your 4MB String, such as by providing a large capacity to your StringBuilder constructor.

Or, find a way to implement whatever it is that you are doing without creating a contiguous 4MB String.

查看更多
登录 后发表回答