我发现如果我有一个在 DataTemplate 中有一个自定义控件的枢轴,每次在枢轴上滑动都会泄漏 ~2-3mb。即使自定义控件为空,此问题也会对我产生影响。我的目标是让自定义控件加载图像,但泄漏情况更糟。
如何为枢轴上不再显示的未实现对象释放内存?
自定义控件的代码隐藏为空。我已禁用控件上图像的显示,因为我认为这里还有另一个问题我想在解决第二个问题之前确定。
完整代码示例:https ://github.com/mpoulson/PivotMemoryLeak
没有加载任何图像的内存配置文件(没有足够的点来拥有图像) https://raw.githubusercontent.com/mpoulson/PivotMemoryLeak/master/Analysis%20-%20No%20Image.png
复制:
- 在调试器中加载示例应用程序
- 加载后选择查看配置文件按钮
- 通过枢轴向左/向右滑动
- 请注意,每次滑动都会增加内存使用量(示例只有几个配置文件,但我希望有 >300 并且可用)。
页面.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>