Can I recognize all the objects in the garbage collector generation?

I was struck by the thought that it might be interesting if the application can scan which objects constantly hang around in its generation 2 or a large heap of objects in the garbage collector and see if it can detect things that linger around for a really long time / forever. The general idea is that the application can identify potential objects that are resource leaks if it was the same for several collections (tracking them with weak links, so the profiling action does not hold it). I can find ways to ask what generation a particular object is in, and I can find an API for unmanaged code or debugging tools to examine managed heaps, but I really want this managed call to give me some kind of data structure with all the objects in the specified generation.

Do I have any hope of finding one, or am I looking for something that does not exist?

It could theoretically be possible to lay out an instance of a debugger application and analyze the results or something like that, but I would like it to work on live web servers during low load, and I'm not sure if ops would like me to connect debugger, even if it was possible :)

+4
source share
1 answer

While working with similar tools recently in Objective-C, you might want to find a heap tool. The heapshot tool will take snapshots of your heap, build a memory graph, and try to figure out which one is rooted and where. Most of this is similar to how the garbage collector detects which objects to collect.

In general, the heap tool allows you to take pictures of your heap at different times and compare which memory is rooted and which objects occupy this space. Mono Heapshot https://github.com/mono/heap-shot seems like a good starting point here, although I have not used it personally. I have had good results with JetBrains dotTrace memory in the past. Unfortunately, both of these tools do not show you the generations in which the object lives, but they can track the identification of the object, sometimes even in snapshots. If you find that an object survives from several collections, it is likely to live in a higher generation. The exact generation is implementation, runtime, and environment, though.

Memory profiles often exist. A very good tool for the Microsoft CLR is the WinDbg and SOS extensions. There is a good msdn magazine article here: http://msdn.microsoft.com/en-us/magazine/cc163528.aspx , and I found Tess from (wonderfully titled) "If it is broken, fix it you should" the blog has great content also. http://blogs.msdn.com/b/tess/

Some general information about your heap structure and GC generations can be obtained using the set of performance counters documented at http://msdn.microsoft.com/en-us/library/x2tyfybc.aspx .

+2
source

Source: https://habr.com/ru/post/1382595/


All Articles