Clearing a program on exit and working with GC

I am writing a program that includes a ton of images, more than 100 per screen density. Fortunately, they are not very large in terms of space. I use about 11 at any given time. I also use a function Bitmap.createScaledBitmapfor each image, which apparently takes up a lot of memory.

So far, in debugging this application, I seem to be able to use it indefinitely without encountering a memory problem, which I hope means that I'm not a memory leak.

However, one thing I noticed is that if I "exit" my application through the "Back" button (I do not start anything in the background), and then restart the application shortly afterwards, I sometimes get memory errors when call Bitmap.createScaledBitmap

01-07 19:01:24.935: ERROR/AndroidRuntime(27419): java.lang.OutOfMemoryError: bitmap size exceeds VM budget

So basically my question is: what am I doing wrong here? Do I need to clear my own garbage, for example in onDestroy, when the user clicks the back button? I would have thought that the GC would take care of this automatically when the back button was pressed and the action was destroyed. It makes me think that I am doing something else than a memory leak. But then I return to the fact that my application can use heavy memory for 20 minutes, but then kill itself 5 seconds after the restart, which puzzles me.

Thanks to everyone.

Edit: I implemented a couple of quick and dirty fixes.

I tried this first, and it was harder to force the application to force close.

@Override
public void onBackPressed() {
    finish();
}

onBackPressed, . , - , allImages = null;

protected void onStop () {
    super.onStop();
    mComponent.releaseImages();
}

, finish() . .

+3
3

, - Android. "" Java. SO , .

. http://developer.android.com/guide/topics/fundamentals.html#actlife

, , onStart(), onStop(), . .

+3

1: onResume(), Bitmap . , Bitmap .

2: , . Bitmap - initialized, . , Bitmap. " " .

: . , , . . , . - .

, .

, :

  • Bitmap , .
  • , , Bitmap, .

, :

  • Bitmap .

, (, ).

. Bitmap, , Bitmap . , Bitmap, , , .

+2

Source: https://habr.com/ru/post/1784198/


All Articles