Jay*_*iyk 20 android out-of-memory android-memory android-studio android-bitmap
我有一个启动器Activity加载和调整大位图的大小,因为它是打开时的背景.
每当按下后退按钮时,Activity就是destroyed.但我认为记忆尚未公布.
当我打开应用程序时,点击后退按钮再次打开它(反复),我会得到一个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();
}
});
}
Run Code Online (Sandbox Code Playgroud)
12-22 13:44:49.549 30885-30885 /?E/AndroidRuntime:FATAL EXCEPTION:main进程:id.testingapp.android.TestingApp,PID:30885 java.lang.OutOfMemoryError:无法在dalvik.system.VMRuntime.newNonMovableArray中分配带有14018312个空闲字节和13MB的26211852字节分配android.graphics上的android.graphics.Bitmap.createBitmap(Bitmap.java:939)的android.graphics.Bitmap.nativeCreate(Native Method)处于android.graphics的android.graphics.Bitmap.createBitmap(Bitmap.java:912)位于id.TestingApp的id.testingapp.android.TestingApp.WelcomeActivity.prepareBackgroundAnimation(WelcomeActivity.java:140)的android.graphics.Bitmap.createScaledBitmap(Bitmap.java:719)上的.bitmap.createBitmap(Bitmap.java:843). android.TestingApp.WelcomeActivity.onCreate(WelcomeActivity.java:72)在android.app.AseCreate(Activity.java:6283)的android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)android.app.ActivityThread上的android.app.Activity.performCreate(Activity.java:6283) .performLaunchActivity(ActivityThread.java:2646)在android.app.ActivityThread.ha 在Android.app.Handler.dispatchMessage上的android.app.ActivityThread.access $ 900(ActivityThread.java:177)的android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1448)的ndleLaunchActivity(ActivityThread.java:2758) Handler.java:102)在android.app.Looper.loop(Looper.java:145)的android.app.ActivityThread.main(ActivityThread.java:5942)at 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
protected void onDestroy() {
finish();
bitmapImage = null;
scaledBitmap = null;
super.onDestroy();
Runtime.getRuntime().gc();
System.gc();
}
Run Code Online (Sandbox Code Playgroud)
Rav*_*aha 13
为它添加以下代码
@Override
protected void onDestroy() {
//android.os.Process.killProcess(android.os.Process.myPid());
super.onDestroy();
if(scaledBitmap!=null)
{
scaledBitmap.recycle();
scaledBitmap=null;
}
}
Run Code Online (Sandbox Code Playgroud)
在活动中,如果您正在调用finish()方法,则会销毁该方法并将其所有资源排入队列以进行垃圾回收.
因此,此活动使用的所有内存将在下一个GC周期中释放.
要么
你可以尝试这个来清理记忆,
@Override
public void onDestroy() {
super.onDestroy();
Runtime.getRuntime().gc();
}
Run Code Online (Sandbox Code Playgroud)
查看此详细信息.希望有所帮助.
| 归档时间: |
|
| 查看次数: |
19679 次 |
| 最近记录: |