内存不足的错误重新启动应用程序时(Android版) [英] Out of memory error when restarting app (Android)

查看:504
本文介绍了内存不足的错误重新启动应用程序时(Android版)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作,使用5是围绕900x600的大图像的游戏。当我退出游戏(preSS后退按钮),然后尝试启动游戏,我得到以下错误:

I am working on an game that uses 5 large images that are around 900x600. When I exit the game(press the back button) and then try to launch the game, I get the following error:

12-29 15:59:16.633: E/AndroidRuntime(18642): FATAL EXCEPTION: GLThread 17
12-29 15:59:16.633: E/AndroidRuntime(18642): java.lang.OutOfMemoryError: (Heap  Size=20423KB, Allocated=3473KB, Bitmap Size=77KB)
12-29 15:59:16.633: E/AndroidRuntime(18642):    at org.andengine.opengl.util.GLHelper.getPixelsARGB_8888(GLHelper.java:165)
12-29 15:59:16.633: E/AndroidRuntime(18642):    at org.andengine.opengl.util.GLHelper.getPixels(GLHelper.java:41)
12-29 15:59:16.633: E/AndroidRuntime(18642):    at org.andengine.opengl.util.GLState.glTexImage2D(GLState.java:641)
12-29 15:59:16.633: E/AndroidRuntime(18642):    at org.andengine.opengl.texture.bitmap.BitmapTexture.writeTextureToHardware(BitmapTexture.java:120)
12-29 15:59:16.633: E/AndroidRuntime(18642):    at org.andengine.opengl.texture.Texture.loadToHardware(Texture.java:137)
12-29 15:59:16.633: E/AndroidRuntime(18642):    at org.andengine.opengl.texture.TextureManager.updateTextures(TextureManager.java:254)
12-29 15:59:16.633: E/AndroidRuntime(18642):    at org.andengine.engine.Engine.onDrawFrame(Engine.java:621)
12-29 15:59:16.633: E/AndroidRuntime(18642):    at org.andengine.opengl.view.EngineRenderer.onDrawFrame(EngineRenderer.java:105)
12-29 15:59:16.633: E/AndroidRuntime(18642):    at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1363)
12-29 15:59:16.633: E/AndroidRuntime(18642):    at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1118)

这是真的令人困惑,因为我卸载所有的纹理应用程序销毁。我知道这5个图像导致错误,因为当我跳过完全加载它们,我没有得到任何错误。为了确保所有纹理被卸载了,我装的纹理和右后卸载它们。然后我退出并重新启动游戏,但收到了同样的错误。纹理没有任何静态引用无论是。我还使用了MAT看到我使用多少内存在游戏中,发现我只是使用2.5MB,所以我不知道为什么错误是说堆大小= 20MB。我已成功地解决了错误暂时的唯一方法是包含

This is really perplexing since I unload all the textures before the application is destroyed. I know these 5 images are causing the error because when I skipped loading them altogether, I did not get any errors. To make sure all textures were unloaded, I loaded the textures and right after unloaded them. I then exited and relaunched the game but received the same error. The textures do not have any static references either. I also used the MAT to see how much memory I was using in game and noticed that I was only using 2.5MB, so I have no idea why the error is saying "heap size = 20MB." The only way I have managed to solve the error for the time being is to include

system.exit(0);

我知道我不应该使用的,但我完全没了主意。

which I know I shouldn't be using, but I am completely out of ideas.

编辑:我确信纹理都被卸载。我在游戏中的一个按钮,将卸载所有的纹理。当我pressed它所有的纹理去黑我以为卸下纹理。卸载纹理时我用的一般方法只是说

I made sure the textures were being unloaded. I had a button in game that would unload all the textures. When I pressed it all the textures went black which I assume unloaded the textures. The general approach I used when unloading textures was just to say

someTexture.unload();

其是andEngine方法。我不知道会发生什么不止于此。

which is an andEngine method. I'm not sure what happens beyond that.

编辑2:我加载了纹理的方法称为onCreateResources()和我卸载它们的onDestroy()。这些是仅有的两个地方,我操纵的纹理。

EDIT 2: I am loading the textures in a method called onCreateResources() and I unload them onDestroy(). Those are the only two places I manipulate the textures.

推荐答案

在垃圾回收器不会立即免费使用的对象的内存,所以如果你试图经过短暂的重新加载新的位图,天下没有免费的内存来使用该

The Garbage Collector doesn't free immediately unused object memory, so if you try to reload new bitmaps after a short, there is no free memory to use for that.

也许你应该使用的WeakReference:抛出一个OutOfMemoryException之前JVM必须释放WeakReference的对象。一个例子可以在这里找到: http://developer.android.com/培训/显示的位图/显示-bitmap.html

Maybe you should use WeakReference: JVM must free WeakReference objects before throwing an OutOfMemoryException. An example could be found here: http://developer.android.com/training/displaying-bitmaps/display-bitmap.html

这篇关于内存不足的错误重新启动应用程序时(Android版)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆