Android 游戏内存管理
我的游戏在 Android 2.2 上的内存管理存在问题。 我可以列举几种可能导致这种情况的理论:
- 在非缓存想法上加载位图(我使用项目的位图对象加载它)
- 我没有使用 Bitmap.recycle()
- 因为我正在使用 OOP 设计,我只是使用 Iterator.remove() 而不是将其
- 清空 位图实际大小达到 300KB [滞后的主要原因]
- MediaPlayer 对象用于播放音频动画
- 游戏开始前内存开销太大 [一切已加载]
虽然可以通过调整我的一些编码风格来枚举上述项目,但我主要关心的是如何在 Android 2.2 上正确编码游戏而不导致手机上出现严重延迟。
- 我应该考虑什么类比?
- 我应该使用 OpenGL(但我的游戏只是 2D 加速计游戏)吗?
- 我是否应该只从数据库中获取我需要的对象(但这意味着在整个游戏过程中保持对表的访问处于开放状态)?
- 或者其他人,请发布您的答案。
非常感谢您的回复并提前致谢!
I have a problem regards the memory management of my game on Android 2.2.
There are several possible theories I could enumerate that could cause this:
- Loading bitmaps on a non-cache idea (I load it using the item's bitmap object)
- I didn't use the Bitmap.recycle()
- Because I'm using OOP design, I just using Iterator.remove() instead of nulling it
- Bitmap actual size reached 300KB [major cause of lag]
- MediaPlayer object was used to play audio animation
- Too much memory overhead before game-play [everything is already loaded]
Though enumerating the said items can be done by adjusting some of my style of coding it, my main concern is how can I code properly a game on Android 2.2 without causing the major lag on the phone.
- What analogy should I consider?
- Should I use OpenGL (but my game is only a 2D accelerometer game)?
- Should I only get the objects I need from the database (but that would mean leaving the access to the table open throughout the game)?
- Or others, kindly post your answer.
Your response is highly appreciated and thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如斯雷顿评论的那样,你的问题不是很集中。您似乎关心应用程序的内存占用和性能,但不确定从哪里开始。我建议分析您的应用程序。找出它大部分时间都花在处理或分配内存的地方。然后努力使这部分代码更加高效。
Android SDK 包含一个很好的分析工具,记录如下:
http://developer.android.com/guide/developing/debugging/debugging -tracing.html
As commented on by slayton, your question is not very focused. It appears that you are concerned with the memory footprint and performance of your app but unsure of where to start. I would recommend profiling your app. Find out where it spends most of its time processing or allocating memory. Then work on making that part of your code more efficient.
The Android SDK includes a nice profiling tool documented here:
http://developer.android.com/guide/developing/debugging/debugging-tracing.html
落后怎么办?如果它时不时地卡住,则可能是 GC,这意味着您需要检查您的分配。您可以使用 DDMS 进行检查。 (例如,当您使用 foreach 样式循环时,存在隐藏的分配。这些分配需要清理,并且 GC 将在代码中间运行。使用旧的
for(int i = 0; i < arraySize; i++)
可以防止这种情况。)另一方面,如果它是恒定的滞后(触摸屏幕,等待约 200 毫秒以获得响应),则可能是操作的顺序,尤其是任何绘制操作。将
Bitmap
绘制到Canvas
的速度很慢,使用 OpenGL 可能会有所帮助。您可能还需要重组,以便任何输入在下一次绘制时都有视觉响应。无论如何,如果您遇到特定问题,它都会有所帮助。任何代码、分析测量等都可以帮助我们找到更好的答案。
Lagging how? If it stutters every now and then, it could be the GC, meaning you need to check your allocations. You can use DDMS to check. (For example, there are hidden allocations when you use a foreach style loop. Those allocations need to be cleaned up, and the GC will run right in the middle of your code. Using the old
for(int i = 0; i < arraySize; i++)
prevents that.)On the other hand, if it's constant lag (touch the screen, wait ~200ms for response) it could be the order of operations, especially any draw operations. Drawing
Bitmap
toCanvas
is slow, and using OpenGL may help. You may also need to restructure so that any input has a visual response on the very next draw.In any case, it helps if you have a specific problem. Any code, profiling measurements, etc help us come up with better answers.