我有一个启动器 Activity
,它在打开时加载并调整大位图的大小,因为它是背景。
无论何时点击返回按钮,Activity
都会被销毁
。但我认为内存还没有释放。
当我打开应用程序时,点击返回按钮并再次打开它(重复),我会得到一个 OutOfMemoryError
。
对于这个新手问题我很抱歉,但我想知道每当 Activity
被 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)
编辑:
我已尝试将所有这些都放入 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/