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
Community
  • 1
  • 1
cmario
  • 605
  • 1
  • 7
  • 22
  • 3
    Please provide a [mcve]. This would include the code that is crashing and the stack trace of the crash. – CommonsWare Apr 18 '16 at 11:49
  • 1
    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. – Ewald Apr 18 '16 at 12:03
  • Yes, I placed a breakpoint to make sure that this method is called – cmario Apr 18 '16 at 12:05
  • My question is: I am destroying all my objects properly? my project contains variables such as MediaPlayer, Animation, int(s), timers and buttons – cmario Apr 18 '16 at 12:10
  • `finish();` ??? In onDestroy() ? – greenapps Apr 18 '16 at 12:18
  • 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() – cmario Apr 18 '16 at 12:19
  • Objects dont get deleted if you make a pointer null. – greenapps Apr 18 '16 at 12:22
  • should I go with removeView for interface objects? or else, what is the best way to destroy an imageView, Animation and MediaPlayer – cmario Apr 18 '16 at 12:24
  • Try `animationView.setImageResource(resId);` so you dont have to make that Drawable. – greenapps Apr 18 '16 at 15:23
  • thank you for your suggestion, but unfortunately this did not resolve my issue=/ anything else to suggest here?? – cmario Apr 18 '16 at 15:40
  • is there any more evidence I can get to see what i causing my application to run out of memory on the second launch? – cmario Apr 18 '16 at 15:56
  • 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 ? – mfaisalhyder Aug 10 '16 at 07:50
  • 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() –  Dec 17 '16 at 19:01

1 Answers1

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.

Henrique
  • 822
  • 8
  • 13