Using the snapshot API
Published 18 December 2012
You can use the API exposed by ANTS Memory Profiler to take snapshots from within your code. First, you need to edit your application's code to trigger the snapshot, then run the built application alongside ANTS Memory Profiler.
Take snapshots from your code to ensure that snapshots are taken precisely when you believe that a leak occurs, or if your application does not have an interface through which you can control it. The snapshot API lets you essentially automate the process of taking a snapshot.
If your application is a Silverlight browser application, you cannot use the snapshot API. (This is due to security restrictions in Silverlight.)
Editing your code to invoke the snapshot:
- Create a reference to RedGate.MemoryProfiler.Snapshot.dll (You can find a copy in %ProgramFiles%\Red Gate\ANTS Memory Profiler 7\)
- Call
RedGate.MemoryProfiler.Snapshot.TakeSnapshot()
whenever you want to take a snapshot. This method returnstrue
on success andfalse
on failure. To take a snapshot and give it a memorable name at the same time, provide a string argument to theTakeSnapshot
method, for example:RedGate.MemoryProfiler.Snapshot.TakeSnapshot("Clicked button")
.
We recommend that you contain the TakeSnapshot()
method within a try-catch
statement. This is because TakeSnapshot()
can raise exceptions, for example if it is called more than 5 times in 30 seconds.
Running your application
- Start ANTS Memory Profiler.
- Set up ANTS Memory Profiler to profile your application in the normal way.
- When you click , your application starts.
- Snapshots are taken automatically at the trigger points you defined in your code. If required, you can still use the button to take snapshots manually as well.
- Profiling results are shown in ANTS Memory Profiler.