I have found that if I have a pivot that has a Custom Control in the DataTemplate it will leak ~2-3mb with each swipe on the pivot. This issue repros for me even if the custom control is empty. My goal is to have the custom control load an image but then the leak is even worse.
How can I free the memory for the unrealized object that are not longer in view on the pivot?
The custom control's codebehind is empty. I have disable the showing of the image on the control because I think there is another issue here that I want to nail down before working on resolving the second.
Full code sample: https://github.com/mpoulson/PivotMemoryLeak
Memory Profile without loading any images (don't have enough points yet to have images) https://raw.githubusercontent.com/mpoulson/PivotMemoryLeak/master/Analysis%20-%20No%20Image.png
Repro:
- Load sample app in debugger
- Once loaded select View Profiles button
- Swipe left/right through pivot
- Note memory usage increase with each swipe (sample only has a few profiles but I hope to have >300 and be usable).
Page.xaml
<phone:PhoneApplicationPage
x:Class="PanoramaApp1.Views.ProfilePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:PanoramaApp1.ViewModels"
xmlns:controls="clr-namespace:PanoramaApp1.Controls"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d"
shell:SystemTray.IsVisible="True">
<shell:SystemTray.ProgressIndicator>
<shell:ProgressIndicator IsIndeterminate="True" Text="Loading..." />
</shell:SystemTray.ProgressIndicator>
<phone:PhoneApplicationPage.Resources>
<vm:ProfileCollectionViewModel x:Key="viewModel"/>
<DataTemplate x:Key="HeaderTemplate">
<TextBlock Text="{Binding TargetProfile.ScreenName}"
FontSize="{StaticResource PhoneFontSizeMedium}"/>
</DataTemplate>
<DataTemplate x:Key="ItemTemplate">
<controls:WindowsPhoneControl1 Visibility="Visible"/>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<!--TitlePanel contains the name of the application and page title-->
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto" MaxHeight="80"/>
</Grid.RowDefinitions>
<phone:Pivot
x:Name="ProfilePivot"
Grid.Row="0"
DataContext="{StaticResource viewModel}"
ItemsSource="{Binding Profiles}"
SelectionChanged="ProfilePivot_SelectionChanged"
HeaderTemplate="{StaticResource HeaderTemplate}"
ItemTemplate="{StaticResource ItemTemplate}" />
</Grid>
</phone:PhoneApplicationPage>
WindowsPhoneControl1.xaml
<UserControl x:Class="PanoramaApp1.Controls.WindowsPhoneControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="480" d:DesignWidth="480">
<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--This make the leak even worse!!
<Image x:Name="Image" Source="{Binding TargetProfile, Converter={StaticResource ProfileBackgroundImageConverter}}" />-->
</Grid>
</UserControl>