Clearing heap memory for Out of Memory Exception

后端 未结 1 1895
轻奢々
轻奢々 2021-02-02 03:10

I Know the question regarding out of memory already been asked ,but i found no solution

In Bitmap Factory i got out of memory Exception, even use

inSampl         


        
相关标签:
1条回答
  • 2021-02-02 03:58

    After 3 days of struggle i found a solution for not increasing Heap memory by using this

    i replace all my ImageView like this

    <com.example.util.SingleShotImageView
                        android:id="@+id/grid_image"
                        android:layout_width="170dp"
                        android:layout_height="240dp"
                        android:adjustViewBounds="true"
                        android:layout_centerInParent="true"
                         />
    

    Using this class i use to clear the Image bitmap heap size in onDetachedFromWindow function

    public class SingleShotImageView extends ImageView {
    
        public SingleShotImageView(Context context) {
            super(context);
        }
    
        public SingleShotImageView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public SingleShotImageView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        protected void onDetachedFromWindow () {
            setImageDrawable(null);
            setBackgroundDrawable(null);
            setImageBitmap(null);
            System.gc();
        }
    
    }
    

    now it works fine and my heap memory remains

    Grow heap (frag case) to 11.719MB for 8192016-byte allocation

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