A simple way to track memory usage in your Flex application is to create a Label that displays the System.totalMemory property and updates it on render events.

This isn’t anything fancy, but it’s useful when checking for memory leaks and helps you avoid making mistakes when dealing with memory intensive applications.

Also, its just kind of cool. 😎

You can even go a step further and create a button that allows you to manually perform garbage collection. This, coupled with the memory monitor, gives you the basic workings of a Flash Player memory management tool.

Just remember that System.totalMemory reports the total memory in use across ALL instances of Flash Player and not just the current one – so if you have other Flash or Flex applications open your usage patterns will be skewed!

Here’s some sample code for the label and button:

<mx:Label id="totalMemory"
    text="{(System.totalMemory / 1024) + ' KB'}"
    render="totalMemory.text = (System.totalMemory / 1024) + ' KB'" />

<mx:Button id="gcButton"
    label="force garbage collection"
    click="System.gc ()" />


Update: As pointed out in the comments, forcing a garbage collection only works when you’re using the debug player. (unless you use the hack linked to below)

Thanks espenskogen!