Android 为什么在一个简单的应用程序中有这么多 GC_FOR_ALLOC?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20517908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:57:46  来源:igfitidea点击:

Why so many GC_FOR_ALLOC in a simple app?

androidperformancedalvik

提问by Rui Campi?o

I'm getting way too many GC_FOR_ALLOC from the dalvikvm. I'm getting XML from a REST service: in one activity I parse about 100 lines programatically(me) and in the other activity I use the SimpleXML to parse about 200 lines.

我从 dalvikvm 获得了太多 GC_FOR_ALLOC。我从 REST 服务获取 XML:在一个活动中,我以编程方式(我)解析了大约 100 行,而在另一个活动中,我使用 SimpleXML 解析了大约 200 行。

In the first one I get 50 GC_FOR_ALLOC. In the second one I get like 300!! (I can't even post it all, the body makes 29579 characters and it's allowed only 30k)

在第一个我得到 50 GC_FOR_ALLOC。在第二个中,我得到了 300 !(我什至不能全部发布,正文有 29579 个字符,并且只允许 30k)

I've searched and almost everyone complains about gc_for_"M"alloc and not gc_for_"A"lloc.

我搜索过,几乎每个人都抱怨 gc_for_"M"alloc 而不是 gc_for_"A"lloc。

Is the SimpleXML the problem because the instances created?

SimpleXML 是不是因为实例创建的问题?

I'll post the logcat dump by dalvikvm, maybe the values have some information.

我将通过 dalvikvm 发布 logcat 转储,也许这些值有一些信息。

Thank you very much for your help.

非常感谢您的帮助。

12-11 06:13:49.564: D/dalvikvm(6759): GC_FOR_ALLOC freed 362K, 13% free 4116K/4688K, paused 181ms, total 182ms
12-11 06:13:50.074: D/dalvikvm(6759): GC_FOR_ALLOC freed 303K, 13% free 4134K/4708K, paused 142ms, total 142ms
.... repeated many times .....
12-11 06:14:06.254: D/dalvikvm(6759): GC_FOR_ALLOC freed 73K, 13% free 4159K/4768K, paused 53ms, total 53ms
12-11 06:14:06.314: D/dalvikvm(6759): GC_FOR_ALLOC freed 103K, 13% free 4159K/4768K, paused 56ms, total 57ms
12-11 06:14:06.374: D/dalvikvm(6759): GC_FOR_ALLOC freed 29K, 12% free 4203K/4768K, paused 54ms, total 54ms
12-11 06:14:06.424: D/dalvikvm(6759): GC_FOR_ALLOC freed 73K, 13% fre

回答by fadden

You can see the most-recently-allocated objects using the DDMS Allocation Tracker (memory debugging docs, old blog post, ddms docs). This will show you what's being allocated and give you a stack trace for the place where the allocation is being performed.

您可以使用 DDMS 分配跟踪器(内存调试文档、旧博客文章ddms 文档)查看最近分配的对象。这将向您显示正在分配的内容,并为您提供执行分配位置的堆栈跟踪。

Another blog postdescribes MAT and other relevant tools, though heap-dump analysis is less useful for this sort of problem because it generally shows you the objects that haven'tbeen freed, and you're more interested in the objects that arebeing freed.

另一篇博客中介绍了MAT和其他相关工具,虽然堆转储分析是这类问题不太有用,因为它你一般表明该对象没有被释放,而你更感兴趣的是,对象被释放.

回答by QJGui

In Android Dalvik VM, GC_FOR_ALLOCis inovked in object alloc stepwhen dlmalloc footprint space is NOT enough for new or heap->bytesAllocated + n > hs->softLimit. You can set dalvik.system.setTargetHeapUtilizationlower for more free heap space.

在Android的Dalvik虚拟机,GC_FOR_ALLOC在inovked对象页头一步时dlmalloc占用的空间是不够的,新的或heap->bytesAllocated + n > hs->softLimit。您可以设置dalvik.system.setTargetHeapUtilization较低以获得更多可用堆空间。

回答by nitesh goel

you can use MAT MAT tutorial

您可以使用 MAT MAT 教程

to find how many object are creating and garbage collected. so that youcan optimize your code

找出有多少对象正在创建和垃圾收集。这样你就可以优化你的代码

回答by jonh

If you get that multiple GC_FOR_ALLOCwhile your app is lagging, there is a big possibility that the bug is in a loop. Check where the line of code starts to trigger the GC then start tracing the code from there. In my experience, I mistyped my inner loop iterator which causes the program to make an infinite loop. I created a bug like this:

如果您GC_FOR_ALLOC在应用程序滞后时获得该倍数,则该错误很有可能处于循环中。检查代码行从哪里开始触发 GC,然后从那里开始跟踪代码。根据我的经验,我打错了内部循环迭代器,导致程序无限循环。我创建了一个这样的错误:

for(int i=0; i<list.size(); i++) {
     for(int j=i+1 j<list.size(); i++) {

     // I mistyped the iterator of integer j with i
     // making an infinite loop which triggered the GC.
     //appears many times

     }
}

回答by ZhanZF

I encounter the same problem today. I find a not ended loop in my code such as while(i < xx), but I not implement the i++ statement in the while body. So the messages like you meet appeared. Check your code firstly please.

我今天遇到同样的问题。我在我的代码中发现了一个未结束的循环,例如 while(i < xx),但我没有在 while 主体中实现 i++ 语句。于是就出现了类似你遇到的消息。请先检查您的代码。

回答by Fortran

My log:

我的日志:

D/dalvikvm: GC_FOR_ALLOC freed 549K, 9% free 7878K/8596K, paused 30ms, total 34ms

...freed 539K, 9% free 7888K/8596K, paused 30ms, total 30ms
...freed 1856K, 21% free 8083K/10108K, paused 51ms, total 51ms
...freed 582K, 9% free 7845K/8596K, paused 38ms, total 38ms

Explain:

解释:

When your app get memory more limit per app. Dalvik/Ant call garbage collector.

当您的应用程序获得每个应用程序的内存限制时。Dalvik/Ant 调用垃圾收集器。

What limits memory for your App decide Dalvik/Ant. As you see for my app Dalvik decide 8596K(double case) and 8083K(one case).

应用程序的内存限制决定了 Dalvik/Ant。正如你在我的应用程序中看到的,Dalvik 决定 8596K(双例)和 8083K(一个例)。

And limits change in runtime.

并限制运行时的变化。

And you can not be sure when this happens. But you can reduce the likelihood. Decreasing the amount of memory that your application consumes.

您无法确定何时会发生这种情况。但是你可以减少这种可能性。减少应用程序消耗的内存量。

PS: Decide when call GC teakes Dalvik/Ant. And you can not be sure when this happens. But you can reduce the likelihood. Decreasing the amount of memory that your application consumes.

PS:决定何时调用 GC 茶 Dalvik/Ant。您无法确定何时会发生这种情况。但是你可以减少这种可能性。减少应用程序消耗的内存量。

PS: In "Monitor android" see tab "Monitors", graphics "Memory". And use buttons: "pause(enabled)", Initiate GC, "Dump Java Heap" "Start Alocation Tracking(very useful)". And use official guide for this:

PS:在“Monitor android”中看到“Monitors”标签,图形“Memory”。并使用按钮:“暂停(启用)”、启动 GC、“转储 Java 堆”、“启动定位跟踪(非常有用)”。并为此使用官方指南:

https://developer.android.com/studio/profile/am-memory.html?utm_source=android-studio.

https://developer.android.com/studio/profile/am-memory.html?utm_source=android-studio

As far as I understand App must not stop/pause working or crashes when VM call GC.

据我了解,当 VM 调用 GC 时,App 不得停止/暂停工作或崩溃