Memory and Activities :: OutOfMemory

前端 未结 3 2155
一生所求
一生所求 2020-12-19 11:35

SETUP :

I have this app which has 4 activities in a linear path, really simple navigation : A -> B -> C -> D

All the activities share the

3条回答
  •  太阳男子
    2020-12-19 12:02

    So after hours of investigation and the help of Xavi here are the results :

    Q. Why would Android decompress multiple times the same background, once per activity ? Seems inefficient.

    A. Even though it would seem logical to have some sort of way to ask a bitmap to be shared across activities since we are on mobile devices with little memory, this does not seem to exists in Android. Every time a bitmap is used in different activities it is uncompressed to native memory.

    Q. Is it possible to overcome this problem by using themes or will I see the same "allocate one bitmap by activity" weirdness ?

    After experimentation, the memory consumed using themes does not differ at all from the amount of memory used by explicitely setting the bitmap in the xml of the layouts. This is weird to me since styling is about grouping attributes to a same place.

    Q. Why are the activities not reclaimed when closed ?

    A. Well i'm not sure but what I found is that this gave me OOM errors almost only when debugging. When launching the app from the device, it almost never happened. A glitch in the debugging process ? Try it before you loose 5 hours testing a zillion thing.

    Q. Why are MAT and dumpsys presenting different numbers ?

    A. The answer by Xavi is correct, dumpsys meminfo shows all the memory allocated (native + dalvik) while the MAT shows only the Dalvik one. Since bitmaps pixels are allocated in the native memory, MAT won't see it. This is only true prior to Android 3.0 where they changed the allocation scheme and made the pixel data of bitmap fit into Dalvik.

    Q. How did I solve my problem

    A. First this might not have been a problem when not debugging. Second, to be on the safe side I replaced the gradient png with a shape with a radial gradient and used the

    getWindow().setFormat(PixelFormat.RGBA_8888);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DITHER);
    

    in the oncreate of my activities to try to avoid banding. I will still have banding on some devices but I'd rather have banding than FCs

提交回复
热议问题