A
A
Alexander2014-09-04 16:49:19
Java
Alexander, 2014-09-04 16:49:19

Memory leak in LinearLayout's Background, how to fix?

private LinearLayout bg;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
        super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

        bg = (LinearLayout)findViewById(R.id.bg);

        if(bg.getBackground() == null)
  {			
                // На этом поле
    Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565); 
    bitmap.eraseColor(colorBg);
    //radGrad(bitmap);
    //drawCircles(bitmap);
    bg.setBackground(new BitmapDrawable(this.getResources(), bitmap));
  }
}

After the activity is called again, the application starts eating a couple of megabytes more memory until it eats up all the memory.
Drawing with XML doesn't work for me.
I have a few questions:
1: Why is bg.getBackground equal to null after calling onCreate again, but the object is still in memory?
2: How to delete this object
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mintormo, 2014-09-04
@UserAlexandr

There is no explicit object deletion in Java. Instead, there's garbage collection. If you assign null to an object reference, then the object will still be in memory for some time, until the collector finally removes it. Deletion time is not defined. Judging by the code, the Bitmap will be created every time a new activity is created. For example, if you twist the phone: when you change the layout, the activity is deleted and recreated. If the pictures are large, then you can quickly eat out all the memory. The first solution that came to mind was to store resources like these pictures in a separate class. Download them as needed, checking the fact of their availability. It is not worth storing resources in activity classes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question