Performance is a regard for all though the many pardonable of applications, and in a compelled sourroundings identical to the phone it is all the some-more so; opening issues without delay perceptible themselves in the user experience. Memory operate is a single such source of opening issues which can reduce user experience, and the Windows Phone Marketplace enforces a technical acceptance sequence which an focus contingency not surpass 90 MB of RAM usage, solely on inclination which have some-more than 256 MB of memory.
What could means 90 MB of operate in a garbage-collected runtime? There could be multiform causes:
- Allocating as good much.
- Long vital objects which grow operative set.
- Holding on to references unintentionally heading to mental recall leaks.
Applications allocating as good most could finish up with operative sets breaching this threshold; applications with a vast operative set competence in the future exit with an “Out of Memory” exception; and land on to references unintentionally can lead to mental recall leaks causing operative set to enlarge usually and stealthily, in the future heading to the focus exiting with an “Out of Memory” exception.
Up until the Windows Phone SDK 7.1 release, focus developers had to ensure opposite this by programmatically instrumenting their formula with specific horizon process calls and lane mental recall usage. These methods enclosed the following:
- System.GC.GetTotalMemory
- Microsoft.Phone.Info.DeviceExtendedProperties.DeviceTotalMemory
- Microsoft.Phone.Info.DeviceExtendedProperties.ApplicationCurrentMemoryUsage
- Microsoft.Phone.Info.DeviceExtendedProperties.ApplicationPeakMemoryUsage
But over which there was small assistance from the tools.
The Windows Phone SDK 7.1 addresses this by the key to profiling collection to examine the execution, visual, and mental recall characteristics of an application.
Install the SDK now, and let us travel by the Memory Profiler as we examine a box of a mental recall trickle caused by unintentionally land on to references.
A Sample Application
Consider this focus which has 2 pages, and where the second page renders multiform images inside of a grid, and where a single might navigate in in in between pages.
Page2 has 4 images controls inside of a grid carry out which is bitmapcached. During the initialization of Page2, it holds the 4 picture controls to jpeg images and registers for an eventuality handler.
The 2 pages demeanour as follows:
![]()
As we try out the focus starting behind and onward in in in between the pages a couple of times, comply which mental recall operate keeps starting up; there is a mental recall leak.
Visualizing the leak
To excavate deeper in to this trickle serve let us run this focus by the Memory Profiler regulating the following steps:
- Invoke the Profiler, name “Memory (managed intent grant and hardness usage)”, and launch the application. Note the mental recall used by the focus at the tip of the screen.
- Click on Go To Page2. This will launch Page2, and arrangement the 4 flowering plant images.
- Click on the behind button.
- Click on ‘Force GC’. Note the mental recall used by the focus at the tip of the screen. Observe which it does not go down.
- Repeat stairs 2, 3, 4 4 some-more times.
- Now click on ‘Stop Profiling’.
We should see a graph identical to the following:
![]()
As approaching mental recall operate is usually growing.
Analysis
Select a range, from prior to the begin of the initial mountainous country to only after the after the begin of the final plateau, for analysis. Once research is complete, regulating Performance Warnings as the starting point, navigate to the Heap Summary view. We should see a list identical to the following:
![]()
Note which the mental recall reason by Retained Silverlight Visuals at End is vast as compared to which reason by only managed objects. So, let’s cavalcade down in to these defended Silverlight visuals.
Select Retained Silverlight Visuals at End and navigate to the Types view. In the Types view, arrange the Total Size (Bytes) column in forward order. We should right divided see a list identical to the following:
![]()
Note which Grid and Page2 have been the complicated consumers. We additionally notice right divided which 5 instances of Page2 have been unresolved around. In the scenario, we navigated 5 times to Page2, remember?
Let’s demeanour at what those 10 instances of the Grid are: click on the Grid and navigate to the Instances view. We should right divided see a list identical to the following:
![]()
Note which 5 of the instances have been being allocated from Page2. Click on the hyperlink and we can go the line of formula in XAML where the Grid is allocated.
To see what is the mental recall being defended by the visible tree secure at a Grid instance, click on, say, the grid with the ID 33 (this is a grid allocated from Page2), and navigate to the Visual Tree view. We should right divided a list identical to the following:
![]()
Note the visible tree secure at which grid instance. We can see the 4 contained picture controls, as good as the Texture mental recall compared with the grid.
To cranky check the organisation in in in between the grid example and Page2, we can go behind to the Types view, name Page2, and navigate to the Instances view. We should afterwards see a list identical to the following:
![]()
Notice which there have been 5 instances combined and nothing of them has been broken (i.e. rubbish collected).
Now let’s demeanour at the Visual Tree secure at a Page2 instance. Select the initial instance, and navigate to the Visual Tree. We should see a list identical to the following:
![]()
Again, we see the grid, the compared hardness memory, and the contained picture controls. Earlier we saw which 5 instances of Page2 were unresolved around. That trickle was causing the compared grid, images, and hardness mental recall to not get spotless up.
Now let us see because the Page2 instances were unresolved around.
Go behind to the Instances view, name the initial Page2 instance, and navigate to the GC Roots view. Collapse the tree structures. We should see a list identical to the following:
![]()
Ignore the diseased references (these do not forestall the GC from behaving on the references). But demeanour at the TouchFrameEventHandler; enhance it and we should see a list identical to the following:
![]()
There have been 5 cumulative eventuality handlers! The initial a single was allocated from the assembly. The rest were cloned inside of mscorlib, as any page appended the own TouchFrameEventHandler (the eventuality handler is implemented as a .Net Delegate type). That eventuality handler is land a anxiety to the page which in spin is preventing the page from being rubbish collected.
So the eventhandlers references the Page2 instances, preventing it from being GC’d; and to illustrate resources contained in Page2 have been not spotless up (the grid, compared hardness memory, images).
Solved!
All which stays to be finished is to find where which handler is being purebred in the assembly.
Go behind to Heap Summary, name Retained Allocations at End, navigate to the Types view, arrange by Type Name column, and corkscrew down to repair up the TouchFrameEventHandler. We should see the following:
![]()
To find out where which 1 example was allocated in the focus code, name the initial entry, and navigate to the Methods view. You should see the following:
![]()
Click on the Allocating Method hyperlink, and we will burst to the process which did the allocation. You should see the following:
![]()
There, right at the bottom is the place where we register to the eventhandler! The step of induction stashes divided a clever anxiety to this example of the page inside of the eventuality handler delegate. Unless the user unregisters this handler, the Page2 example will sojourn alive (and hence so as good will the grid and the images, and the hardness mental recall will not get spotless up).
If we criticism out the line which enregisters the handler, and rerun this scenario, here is the graph we would see:
![]()
Voilà!
Conclusion
Having a rubbish picked up runtime removes a single of the greatest sources of module errors, mental recall grant errors; we no longer have to be concerned about pardon mental recall which we no longer use. But leaks open when we unintentionally reason on to intent references which we no longer use. By on condition that a accumulation of Views on the applications mental recall operate the Memory Profiler can be a profitable apparatus to acknowledge and repair such leaks.
Powered by PHPDug open country at, Powered by PHPDug just cause, Powered by PHPDug flower arrangement, Powered by PHPDug mental abuse, Powered by PHPDug root cause analysis

