有没有办法让 Android 进程在发生 OutOfMemoryError 时生成堆转储?
sun JVM 支持 -XX:+HeapDumpOnOutOfMemoryError
选项,以便在 java 进程耗尽堆时转储堆。
Android 上是否有类似的选项可以使 Android 应用程序在 OutOfMemoryException 上转储堆?手动使用 DDMS 时,尝试正确计时可能很困难。
The sun JVM supports a -XX:+HeapDumpOnOutOfMemoryError
option to dump heap whenever a java process runs out of heap.
Is there a similar option on Android that will make an android app dump heap on an OutOfMemoryException? It can be difficult to try to time it properly when using DDMS manually.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
扩展 CommonsWare 的答案:
我在自己的 Android 应用程序中成功遵循了他的建议,代码如下:
创建转储后,您需要将其从手机复制到 PC:单击手机上的“打开 USB 存储”,找到文件并复制到您的硬盘。
然后,如果您想使用 Eclipse 内存分析器 (MAT) 来分析该文件,则需要隐藏该文件:
hprof-conv.exe dump.hprof dump-conv.hprof
(hprof- conv位于android-sdk/tools
下)最后,用MAT打开
dump-conv.hprof
文件To expand upon CommonsWare's answer:
I followed his suggestion successfully in my own Android app with the following code:
After the dump is created, you need to copy it from your phone to your PC: Click "Turn on USB storage" on the phone, find the file and copy it to your hard drive.
Then, if you want to use the Eclipse Memory Analyzer (MAT) to analyze the file, you will need to covert the file:
hprof-conv.exe dump.hprof dump-conv.hprof
(hprof-conv is located underandroid-sdk/tools
)Finally, open the
dump-conv.hprof
file with MAT这是一个改进版本。除了原始实现之外,此实现还支持:
用法:在 中调用 Application 类中的静态
initialize
方法onCreate 方法。Here is an improved version. On top of the original implementation this implementation also supports:
Usage: Call the static
initialize
method in your Application class in the onCreate method.我不知道这是否有效,但您可以尝试 添加顶级异常处理程序,并在其中 如果是
OutOfMemoryError
,则请求堆转储。I have no idea if this works, but you might try adding a top-level exception handler, and in there asking for a heap dump if it is an
OutOfMemoryError
.