java 如何最小化 GC_FOR_ALLOC 的出现?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14088159/
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-10-31 15:01:22  来源:igfitidea点击:

How to minimize GC_FOR_ALLOC occurrences?

javaandroidmemory-managementgarbage-collectiondalvik

提问by srf

My application is causing these dreaded GC_FOR_ALLOC occur way to many times in specific locations (methods):

我的应用程序导致这些可怕的 GC_FOR_ALLOC 在特定位置(方法)多次发生:

12-29 22:20:30.229: D/dalvikvm(10592): GC_FOR_ALLOC freed 1105K, 14% free 10933K/12615K, paused 33ms, total 34ms
12-29 22:20:30.260: D/dalvikvm(10592): GC_FOR_ALLOC freed 337K, 13% free 11055K/12615K, paused 25ms, total 26ms
12-29 22:20:30.288: D/dalvikvm(10592): GC_FOR_ALLOC freed 278K, 14% free 10951K/12615K, paused 24ms, total 24ms
12-29 22:20:30.495: D/dalvikvm(10592): GC_CONCURRENT freed 633K, 11% free 11317K/12615K, paused 16ms+3ms, total 79ms
12-29 22:20:30.495: D/dalvikvm(10592): WAIT_FOR_CONCURRENT_GC blocked 16ms
12-29 22:20:30.499: D/dalvikvm(10592): WAIT_FOR_CONCURRENT_GC blocked 15ms

It's clear to me that I am doing something wrong in regard to memory management (yes, garbage collection is great but still doesn't free me from some responsibility to know when & how to allocate).

我很清楚我在内存管理方面做错了什么(是的,垃圾收集很棒,但仍然没有让我免于知道何时和如何分配的责任)。

Can you recommend a troubleshooting approach or techniquethat can lead me to the offending lines of code and possible solutions?

你能推荐一种故障排除方法或技术,可以引导我找到有问题的代码行和可能的解决方案吗?

回答by iTech

Android DDMS, which is part of Android development tools in eclipse, has very useful tools that you can use for profiling and memory allocation tracking.

Android DDMS 是 Eclipse 中 Android 开发工具的一部分,具有非常有用的工具,可用于分析和内存分配跟踪。

The below screenshot highlights some of these tools (all under DDMS), some tools need to be activated by clicking the corresponding button in the left panel (highlighted in red) and then you can see the values (some with graphs) in the right panel by selecting the tab you want to watch (highlighted in Blue)

下面的截图突出显示了其中的一些工具(都在 DDMS 下),一些工具需要通过单击左侧面板中的相应按钮(以红色突出显示)激活,然后您可以在右侧面板中看到值(一些带有图表)通过选择您要观看的标签(以蓝色突出显示)

Heap and Allocation Tracker will be very useful in your case.

堆和分配跟踪器在您的情况下非常有用。

eclipse screenshot showing profiling and memory allocation tools

显示分析和内存分配工具的 eclipse 屏幕截图