OutOfMemory error on Android 6.0.1 devices after the second launch
Asked Answered
F

1

11

My project was working until I updated my S6 Edge to Android 6.0.1.

The app crashes with an OutOfMemory error after the second launch.

The application contains 2000+ images in the directory and I am using a timer to increment a counter and display them on an imageView. When onFinish()/onPause()/onDestroy() is called I am destroying/cancelling all the objects such as the timers, counter and imageView by setting them to null.

This is how I am fetching/printing the image to the imageView

int resID = getResources().getIdentifier("animation"+i , "drawable", getPackageName());
    Drawable animationFrame = ContextCompat.getDrawable(this, resID);
    animationView.setImageDrawable(animationFrame);
    i++;

It runs on initial launch (even if I install the app using a generated APK). When I remove the app from the minimised applications, the application crashes after 2-3 seconds. I have checked the memory allocated and it is normal (up to 12MB) on the first run whereas on the second the memory allocated is 255MB.

onDestroy() method This includes ALL my variables initialised. My variables are initialised as private or with no access variable.

@Override
protected void onDestroy() {
    super.onDestroy();
    pauseAnimation();
    animationView.setImageDrawable(null);
    animationView = null;
    justAnimation = null;
    buttonSign = null;
    i = 0;
    media.stop();
    media  = null;
    deathRateEU = 0;
    deathRateUK = 0;
    labelNumber = null;
    labelNumberUK = null;
    buttonSign = null;
    loadingEU = null;
    loadingUK = null;

}

Media is a MediaPlayer initialised as public and it plays a sound when counter(i) reaches a point

I installed this on another device that runs Android 5.1.3 (not sure but it is 5.1 something).

Is there is a known bug for OutOfMemory issues when application get minimised on Android 6.0.1?

I am 100% sure that I did not make any changes before/after my phone updated to version 6.0.1

Memory Monitor on 1st Launch
enter image description here

Second Launch
enter image description here

Notes:

  1. If the application is already installed on the device and I try to compile it using Android Studio, the crash occurs
  2. I have to uninstall/re-compile to manage to get the app working in the first go
  3. I get the same error when I try to run my project on a Genymotion Android 6.0 device, but not on an Android 5.1 genymotion virtual device
Fluviatile answered 18/4, 2016 at 11:46 Comment(13)
Please provide a minimal reproducible example. This would include the code that is crashing and the stack trace of the crash.Supraliminal
Out of interest, does onDestroy even get called? I had to move some image and video related cleanup code to onPause, and restore it in onResume, because onDestroy wasn't a reliable place to do this.Indebtedness
Yes, I placed a breakpoint to make sure that this method is calledFluviatile
My question is: I am destroying all my objects properly? my project contains variables such as MediaPlayer, Animation, int(s), timers and buttonsFluviatile
finish(); ??? In onDestroy() ?Litmus
that was put there just to make sure that the application will call onDestroy() method. doesn't make a difference if I remove the calling of finish()Fluviatile
Objects dont get deleted if you make a pointer null.Litmus
should I go with removeView for interface objects? or else, what is the best way to destroy an imageView, Animation and MediaPlayerFluviatile
Try animationView.setImageResource(resId); so you dont have to make that Drawable.Litmus
thank you for your suggestion, but unfortunately this did not resolve my issue=/ anything else to suggest here??Fluviatile
is there any more evidence I can get to see what i causing my application to run out of memory on the second launch?Fluviatile
there is a chance that android is scaling those 2k+ images every time before showing, so did you try putting images inside drawable-nodpi folder ?Rena
Have you tried recycling the drawablw once you have done animationView.setImageDrawable(null)? You might try the following in onDestroy(): get the drawable assigned to a local variable; setImageDrawable(null); drawable.recycle()Klinges
G
0

I would recommend you to use Glide library to efficiently load large images.
You can also check this article about how to efficiently load Bitmaps.

Gambill answered 28/1, 2019 at 9:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.