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

问题描述

我有一个启动器 Activity,它在打开时加载和调整大位图作为背景.

每当点击后退按钮时,Activity 就会被销毁.但我认为内存还没有释放.

当我打开应用程序时,点击后退按钮并再次打开它(重复),我会收到一个OutOfMemoryError.

我很抱歉这个新手问题,但我想知道每当 Activity破坏 时我如何释放内存?

@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_welcome);//MARK - 移动BackgroundImageViewmovingBackgroundImageView = (ImageView) findViewById(R.id.movi​​ngBackgroundImageView);movingBackgroundImageView.setColorFilter(Color.argb(255, 255, 255, 255));movingBackgroundImageView.setScaleType(ImageView.ScaleType.MATRIX);movingBackgroundImageView.setAlpha(0.28f);准备背景动画();}私有无效prepareBackgroundAnimation(){DisplayMetrics displaymetrics = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);screenWidth = displaymetrics.widthPixels;screenHeight = displaymetrics.heightPixels;movingImageHeight = displaymetrics.heightPixels;移动图像宽度 = 1920.0/1080.0 * 移动图像高度;bitmapImage = BitmapFactory.decodeResource(this.getResources(), R.drawable.movi​​ng_background_image);scaledBitmap = bitmapImage.createScaledBitmap(bitmapImage, (int)movingImageWidth,(int)movingImageHeight,false);movingBackgroundImageView.setImageBitmap(scaledBitmap);backgroundImageInBeginning = true;movingBackgroundImageView.post(new Runnable() {@覆盖公共无效运行(){movingBackgroundImageView.setImageMatrix(matrix);移动背景();}});}

12-22 13:44:49.549 30885-30885/?E/AndroidRuntime:致命异常:主要进程:id.testingapp.android.TestingApp,PID:30885java.lang.OutOfMemoryError:无法分配 26211852 字节的分配,其中有 14018312 个空闲字节和 13MB,直到 OOM在 dalvik.system.VMRuntime.newNonMovableArray(本机方法)在 android.graphics.Bitmap.nativeCreate(Native Method)在 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(Native Method)在 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)

我已经尝试将所有这些都放在 onDestroyed() 中,但问题仍然存在

@Override受保护的无效 onDestroy() {结束();位图图像 = 空;scaledBitmap = null;super.onDestroy();Runtime.getRuntime().gc();System.gc();}
解决方案

为其添加以下代码

@Override受保护的无效 onDestroy() {//android.os.Process.killProcess(android.os.Process.myPid());super.onDestroy();if(scaledBitmap!=null){scaledBitmap.recycle();缩放位图=空;}}

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

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();
}
解决方案

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

     }

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

08-04 05:00