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).
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
.
来源:https://stackoverflow.com/questions/26802531/how-to-increase-heap-size-on-android-2-3-gingerbread