admin管理员组文章数量:1194132
I have significant garbage collection pauses. I'd like to pinpoint the objects most responsible for this collection before I try to fix the problem. I've looked at the heap snapshot on Chrome, but (correct me if I am wrong) I cannot seem to find any indicator of what is being collected, only what is taking up the most memory. Is there a way to answer this empirically, or am I limited to educated guesses?
I have significant garbage collection pauses. I'd like to pinpoint the objects most responsible for this collection before I try to fix the problem. I've looked at the heap snapshot on Chrome, but (correct me if I am wrong) I cannot seem to find any indicator of what is being collected, only what is taking up the most memory. Is there a way to answer this empirically, or am I limited to educated guesses?
Share Improve this question edited Sep 22, 2012 at 22:04 j08691 208k32 gold badges267 silver badges280 bronze badges asked Sep 22, 2012 at 21:12 shinoshino 4,7345 gold badges42 silver badges57 bronze badges 5- 2 How can you be sure that the pauses are caused by garbage collection? – pencil Commented Sep 22, 2012 at 21:16
- Did you read this? gent.ilcore.com/2011/08/finding-memory-leaks.html?m=1 – yunzen Commented Sep 22, 2012 at 21:17
- 1 @pencil Two ways - they coincide with the drop in memory on the memory timeline in Chrome, and when I force the code to iterate heavily to induce the pauses constantly, garbage collection is (by far) the most time consuming part of my profiling results. – shino Commented Sep 22, 2012 at 21:20
- @yunzen No, I had not, thanks. I could be missing something, but I'm not sure it helps me. I'm confident I do not have a leak as my memory use is not growing over time. – shino Commented Sep 22, 2012 at 21:24
- Check out Google's web tracing framework google.github.io/tracing-framework/… and jankfree.org – RoryKoehein Commented Aug 26, 2013 at 22:25
4 Answers
Reset to default 9In chrome profiles takes two heap snapshots, one before doing action you want to check and one after.
Now click on second snapshot.
On the bottom bar you will see select box with option "summary". Change it to "comparision".
Then in select box next to it select snaphot you want to compare against (it should automaticaly select snapshot1).
As the results you will get table with data you need ie. "New" and "Deleted" objects.
With newer Chrome releases there is a new tool available that is handy for this kind of task:
The "Record Heap Allocations" profiling type. The regular "Heap SnapShot" comparison tool (as explained in Rafał Łużyński answers) cannot give you that kind of information because each time you take a heap snapshot, a GC run is performed, so GCed objects are never part of the snapshots. However with the "Record Heap Allocations" tool constantly all allocations are being recorded (that's why it may slow down your application a lot when it is recording). If you are experiencing frequent GC runs, this tool can help you identify places in your code where lots of memory is allocated. In conjunction with the Heap SnapShot comparison you will see that most of the time a lot more memory is allocated between two snapshots, than you can see from the comparison. In extreme cases the comparison will yield no difference at all, whereas the allocation tool will show you lots and lots of allocated memory (which obviously had to be garbage collected in the meantime).
Unfortunately the current version of the tool does not show you where the allocation took place, but it will show you what has been allocated and how it is was retained at the time of the allocation. From the data (and possibly the constructors) you will however be able to identify your objects and thus the place where they are being allocated.
If you're trying to choose between a few likely culprits, you could modify the object definition to attach themselves to the global scope (as list under document or something). Then this will stop them from being collected. Which may make the program faster (they're not being reclaimed) or slower (because they build up and get checked by the mark-and-sweep every time). So if you see a change in performance, you may have found the problem.
One alternative is to look at how many objects are being created of each type (set up a counter in the constructor). If they're getting collected a lot, they're also being created just as frequently.
Take a look at https://developers.google.com/chrome-developer-tools/docs/heap-profiling
especially Containment View
The Containment view is essentially a "bird's eye view" of your application's objects structure. It allows you to peek inside function closures, to observe VM internal objects that together make up your JavaScript objects, and to understand how much memory your application uses at a very low level.
The view provides several entry points:
DOMWindow objects — these are objects considered as "global" objects for JavaScript code; GC roots — actual GC roots used by VM's garbage collector; Native objects — browser objects that are "pushed" inside the JavaScript virtual machine to allow automation, e.g. DOM nodes, CSS rules (see the next section for more details.) Below is the example of what the Containment view looks like:
版权声明:本文标题:javascript - How can I determine what objects are being collected by the garbage collector? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738488561a2089568.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论