在Android上,每次运行都会内存不足

时间:2012-05-08 19:34:00

标签: android memory

我的应用程序在第一次运行时运行良好,但如果我重新启动它会崩溃并让我强行关闭。在我按下强制关闭按钮后,我可以再次启动应用程序。如果我在按下后退按钮后尝试重新启动我必须强制关闭。我允许我每次尝试都没有错误地运行它。我认为android应该负责将内存返回到计算机,所以我们不必做那个家务管理:我确实有一个大的阵列:

int  worldgrid= new int[640][480][3];

在我的构造函数中,我可能需要缩短它。我做了第一次尝试。 log cat消息:

  

05-08 19:09:46.0​​35:E / dalvikvm-heap(420):28字节内存不足   分配

     

05-08 19:09:46.195:I / dalvikvm(420):|基团="主" SCOUNT = 0   dsCount = 0 s = N obj = 0x4001d8e0 self = 0xccb0 05-08 19:09:46.195:   I / dalvikvm(420):| sysTid = 420 nice = 0 sched = 0/0 cgrp = default   handle = -1345026008 05-08 19:09:46.205:I / dalvikvm(420):|   schedstat =(52616400652 22917249866 1196)05-08 19:09:46.205:   I / dalvikvm(420):at java.lang.reflect.Array.createMultiArray(Native   方法)05-08 19:09:46.205:I / dalvikvm(420):at   java.lang.reflect.Array.newInstance(Array.java:444)05-08   19:09:46.455:I / dalvikvm(420):at   com.gravedigger.MainGamePanel。(MainGamePanel.java:215)05-08   19:09:46.455:I / dalvikvm(420):at   com.digger.DiggerActivity.onCreate(GravediggerActivity.java:116)05-08   19:09:46.615:I / dalvikvm(420):at   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)   05-08 19:09:46.625:I / dalvikvm(420):at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)   05-08 19:09:46.765:I / dalvikvm(420):at   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)   05-08 19:09:46.765:I / dalvikvm(420):at   android.app.ActivityThread.access $ 2300(ActivityThread.java:125)05-08   19:09:46.775:I / dalvikvm(420):at   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)   05-08 19:09:46.775:I / dalvikvm(420):at   android.os.Handler.dispatchMessage(Handler.java:99)05-08   19:09:46.785:I / dalvikvm(420):at   android.os.Looper.loop(Looper.java:123)05-08 19:09:46.785:   I / dalvikvm(420):at   android.app.ActivityThread.main(ActivityThread.java:4627)05-08   19:09:46.895:I / dalvikvm(420):at   java.lang.reflect.Method.invokeNative(Native Method)05-08   19:09:46.895:I / dalvikvm(420):at   java.lang.reflect.Method.invoke(Method.java:521)05-08 19:09:46.895:   I / dalvikvm(420):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)   05-08 19:09:46.895:I / dalvikvm(420):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)05-08   19:09:46.905:I / dalvikvm(420):at   dalvik.system.NativeStart.main(Native Method)05-08 19:09:47.038:   E / dalvikvm(420):HeapWorker被楔入:内部花费11152ms   Lcom / android / internal / os / BinderInternal $ GcWatcher; .finalize()V 05-08   19:09:47.038:I / dalvikvm(420):DALVIK THREADS:05-08 19:09:47.045:   I / dalvikvm(420):" main" prio = 5 tid = 1 VMWAIT

     

05-08 19:09:47.045:I / dalvikvm(420):|基团="主" SCOUNT = 1   dsCount = 0 s = N obj = 0x4001d8e0 self = 0xccb0 05-08 19:09:47.045:   I / dalvikvm(420):| sysTid = 420 nice = 0 sched = 0/0 cgrp = default   handle = -1345026008 05-08 19:09:47.045:I / dalvikvm(420):|   schedstat =(52980087202 23270903160 1219)05-08 19:09:47.045:   I / dalvikvm(420):at java.lang.reflect.Array.createMultiArray(Native   方法)05-08 19:09:47.045:I / dalvikvm(420):at   java.lang.reflect.Array.newInstance(Array.java:444)05-08   19:09:47.045:I / dalvikvm(420):at   com.digger.MainGamePanel。(MainGamePanel.java:215)05-08   19:09:47.045:I / dalvikvm(420):at   com.digger.DiggerActivity.onCreate(GravediggerActivity.java:116)05-08   19:09:47.045:I / dalvikvm(420):at   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)   05-08 19:09:47.185:I / dalvikvm(420):at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)   05-08 19:09:47.185:I / dalvikvm(420):at   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)   05-08 19:09:47.195:I / dalvikvm(420):at   android.app.ActivityThread.access $ 2300(ActivityThread.java:125)05-08   19:09:47.195:I / dalvikvm(420):at   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)   05-08 19:09:47.195:I / dalvikvm(420):at   android.os.Handler.dispatchMessage(Handler.java:99)05-08   19:09:47.195:I / dalvikvm(420):at   android.os.Looper.loop(Looper.java:123)05-08 19:09:47.195:   I / dalvikvm(420):at   android.app.ActivityThread.main(ActivityThread.java:4627)05-08   19:09:47.195:I / dalvikvm(420):at   java.lang.reflect.Method.invokeNative(Native Method)05-08   19:09:47.195:I / dalvikvm(420):at   java.lang.reflect.Method.invoke(Method.java:521)05-08 19:09:47.195:   I / dalvikvm(420):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)   05-08 19:09:47.195:I / dalvikvm(420):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)05-08   19:09:47.195:I / dalvikvm(420):at   dalvik.system.NativeStart.main(Native Method)05-08 19:09:47.195:   I / dalvikvm(420):" Binder Thread#2" prio = 5 tid = 6 NATIVE 05-08   19:09:47.195:I / dalvikvm(420):|基团="主" sCount = 1 dsCount = 0 s = N.   obj = 0x44f366b8 self = 0x11d230 05-08 19:09:47.195:I / dalvikvm(420):|   sysTid = 425 nice = 0 sched = 0/0 cgrp = default handle = 1297528 05-08   19:09:47.195:I / dalvikvm(420):| schedstat =(118912719 416670422 51   )05-08 19:09:47.195:I / dalvikvm(420):at   dalvik.system.NativeStart.run(Native Method)05-08 19:09:47.195:   I / dalvikvm(420):" Binder Thread#1" prio = 5 tid = 5 NATIVE 05-08   19:09:47.195:I / dalvikvm(420):|基团="主" sCount = 1 dsCount = 0 s = N.   obj = 0x44f36480 self = 0x11fd80 05-08 19:09:47.750:I / dalvikvm(420):|   sysTid = 424 nice = 0 sched = 0/0 cgrp = default handle = 1269616 05-08   19:09:47.750:I / dalvikvm(420):| schedstat =(114332827 305656917 35   )05-08 19:09:47.750:I / dalvikvm(420):at   dalvik.system.NativeStart.run(Native Method)05-08 19:09:47.755:   I / dalvikvm(420):" JDWP"守护进程prio = 5 tid = 4 VMWAIT 05-08 19:09:47.755:   I / dalvikvm(420):|基团="系统" sCount = 1 dsCount = 0 s = N.   obj = 0x44f352a0 self = 0x135ad0 05-08 19:09:47.755:I / dalvikvm(420):|   sysTid = 423 nice = 0 sched = 0/0 cgrp = default handle = 1196352 05-08   19:09:47.765:I / dalvikvm(420):| schedstat =(59551849 158373474 18)   05-08 19:09:47.765:I / dalvikvm(420):at   dalvik.system.NativeStart.run(Native Method)05-08 19:09:47.775:   I / dalvikvm(420):" Signal Catcher"守护进程prio = 5 tid = 3 VMWAIT 05-08   19:09:47.886:I / dalvikvm(420):|基团="系统" sCount = 1 dsCount = 0   s = N obj = 0x44f351e8 self = 0x125550 05-08 19:09:47.886:I / dalvikvm(420):   | sysTid = 422 nice = 0 sched = 0/0 cgrp = default handle = 1240608 05-08   19:09:47.886:I / dalvikvm(420):| schedstat =(2639901 40055709 3)   05-08 19:09:47.886:I / dalvikvm(420):at   dalvik.system.NativeStart.run(Native Method)05-08 19:09:47.886:   I / dalvikvm(420):" HeapWorker"守护进程prio = 5 tid = 2 RUNNABLE 05-08   19:09:47.886:I / dalvikvm(420):|基团="系统" sCount = 0 dsCount = 0   s = N obj = 0x438b8e50 self = 0x12ece0 05-08 19:09:47.886:I / dalvikvm(420):   | sysTid = 421 nice = 0 sched = 0/0 cgrp = default handle = 1195896 05-08   19:09:47.906:I / dalvikvm(420):| schedstat =(4477669539 2785983510   211)05-08 19:09:47.906:I / dalvikvm(420):at   com.android.internal.os.BinderInternal $ GcWatcher.finalize(BinderInternal.java:~48)   05-08 19:09:47.906:I / dalvikvm(420):at   dalvik.system.NativeStart.run(Native Method)05-08 19:09:47.906:   D / dalvikvm(420):threadid = 2:向threadid = 2发送两个SIGSTKFLT   (tid = 421)导致debuggerd dump 05-08 19:09:57.895:D / dalvikvm(420):   发送,暂停让debuggerd运行05-08 19:10:05.956:   D / dalvikvm(420):持续05-08 19:10:05.956:E / dalvikvm(420):VM   中止

2 个答案:

答案 0 :(得分:1)

在内存中拥有如此相当大的数组可能并不好。 但无论如何,您可以通过以下方式重构阵列来减少内存使用量: int worldgrid = new int [3] [480] [640];

这个技巧会多次减少内存使用量。它与数组和每个项目的java内存开销有关,详情可以在这里找到。 http://www.javamex.com/tutorials/memory/array_memory_usage.shtml

另外检查一下,你没有保留任何到数组的链接,它会阻止它从内存中删除。删除所有链接后,垃圾收集器也不会立即从内存中删除。所以,如果你没有使数组的链接等于null,它会被保存在内存中,当你重新启动应用程序时(没有保证活动被完全杀死),并尝试在onCreate中创建数组(我从日志中看到) )旧阵列尚未被移除。

答案 1 :(得分:0)

我无法告诉您为什么每隔一段时间就会收到错误,而不会看到其他代码。你有使用的静态变量吗?

但是我可以给你建议减少你的内存消耗3倍。看起来你正试图存储一个rgb图像。问题是每个红色,绿色或蓝色像素只需要一个字节,但是你给它一个4字节的int。更好的方法是使用以下方法将红色,绿色和蓝色编码为单个int:int color = Color.rgb(red, geen, blue)这样,您只需将数组初始化为int[] worldgrid= new int[640][480],并将内存使用量减少一半3.

此外,您甚至可能根本不应将其存储为多维数组,只需执行int[] worldgrid = new int[640*480];并在索引到数组时,只需执行:int result = worldgrid[row + col*640];