如何分析 Dalvik GC 行为?

发布于 2024-10-09 22:20:53 字数 386 浏览 0 评论 0原文

我正在 Android 上开发一个应用程序。它是一个长时间运行的应用程序,持续处理传感器数据。运行应用程序时,我在 logcat 中看到很多 GC 消息;大约每秒一个。

这很可能是因为在循环中创建对象并立即取消引用。

如何找到哪些对象正在立即创建和释放?

我尝试过的所有 java 堆分析工具(*) 都关心堆上对象的数量和大小。虽然它们很有用,但我更感兴趣的是找出创建 temporary 短期对象最多的站点。

(*) 我尝试了 jcatEclipse MAT。我无法让 hat 处理 Android 堆转储;它抱怨转储文件版本不受支持。

I am developing an application on Android. It is a long running application that continuously processes sensor data. While running the application I see a lot of GC messages in the logcat; about one every second.

This is most probably because of objects being created and immediately de-referenced in a loop.

How do I find which objects are being created and released immediately?

All the java heap analysis tools that I have tried(*) are bothered with the counts and sizes of objects on the heap. While they are useful, I am more interested in finding out the site where temporary short-lived objects get created the most.

(*) I tried jcat and Eclipse MAT. I couldn't get hat to work on the Android heap-dumps; it complained of an unsupported dump file version.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

踏月而来 2024-10-16 22:20:53

如何找到正在立即创建和释放的对象?

步骤#1:暂时修改您的代码(或使用代码的相关部分创建一个废弃项目),您可以在其中单击按钮或其他内容以通过传感器处理逻辑运行一次。

步骤#2:进入 DDMS(独立或 Eclipse 透视图)。

步骤#3:选择您的模拟器,然后单击“分配跟踪器”选项卡

步骤#4:让您的应用程序等待步骤#1 中单击按钮,然后单击“DDMS 分配跟踪器”选项卡中的“开始跟踪”。

步骤 #4:单击 按钮,传感器处理过程完成后,单击 DDMS 分配跟踪器选项卡上的获取分配。

这将告诉您在代码的该部分期间分配了什么。它不会告诉您什么是“释放”的,因为在 GC 周期运行之前这是不确定的。

编辑

我不确定,但是 android.os.Debug 类上的 startAllocCounting() 可能与单击“开始跟踪”按钮具有相同的效果。如果是这样,您可以简单地检测代码来跟踪循环的一次分配,而不是搞乱我上面概述的代码更改。

而且,FWIW,这是一篇关于 DDMS 和分配跟踪的简短技术文章

How do I find which objects are being created and released immediately?

Step #1: Temporarily modify your code (or create a scrap project with the relevant portions of your code), where you can click a button or something to run exactly once through the sensor processing logic.

Step #2: Get into DDMS (standalone or the Eclipse perspective).

Step #3: Choose your emulator, then click on the Allocation Tracker tab

Step #4: Get your application to the point where it is waiting for the button click from step #1, then click on Start Tracking in the DDMS Allocation Tracker tab.

Step #4: Click the button, and when the sensor processing pass is complete, click Get Allocations on the DDMS Allocation Tracker tab.

This will tell you what was allocated during that portion of your code. It does not tell you what is "released", because that's indeterminate until a GC cycle runs.

EDIT

I am not certain, but startAllocCounting() on the android.os.Debug class may have the same effect as clicking the Start Tracking button. If so, you could simply instrument your code to track allocations over one pass of your loop, rather than mess around with the code changes I outlined above.

And, FWIW, here is a short technical article on DDMS and allocation tracking.

依 靠 2024-10-16 22:20:53

我认为你需要尝试一下分配跟踪器:)

(在你的 /tools 目录中)

http://developer.android.com/resources/articles/track-mem.html

I think you need to give allocation tracker a try :)

(in your /tools dir)

http://developer.android.com/resources/articles/track-mem.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文