android - 特定 Activity 被销毁时释放内存

标签 android android-studio out-of-memory android-bitmap android-memory

我有一个启动器 Activity,它在打开时加载并调整大位图的大小,因为它是背景。

无论何时点击返回按钮,Activity 都会被销毁。但我认为内存还没有释放。

当我打开应用程序时,点击返回按钮并再次打开它(重复),我会得到一个 OutOfMemoryError

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

@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)

编辑:

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

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

     }

关于android - 特定 Activity 被销毁时释放内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34409549/

相关文章:

hadoop - Hive on Tez 中的 ORDER BY 语句抛出 OOM 异常

android - CustomList 未添加到 fragment 中

安卓工作室 : Failure [INSTALL_FAILED_OLDER_SDK]

android - ADB:使用 Uri 中的多个参数从 adb 启动 VIEW Intent

java - 如何增加 STS(Spring Tool Suite)内存大小?

android - 位图 - 内存不足异常

android - JobIntentService 被销毁,当应用程序被销毁时

android - Actionbar 列表导航宽度 - 换行内容

javascript - Reactjs:拆分功能不起作用?

android - 无法在 android studio 中解析 R?