See how background images eat up your memory

There are two pages loading login. Both pages use 1920*1080 images, which are still 32bit.
At the beginning of the program, the loading page ate 30m, then jumped to login, ate another 30m, and then. I removed the
loading background image, and the memory immediately became 39m. It is terrifying to imagine that such a large image eats up 30m of memory
. The

solution is
not to add a background image to the xml, use code to add it, and recycle it
private void initBg(){
        Bitmap bm = BitmapFactory.decodeResource(this.getResources(), R.drawable.bg_reg);
        BitmapDrawable bd = new BitmapDrawable(this.getResources(), bm);
        bg_ll.setBackgroundDrawable(bd);
    }


 @Override
    protected void onDestroy() {
        if(bg_ll!=null){
            BitmapDrawable bd = (BitmapDrawable)bg_ll.getBackground();
            bg_ll.setBackgroundResource(0);//Don't forget to set the background to null to avoid the used a recycled bitmap error when onDraw refreshes the background
            if(bd!=null){
                bd.setCallback(null);
                bd.getBitmap().recycle();
            }
        }
        System.gc();//No gc system will not release background image memory immediately
        super.onDestroy ();

    }




Look at the result after ondestory gc after I changed the code



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326551088&siteId=291194637
Recommended