ANTS Memory Profiler - 7.4

ANTS Memory Profiler

4. Finding out what is using most memory - ANTS Memory Profiler

This topic describes how to find the classes that are using the most memory in your application.

Before finding out what is using most memory, we recommend that you first check for large object heap fragmentation and use of unmanaged memory.

If you are not familiar with memory profiling, you might find it helpful to read the .NET Memory Primer before you start.

Tip: You should not normally filter classes that you do not recognize. For example, if your code contains an array which itself contains a large number of byte arrays, you should start by looking at those byte arrays.

  1. Start ANTS Memory Profiler and start profiling your application.
  2. Use your application until the point where you are interested in its memory. For example, if you believe that a lot of memory is used when a certain screen is displayed, use the application normally until the screen is shown.
  3. Click amp_takesnapshot_button.
  4. On the Summary, look at Largest classes. The largest classes are shown in order of the amount of memory they use.

    summary_largestclasses

  5. Starting with the largest class, click each class in turn to see the instance categorizer for that class. For this analysis, you are exploring memory usage by following references to a class, so it is not important if you do not recognize these classes.
  6. Instances of the chosen class are grouped by the chain of references which holds them in memory, with the largest group of instances displayed at the top of the screen. Starting with the largest category, use the explorer to look at what is keeping your selected class in memory, reading the graph from right-to-left.

    For example, System.String is often the largest class; using the instance categorizer you can find out what is keeping strings in memory, such as an event handler.

  7. If the largest class does not reveal interesting results, click Summary, and try again with the next-largest class, and so on.
  8. If you still cannot see why large amounts of memory are being used after you have examined all of the largest classes by size, click Other classes. The Class List is displayed. Sort the Class List by Live Instances, and look at the instance categorizer for the classes with the most instances.

After using the instance categorizer to find out why instances of a class are still in memory, you might find that some instances are being kept in memory by a type (most commonly an event handler) that should not be referencing those objects. If this happens, when viewing the instance categorizer:

  1. Click Show instances on this path.
  2. The Instance List is displayed, showing the instances of the object which are retained on the path previously shown in the instance categorizer.
  3. Click on any instance to select it (it doesn't matter which one, because they are all on the same shortest path) then click icon_object graph to display the instance retention graph.
  4. Look for the reference that should not be there on the instance retention graph, which shows all paths holding the object in memory.
  5. Change your code to remove the incorrect reference, and then check that the problem is fixed.

Worked example

In this worked example, ANTS Memory Profiler is used to find out what uses most memory in Red Gate's Exception Hunter:

  1. The Byte[] class is the largest class, accounting for 73.74MB of managed memory used by the application.

  2. Click Byte[].
  3. The Instance Categorizer shows that most instances of Byte[] (totaling 23.9MB) are held on a path where the shortest route to a GC root object leads to an object called cB. On the Byte[] node on this path, click Show instances on this path.

  4. The Instance List lists the 46 objects on this path. For instances of System.String it might be interesting to use the Instance List to look at the values of the strings, but this is unlikely to be the case for Byte[] instances, so choose any one and click icon_object graph.

    (All of the instances in the Instance List are all on the same shortest path, so it should make little difference which one you choose. Selecting the instance with the greatest size with children is often a sensible start, however.)

  5. The Instance Retention Graph shows which other types keep this instance in memory.

Conclusions:

  1. The Byte[] class accounts for most memory used by this program (73.74MB).
  2. The largest number of objects from the Byte[] class (46 instances, totaling 23.9MB) are held on a path leading back to a GC root object from the class cB.
  3. The selected instance is being kept in memory by cv, which in turn is being kept in memory by both cB and cB+e.
  4. You now need to decide whether this should be the case, or whether there is a problem.

Was this article helpful?

Search support
Forums

ANTS Memory Profiler

all products