java.lang.OutofMemoryError问题

时间:2016-12-14 09:51:42

标签: java android

我正在为我的拼贴项目开发一个小游戏应用程序。在我的应用程序在第一轮和第二轮顺利运行的那一刻,它会在此之后崩溃以及此错误java.lang.OutofMemoryError。我找不到这个问题的问题。

if (pic == null)
    pic = BitmapFactory.decodeResource(context.getResources(), R.drawable.background);
scalepic = Bitmap.createScaledBitmap(pic, 720, 3840, false);
pic.recycle();
background = new Background(scalepic);

这是错误:

java.lang.OutOfMemoryError: Failed to allocate a 117964812 byte allocation with 16777216 free bytes and 18MB until OOM
    at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:856)
    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:675)
    at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:703)
    at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:733)
    at de.hs_kl.imst.gatav.runner.GameView.<init>(GameView.java:88)
    at de.hs_kl.imst.gatav.runner.Start.onCreate(Start.java:39)
    at android.app.Activity.performCreate(Activity.java:6876)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
    at android.app.ActivityThread.access$1100(ActivityThread.java:222)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:158)
    at android.app.ActivityThread.main(ActivityThread.java:7229)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

如果我删除了回收,那么游戏在第一次尝试后就会崩溃......

1 个答案:

答案 0 :(得分:0)

您可以调用System.gc();方法根据需要调用垃圾收集器。