Android游戏编程 - 堆问题

时间:2011-08-19 17:44:36

标签: android heap dalvik libgdx

我在一家小型Android游戏开发公司工作,目前我遇到了问题。我正在运行一个非常标准的Windows 7 x64机器,使用Eclipse和Android SDK。

我正在阅读我的程序的LogCat输出,想知道为什么我的游戏甚至都不会出现。我很快看到所有的堆都在增长,并且认为我可能有太多的纹理/太大的纹理等。我确定了纹理加载的问题,然后我开始使用DDMS并注意到我的堆不会超过19MB。我剪了几个纹理,试图再次构建和运行。令我震惊的是,堆不会一直超过6.6MB。有没有人有什么建议?

tl; dr:我的堆增长到19MB然后失败了,所以我拿出了内存更昂贵的纹理,使其在堆中保持在19MB以下,现在我的堆不会超过6.6MB

编辑:当取出更大的纹理时,logcat输出:

08-19 19:19:53.744: DEBUG/dalvikvm(4699): GC_FOR_MALLOC freed 91 objects / 514016 bytes in 49ms
08-19 19:19:54.184: DEBUG/dalvikvm(4699): GC_FOR_MALLOC freed 104 objects / 494088 bytes in 48ms
08-19 19:19:54.404: WARN/ActivityManager(59): Launch timeout has expired, giving up wake lock!
08-19 19:19:54.764: DEBUG/dalvikvm(4699): GC_FOR_MALLOC freed 85 objects / 384664 bytes in 49ms
08-19 19:19:54.935: WARN/ActivityManager(59): Activity idle timeout for HistoryRecord{4612eec8 com.wickeyware.zombiearcher.android/.AndroidGame}
08-19 19:19:55.234: DEBUG/dalvikvm(4699): GC_FOR_MALLOC freed 104 objects / 504648 bytes in 47ms
08-19 19:19:55.875: DEBUG/dalvikvm(4699): GC_FOR_MALLOC freed 85 objects / 505712 bytes in 55ms
08-19 19:19:56.905: DEBUG/dalvikvm(4699): GC_FOR_MALLOC freed 104 objects / 522392 bytes in 50ms
08-19 19:19:57.535: DEBUG/dalvikvm(4699): GC_FOR_MALLOC freed 56 objects / 388896 bytes in 50ms
08-19 19:19:57.695: INFO/WindowManager(59): WIN DEATH: Window{4606a8a0 com.android.launcher/com.android.launcher2.Launcher paused=false}
08-19 19:19:57.705: INFO/ActivityManager(59): Process com.android.launcher (pid 4669) has died.
08-19 19:19:57.825: INFO/ActivityManager(59): Low Memory: No more background processes.
08-19 19:19:57.865: INFO/dalvikvm(4699): dvmDdmHandleHpsgChunk(when 1, what 0, heap 0)
08-19 19:19:58.575: DEBUG/ddm-heap(4699): Heap GC request
08-19 19:19:58.685: DEBUG/dalvikvm(4699): GC_EXPLICIT freed 48 objects / 313440 bytes in 106ms
08-19 19:20:01.015: INFO/ActivityManager(59): Process jp.co.omronsoft.openwnn (pid 4677) has died.
08-19 19:20:01.095: WARN/ActivityManager(59): Scheduling restart of crashed service jp.co.omronsoft.openwnn/.OpenWnnJAJP in 5000ms
08-19 19:20:01.106: INFO/ActivityManager(59): Low Memory: No more background processes.
08-19 19:20:03.105: INFO/ActivityManager(59): Process com.wickeyware.zombiearcher.android (pid 4699) has died.
08-19 19:20:03.155: INFO/WindowManager(59): WIN DEATH: Window{46138b68 SurfaceView paused=false}
08-19 19:20:03.275: INFO/WindowManager(59): WIN DEATH: Window{4612d530 com.wickeyware.zombiearcher.android/com.wickeyware.zombiearcher.android.AndroidGame paused=false}
08-19 19:20:03.425: INFO/ActivityManager(59): Start proc com.android.launcher for activity com.android.launcher/com.android.launcher2.Launcher: pid=4710 uid=10025 gids={}
08-19 19:20:03.425: INFO/ActivityManager(59): Low Memory: No more background processes.
08-19 19:20:03.775: INFO/UsageStats(59): Unexpected resume of com.android.launcher while already resumed in com.wickeyware.zombiearcher.android
08-19 19:20:03.904: INFO/ActivityThread(4710): Publishing provider com.android.launcher2.settings: com.android.launcher2.LauncherProvider
08-19 19:20:04.005: DEBUG/dalvikvm(4710): GC_EXTERNAL_ALLOC freed 985 objects / 73440 bytes in 58ms
08-19 19:20:04.465: WARN/InputManagerService(59): Got RemoteException sending setActive(false) notification to pid 4699 uid 10036
08-19 19:20:04.984: INFO/ActivityManager(59): Displayed activity com.android.launcher/com.android.launcher2.Launcher: 1703 ms (total 20584 ms)
08-19 19:20:06.124: INFO/ActivityManager(59): Start proc jp.co.omronsoft.openwnn for service jp.co.omronsoft.openwnn/.OpenWnnJAJP: pid=4718 uid=10023 gids={}

2 个答案:

答案 0 :(得分:1)

GC_FOR_MALLOC freed 104 objects / 504648 bytes in 47ms

这是垃圾收集器开火。不幸的是,没有任何消息来源,这里的任何人都无法帮助你。

但是,看起来你正在实例化WAY太多的对象而且垃圾收集器几乎不断射击。

答案 1 :(得分:0)

您好。我不太确定我的答案是否正确,但我遇到了类似的问题。

问题在于为某个对象创建新纹理。我做了太多次,结果是一个类似的例外。我的解决方案:在我为对象设置新纹理之前,我删除了旧纹理。

例如:

// Delete a texture
int[] textures = { textureId };
gl.glDeleteTextures(1, textures, 0);

// Create new texture ...

或者:

int oldTextureId = textureId;
...
// Create new texture
...
// Delete texture
int[] textures = { oldTextureId };
gl.glDeleteTextures(1, textures, 0);