Android: how to increase heap size at runtime?

前端 未结 5 1793
伪装坚强ぢ
伪装坚强ぢ 2020-12-09 10:41

I have an image cache in my application which is implemented using SoftReferences. Dalvik starts applications with relatively small heap, and then increases it in case of de

相关标签:
5条回答
  • 2020-12-09 10:48

    If the cache is ususally small as you say, you can decide a valid footprint of your app by yourself and maintain your own cache without SoftReferences.

    For example by a simple total byte counter: Just add or move any element used to the top of a list, add its size to the counter if it is new. Delete from the bottom if the total bytes exceed your rule-of-thumb limit, thereby decreasing your counter. Maybe the LinkedHashMap class is useful for that: It can be used as a cache like a HashMap, but it has an order too like a list.

    0 讨论(0)
  • 2020-12-09 10:51

    The problem is that SoftReferences are useful for allocations done in the java heap space, but images are allocated natively, so this cache type won't really work on Android.

    0 讨论(0)
  • 2020-12-09 10:58

    I don't think you can or are supposed to influence a device's memory at this level. Let the system do its thing and don't go against it. Do you need to hold such a big image cache with SoftReferences even when activities start?

    You want to check how it is done in Shelves: see line 82 in http://code.google.com/p/shelves/source/browse/trunk/Shelves/src/org/curiouscreature/android/shelves/util/ImageUtilities.java?r=26

    0 讨论(0)
  • 2020-12-09 10:59

    you can't increase the heap size dynamically.

    you can request to use more by using android:largeHeap="true" in the manifest, but you might not get any more heap size than normal, since it's only a request.

    also, you can use native memory, 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-12-09 11:15

    Instead of increasing heap size you can do some thing better. As you said that you are maintaining cache in you application which is implemented using SoftReferences. The best thing is to use LruCache you can do some thing like this:

    private LruCache<String, Bitmap> bitmapCache;
    final int memClass;
    int cacheSize;
    
    memClass = ((ActivityManager) context.getSystemService(
        Context.ACTIVITY_SERVICE)).getMemoryClass();
    

    Return the approximate per-application memory class of the current device. This gives you an idea of how hard a memory limit you should impose on your application to let the overall system work best. The returned value is in megabytes; the baseline Android memory class is 16 (which happens to be the Java heap limit of those devices); some device with more memory may return 24 or even higher numbers.

    cacheSize = 1024 * 1024 * memClass / 10;
    bitmapCache = new LruCache<String, Bitmap>(cacheSize) {
      @Override
      protected int sizeOf(String key, Bitmap value) {
        return value.getHeight() * value.getRowBytes();
     }
    };
    

    it will remove the bitmap images from LruCache if the memory exceeds the located memory to LruCache and load the new image in it.

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