First aid for detecting memory leaks in a Windows Store App

Before submitting your Windows 8 app to the Windows Store, it never hurts checking for memory problems. This article is about detecting these problems in an early stage of development. I’ll show you a super easy trick to do this without the need for memory profilers

memory leak The solution is to put a TextBlock on each page that shows the memory currently in use. The good thing is that after a while you get an understanding about how much memory your app “should use”. If this changes suddenly, you are in a good position to guess why it happened and the solution is only one Git-action away.

Showing memory usage on your page

<TextBlock TextWrapping="Wrap" x:Name="mem" FontSize="21.333"/>
mem.Text = "Memory in use: " + Math.Round(GC.GetTotalMemory(true) / 1000000.0, 2) + "mb";

Now see what happens if I navigate between 2 pages in my app. Watch the MEMORY USE text in the left upper corner:


Clearly my app has a major memory leak. If memory usage increases with 1 MB each time you navigate, older pages are probably not properly disposed of.

Meten is weten!

Indicating memory leaks is as simple as adding 2 lines of code to each page. It will help you to get a good idea of memory usage regardless of problems.