E/art: Throwing OutOfMemoryError "Failed to allocate a 6144012 byte allocation with 5750804 free bytes and 5MB until OOM"
E/Resources: ExecutionException ... Caused by: java.lang.OutOfMemoryError
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1014)
at android.content.res.Resources$RCCallable.call(Resources.java:1168)
崩潰位置:setContentView(R.layout.activity_dev_list) 時解析佈局資源耗盡內存。
應用啟動時播放開機動畫使用 AnimationDrawable(幀動畫),動畫結束後未調用 stop(),導致:
AnimationDrawable anim = (AnimationDrawable) imageView.getDrawable();
// 動畫結束監聽(API 16+)
anim.setCallback(new Drawable.Callback() {
@Override public void invalidateDrawable(Drawable who) {}
@Override public void scheduleDrawable(Drawable who, Runnable what, long when) {}
@Override public void unscheduleDrawable(Drawable who, Runnable what) {
// 動畫結束時釋放
if (who instanceof AnimationDrawable) {
((AnimationDrawable) who).stop();
who.setCallback(null); // 斷開引用鏈
}
}
});
anim.start();
// 或簡單場景:固定時長後手動停止
new Handler().postDelayed(() -> {
if (anim != null && anim.isRunning()) {
anim.stop();
imageView.setImageDrawable(null); // 釋放 Bitmap
}
}, anim.getDuration());
VideoView/MediaPlayer 播放視頻替代。onDestroy/onStop 中統一調用 anim.stop() 並置空引用。Glide/Picasso 加載大圖時啟用 .apply(RequestOptions().diskCacheStrategy(DiskCacheStrategy.NONE)) 避免內存峰值。