被破坏时释放特定活动的记忆 [英] Release Memory of Particular Activity when it is Destroyed

查看:102
本文介绍了被破坏时释放特定活动的记忆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个启动器Activity,该启动器在打开时会加载大位图并调整其背景大小.

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

每当按下后退"按钮时,Activity就是destroyed.但是我认为内存还没有释放.

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

当我打开应用程序时,点击后退"按钮,然后再次将其打开(重复),我会得到一个OutOfMemoryError.

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

对于这个新手问题,我感到抱歉,但我想知道每当Activitydestroyed时如何释放内存?

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:致命异常:main 流程:id.testingapp.android.TestingApp,PID:30885 java.lang.OutOfMemoryError:未能分配26211852字节分配与14018312可用字节和13MB,直到OOM 在dalvik.system.VMRuntime.newNonMovableArray(本机方法) 在android.graphics.Bitmap.nativeCreate(本地方法) 在android.graphics.Bitmap.createBitmap(Bitmap.java:939) 在android.graphics.Bitmap.createBitmap(Bitmap.java:912) 在android.graphics.Bitmap.createBitmap(Bitmap.java:843) 在android.graphics.Bitmap.createScaledBitmap(Bitmap.java:719) 在id.testingapp.android.TestingApp.WelcomeActivity.prepareBackgroundAnimation(WelcomeActivity.java:140) 在id.TestingApp.android.TestingApp.WelcomeActivity.onCreate(WelcomeActivity.java:72) 在android.app.Activity.performCreate(Activity.java:6283) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758) 在android.app.ActivityThread.access $ 900(ActivityThread.java:177) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1448) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:145) 在android.app.ActivityThread.main(ActivityThread.java:5942) 在java.lang.reflect.Method.invoke(本机方法) 在java.lang.reflect.Method.invoke(Method.java:372) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1400) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

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)

我尝试将所有这些内容放入onDestroyed(),但问题仍然存在

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();
}

推荐答案

为此添加以下代码

@Override
protected void onDestroy() {
    //android.os.Process.killProcess(android.os.Process.myPid());

    super.onDestroy();
    if(scaledBitmap!=null)
            {
                scaledBitmap.recycle();
                scaledBitmap=null;
            }

     }

这篇关于被破坏时释放特定活动的记忆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆