I've discovered a strange problem with one of my layouts. When using a Grid
RowDefinition
with Height=Auto
, the application takes much longer to startup and consumes 5x as much memory. I've managed to create a sample application to demonstrate:
MainWindow.xaml
<Window x:Class="MemoryHog.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mh="clr-namespace:MemoryHog"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<mh:DataSource x:Key="DataSource"/> <!-- 15000 strings-->
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" /> <!-- "Auto" = 112mb, "22" = 22mb. Auto takes much longer to startup -->
</Grid.RowDefinitions>
<ListView Grid.Column="1" Grid.Row="0" Width="200" DataContext="{StaticResource DataSource}" ItemsSource="{Binding Items}"/>
<StatusBar Grid.ColumnSpan="2" Grid.Row="1" />
</Grid>
</Window>
DataSource.cs
using System;
using System.Collections.ObjectModel;
namespace MemoryHog
{
class DataSource
{
public DataSource()
{
this.Items = new ObservableCollection<String>();
for (int i = 0; i < 15000; ++i)
{
this.Items.Add(String.Format("{0}", i + 1));
}
}
public ObservableCollection<String> Items { get; set; }
}
}
Look at my comment in MainWindow.xaml, if you set the RowDefinition.Height
to Auto
the app will consume 112MB of memory, but if you change it to be 22
, the app will only consume 22MB! What is going on here? Is this a bug?
Edit The memory increase is directly proportional to the number of items in the ListView
, and the memory stays allocated forever.