Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Release Memory of Particular Activity when it is Destroyed

Tags:

I have a launcher Activity that load and resize big bitmap as it's background when it opens.

Whenever hit the back button, the Activity is destroyed. But I think the memory is not released yet.

When I open back the app, hit the back button and open it again (repeatedly), I will get a OutOfMemoryError.

I am sorry for this newbie question but I am wondering how do I release the memory whenever an Activity is destroyed?

@Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_welcome);      //MARK - movingBackgroundImageView     movingBackgroundImageView = (ImageView) findViewById(R.id.movingBackgroundImageView);     movingBackgroundImageView.setColorFilter(Color.argb(255, 255, 255, 255));     movingBackgroundImageView.setScaleType(ImageView.ScaleType.MATRIX);     movingBackgroundImageView.setAlpha(0.28f);      prepareBackgroundAnimation(); }  private void prepareBackgroundAnimation() {      DisplayMetrics displaymetrics = new DisplayMetrics();     getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);      screenWidth = displaymetrics.widthPixels;     screenHeight = displaymetrics.heightPixels;      movingImageHeight = displaymetrics.heightPixels;     movingImageWidth = 1920.0 / 1080.0 * movingImageHeight;      bitmapImage = BitmapFactory.decodeResource(this.getResources(), R.drawable.moving_background_image);     scaledBitmap = bitmapImage.createScaledBitmap(bitmapImage, (int) movingImageWidth, (int) movingImageHeight, false);     movingBackgroundImageView.setImageBitmap(scaledBitmap);      backgroundImageInBeginning = true;      movingBackgroundImageView.post(new Runnable() {         @Override         public void run() {             movingBackgroundImageView.setImageMatrix(matrix);             moveBackground();         }     }); } 

12-22 13:44:49.549 30885-30885/? E/AndroidRuntime: FATAL EXCEPTION: main Process: id.testingapp.android.TestingApp, PID: 30885 java.lang.OutOfMemoryError: Failed to allocate a 26211852 byte allocation with 14018312 free bytes and 13MB until OOM at dalvik.system.VMRuntime.newNonMovableArray(Native Method) at android.graphics.Bitmap.nativeCreate(Native Method) at android.graphics.Bitmap.createBitmap(Bitmap.java:939) at android.graphics.Bitmap.createBitmap(Bitmap.java:912) at android.graphics.Bitmap.createBitmap(Bitmap.java:843) at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:719) at id.testingapp.android.TestingApp.WelcomeActivity.prepareBackgroundAnimation(WelcomeActivity.java:140) at id.TestingApp.android.TestingApp.WelcomeActivity.onCreate(WelcomeActivity.java:72) at android.app.Activity.performCreate(Activity.java:6283) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758) at android.app.ActivityThread.access$900(ActivityThread.java:177) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5942) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

EDIT:

I have tried to put all these in onDestroyed() but the problem persists

@Override protected void onDestroy() {     finish();     bitmapImage = null;     scaledBitmap = null;     super.onDestroy();     Runtime.getRuntime().gc();     System.gc(); } 
like image 704
JayVDiyk Avatar asked Dec 22 '15 06:12

JayVDiyk


People also ask

What happens when an activity is destroyed android?

When Android creates and destroys an activity, the activity moves from being launched to running to being destroyed. An activity is running when it's in the foreground of the screen. onCreate() gets called when the activity is first created, and it's where you do your normal activity setup.

Which method is called when Android system kills the activity due to memory issue?

A cached process is one that is not currently needed, so the system is free to kill it as desired when resources like memory are needed elsewhere.

What is the activity life cycle?

An Android activity goes through six major lifecycle stages or callbacks. These are: onCreate() , onStart() , onResume() , onPause() , onStop() , and onDestroy() . The system invokes each of these callbacks as an activity enters a new state.

When can Android activity be destroyed?

The Activity lifecycle is especially important because whenever an activity leaves the screen, the activity can be destroyed. When an activity is destroyed, when the user returns to the activity, the activity will be re-created and the lifecycle methods will be called again.


2 Answers

Add following code for it

@Override protected void onDestroy() {     //android.os.Process.killProcess(android.os.Process.myPid());      super.onDestroy();     if(scaledBitmap!=null)             {                 scaledBitmap.recycle();                 scaledBitmap=null;             }       } 
like image 137
Ravindra Kushwaha Avatar answered Sep 23 '22 11:09

Ravindra Kushwaha


In activity if you're calling the finish() method from is destroyed and all its resources are queued for garbage collection.

So, all memory that was used by this activity will be freed during next GC cycle.

OR

you can try this to clean memory,

@Override public void onDestroy() {     super.onDestroy();     Runtime.getRuntime().gc();       } 

check this details. hope that helps.

like image 35
Amit Vaghela Avatar answered Sep 21 '22 11:09

Amit Vaghela