View Real-time Heap Utilization

The garbage collector is optimized for a scenario in which all allocated instances are short-lived, except for instances that are allocated at the beginning of the application. If the allocations and instances’ lifetimes follow this scenario, the time used for garbage collections will be minimized, and the heap utilization will be more efficient. If they do not follow this scenario, it might result in more time being spent garbage collecting and in less-efficient utilization of memory.

To view how efficient the heap utilization is, the real-time data collection feature of the profiler can be used. To view the heap utilization, perform the following steps:

1.  Make sure that real-time data are collected using the Collect real-time data setting in the Profiler Settings form.

2.  Start profiling the application, service, or ASP.NET using the procedure described under the Profile a .NET Process section.

3.  Select the Real-time page and add plots for Total bytes and Live bytes, by checking the checkboxes in the Statistics view.

4.  Now, you can view how the heap is utilized while the profiled process is running.

5.  Compare the real-time graph with the two graphs below. Preferably, the real-time graph should be similar to the first of the two graphs below. If it is similar to the second graph, then an analysis of the lifetime of instances is recommended.


This graph shows a scenario that is optimal for the garbage collector; only generation #0 collects are needed to keep the Total bytes close to the Live bytes, and the heap utilization is very efficient. The overhead is only about 200 Kbytes.

 


This graph shows a scenario that is far from optimal for the garbage collector. Generation #0 and generation #1 collections fail to remove almost all instances on the heap, even though the number of live instances is almost constant. This forces time-consuming generation #2 collects to be performed in order to reduce the memory used.  Additionally, the heap utilization is very bad, and only 10 Mbytes of memory contain live (reachable) instances, while up to 30 Mbytes of memory are used by the heap.

Some things to avoid to reduce the lifetime of instances are:

      Allocating large short-lived instances. If an instance is placed in the large object heap, it will not be garbage collected until a generation #2 collection is performed.

      Referencing short-lived instances from long-lived instances. If an instance is not going to be used anymore, the reference should be set to null.

For more information about the real-time graph and real-time data collection, see Real-Time Page.

For more information on how to optimize memory usage, see the .NET Memory Profiler tutorials at: http://memprofiler.com/tutorials.

.NET Memory Profiler User Manual

© Copyright 2002-2019. SciTech Software AB.

For information about .NET Memory Profiler, see the product site at http://memprofiler.com

.NET Memory Profiler is developed by SciTech Software AB