Reputation: 11
The problem is a memory leak in my prism, autofac .NET 4.5.1 WPF application. I reduced the problem and the problem is also in a native WPF application without prism and autofac.
On startup the application uses 34 MB ram. I set a new instance of a usercontrol with a big ram allocation in the contentcontrol in the mainwindow. The ram goes up to 900 MB. Now i will clean up my ui and i tested to clear the internal collection in the usercontrol, the content of the contentcontrol set to null ... But the ram is on 140 MB. How can i release the ram between the startup and after cleanup.
When i create the usercontrol and not set it in the content of the contentcontrol the ram goes to 38 MB. That is OK. It is possible to release the "complete" ram when i use a usercontrol in another control?
Mainwindow:
<ContentControl x:Name="myContentControl"></ContentControl>
Code behind:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
this.myContentControl.Content = new UserControl1();
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
this.myContentControl.Content = null;
GC.Collect();
}
The usercontrol:
<Grid>
<DataGrid x:Name="myTestListView" Background="Purple" Height="280></DataGrid>
</Grid>
Code:
public partial class UserControl1 : UserControl
{
private List<string> Testitems = new List<string>();
public UserControl1()
{
InitializeComponent();
this.Testitems = new List<string>();
for (int i = 0; i < 1000000; i++) this.Testitems.Add(i.ToString());
this.myTestListView.ItemsSource = this.Testitems;
}
}
Upvotes: 1
Views: 1393
Reputation: 27
Consider replacing your Usercontrol1
collection backing field by an ObservableCollection
and the leak will disappear.
Upvotes: 1