How can I profile the memory usage in a wp7.1 background agent?

background-agents, windows-phone-7, windows-phone-7.1

Solution

As has already been said, the debugger will add a non-negligible memory overhead. So the best way I found is:

Activate the emulator's console: http://nicksnettravels.builttoroam.com/post/2011/01/19/Windows-Phone-7-Console-Window-on-64-bit-machine.aspx

At strategic points of your code (beginning and end of the main functions for example, or after allocating a big object), display the memory consumption:

Console.WriteLine("Function {0}: {1}", "name of your function", DeviceExtendedProperties.GetValue("ApplicationCurrentMemoryUsage"));

Start the application without attaching the debugger (Ctrl + F5), sit back and enjoy

Problem

I'm working on a scheduled background agent for Mango (7.1) I've managed to cut my memory usage down from 10.5MB to 6.5MB using manual trace techniques based on `DeviceStatus.ApplicationPeakMemoryUsage` along with some intelligent guesswork about what to optimise. However, now I need to get down to below 6MB - and the final 0.5MB is proving elusive. I've managed to run the "Windows Phone Performance Analysis" tool on the main app, but can't seem to find a way to get it to run against the agent. Are there any tools and/or techniques available to assist me in working out what is filling up my agent's memory? Ideally I'd like a way to investigate the entire memory space - including both code and data, and ideally it should work against a release build.

Original source