Android 开发:surfaceview 上的位图泄漏内存或太大?
我的游戏使用surfaceview(我知道我应该使用GL)。 我为我的游戏角色世界控制等绘制了很多位图。当我打开 LogDog 时,我遇到了这个问题:
08-05 10:17:29.151: ERROR/dalvikvm(24048): Out of memory: Heap Size=5379KB, Allocated=2735KB, Bitmap Size=20576KB, Limit=32768KB
我不知道它是泄漏还是什么。
我的分配跟踪器显示:
像 30:
138 96 char[] 9 android.content.res.AssetManager getCookieName
然后 大量:
32 80 android.graphics.BitmapFactory$Options 9 android.graphics.BitmapFactory decodeResource
最后: 像30:
141 56 android.graphics.Bitmap 9 android.graphics.BitmapFactory nativeDecodeAsset
还有一些类似的。
这是一些我认为会耗尽我记忆的代码:
player = BitmapFactory.decodeResource(getResources(), R.raw.ghostright);
world = BitmapFactory.decodeResource(getResources(), R.raw.lvl2);
thumb = BitmapFactory.decodeResource(getResources(), R.raw.thumb);
resized = Bitmap.createScaledBitmap(player, width/10, width/6, false);
player = resized;
resized = Bitmap.createScaledBitmap(world, height*10, height, false);
world = resized;
resized = Bitmap.createScaledBitmap(thumb, height/6, height/6, false);
thumb = resized;
我听说我应该使用 resycle 但我不知道在哪里,因为我总是使用位图
//Simon PS:我真的需要帮助-.-
My game uses surfaceview(I know I should use GL).
I draw alot of bitmaps to my game character world controll so on. And i run into this when I opened my LogDog:
08-05 10:17:29.151: ERROR/dalvikvm(24048): Out of memory: Heap Size=5379KB, Allocated=2735KB, Bitmap Size=20576KB, Limit=32768KB
I dont know if it is leak or what.
My Allocation Tracker shows:
like 30:
138 96 char[] 9 android.content.res.AssetManager getCookieName
Then
Tons of:
32 80 android.graphics.BitmapFactory$Options 9 android.graphics.BitmapFactory decodeResource
And last:
Like 30:
141 56 android.graphics.Bitmap 9 android.graphics.BitmapFactory nativeDecodeAsset
And also some more of simular ones.
Here is some code that I think drains my memory:
player = BitmapFactory.decodeResource(getResources(), R.raw.ghostright);
world = BitmapFactory.decodeResource(getResources(), R.raw.lvl2);
thumb = BitmapFactory.decodeResource(getResources(), R.raw.thumb);
resized = Bitmap.createScaledBitmap(player, width/10, width/6, false);
player = resized;
resized = Bitmap.createScaledBitmap(world, height*10, height, false);
world = resized;
resized = Bitmap.createScaledBitmap(thumb, height/6, height/6, false);
thumb = resized;
I heard that I should use resycle but I dont know where because I always use the bitmaps
//Simon
PS: I really need help -.-
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在 SurfaceView 上也使用了很多位图,但没有这个问题。
当涉及动画精灵时,您可以使用精灵表而不是逐帧单独加载它们。
您不需要使用“调整大小”的参考,您只需说:
旧的位图将失去其引用并被 GC 收集。请注意,在重新缩放以获得更好的质量时,我将 TRUE 用于位图过滤。
在某些设备上,onSizeChanged 可能会发生两次,如果您正在进行缩放,则可能会调整位图大小两次。
加载位图的格式并不重要,无论是 ARGB_4444 还是 ARGB_8888 等,因此您可能需要探索此选项,以及是否可以使用需要较少内存但具有足够好的游戏质量的格式。当然,规则是不要在需要时将大于所需的图像加载到内存中。
I use a lot of bitmaps on SurfaceView too and don't have this problem.
When it comes to animated sprites, you can use a sprite sheet rather than load them individually frame by frame.
You don't need to use the reference "resized" you can just say:
the old bitmap will lose its reference and be collected by GC. Note that I placed TRUE for bitmap filtering when rescaling to make a better quality.
On some devices onSizeChanged can happen two times which may resize bitmaps twice, if that is where you are doing your scaling.
The format of loaded bitmaps does metter whether it is ARGB_4444 or ARGB_8888 etc So you may need to explore this option and if you can use a format which requires less memory yet it has a good enough quality for your game. Of course the rule is not to load images into memory bigger than they are needed and when they are needed.
它不一定是内存泄漏,可能只是您有太大的位图,以至于它们想要分配大量内存。这是确定位图将占用多少内存的好方法:W*H*8。因此,如果您有一个 300*300 像素的位图,则为 300*300*8 = 720 kb。
找出在任何给定时间分配了多少堆,并查看它是否随着时间而增加,即使您知道没有分配新的位图。如果是这样,那么是的,你有内存泄漏。但是,如果您的应用程序在启动时崩溃,那么您可能刚刚超出了堆限制。
It dosen't have to be a memory leak, it could just be that you have so large bitmaps that they want to allocate to much memory. Here's a good method to determine how much memory a bitmap is going to take: W*H*8. So if you have a bitmap that's 300*300 px it's 300*300*8 = 720 kb.
Figure out how much allocated heap you have at any given time and see if it increases with time even though you know that you're not allocating new bitmaps. If so, then yes, you have a memory leak. If, however, your app crashes right on startup, then you are probably just exceeding heap limit.