How to increase heap size of an android application?

后端 未结 7 1562
天涯浪人
天涯浪人 2020-11-22 06:41

I am writing an Android application which uses several 3D models. Such a model with textures can take up a lot of memory. I found out the manufacturer sets a limit on the he

相关标签:
7条回答
  • 2020-11-22 07:03

    Use second process. Declare at AndroidManifest new Service with

    android:process=":second"
    

    Exchange between first and second process over BroadcastReceiver

    0 讨论(0)
  • 2020-11-22 07:03

    This can be done by two ways according to your Android OS.

    1. You can use android:largeHeap="true" in application tag of Android manifest to request a larger heap size, but this will not work on any pre Honeycomb devices.
    2. On pre 2.3 devices, you can use the VMRuntime class, but this will not work on Gingerbread and above See below how to do it.
    VMRuntime.getRuntime().setMinimumHeapSize(BIGGER_SIZE);
    

    Before Setting HeapSize make sure that you have entered the appropriate size which will not affect other application or OS functionality. Before settings just check how much size your app takes & then set the size just to fulfill your job. Dont use so much of memory otherwise other apps might affect.

    Reference: http://dwij.co.in/increase-heap-size-of-android-application

    0 讨论(0)
  • 2020-11-22 07:15

    you can't increase the heap size dynamically.

    you can request to use more by using android:largeHeap="true" in the manifest.

    also, you can use native memory (NDK & JNI) , so you actually bypass the heap size limitation.

    here are some posts i've made about it:

    • How to cache bitmaps into native memory

    • JNI bitmap operations , for helping to avoid OOM when using large images

    and here's a library i've made for it:

    • https://github.com/AndroidDeveloperLB/AndroidJniBitmapOperations
    0 讨论(0)
  • 2020-11-22 07:15

    Increasing Java Heap unfairly eats deficit mobile resurces. Sometimes it is sufficient to just wait for garbage collector and then resume your operations after heap space is reduced. Use this static method then.

    0 讨论(0)
  • 2020-11-22 07:16

    From what I remember you could use VMRuntime class in early Android versions but now you just can't anymore.

    I don't think letting the developer choose the heap size in a mobile environment can be considered so safe though. I think it's easier that you can find a way to modify the heap size in a specific device (not on the programming side) that by trying to modify it from the application itself.

    0 讨论(0)
  • 2020-11-22 07:22

    You can use android:largeHeap="true" to request a larger heap size, but this will not work on any pre Honeycomb devices. On pre 2.3 devices, you can use the VMRuntime class, but this will not work on Gingerbread and above.

    The only way to have as large a limit as possible is to do memory intensive tasks via the NDK, as the NDK does not impose memory limits like the SDK.

    Alternatively, you could only load the part of the model that is currently in view, and load the rest as you need it, while removing the unused parts from memory. However, this may not be possible, depending on your app.

    0 讨论(0)
提交回复
热议问题