My wpf application memory getting bigger and bigger when I keep use it.
so I want to create a simple one to see if it will release memory:
- create a wpf project
- put a ListBox bound collection and 2 Buttons for Load & clear data
- create an model Person
- create an observableCollection
- Load command is load 100,000 persons in to collection
- Clear command call ObservableCollection.Clear() method.
- check memory while these steps
XAML:
<DockPanel> <UniformGrid Rows="1" Columns="2" DockPanel.Dock="Bottom"> <Button Content="Load" Padding="0,12" Command="{Binding LoadCommand}"/> <Button Content="Clear" Padding="0,12" Command="{Binding ClearCommand}"/> </UniformGrid> <ListBox ItemsSource="{Binding Persons}"/> </DockPanel> Person Model:
public class PersonModel : BindableBase { private int _id = 0; public int ID { get { return _id; } set { SetProperty(ref _id, value); } } private string _name = string.Empty; public string Name { get { return _name; } set { SetProperty(ref _name, value); } } public override string ToString() { return $"{ID:00000} : {Name}"; } } ViewModel:
public MainViewModel() { Persons = new ObservableCollection<PersonModel>(); Persons.CollectionChanged += (s, e) => { LoadCommand.RaiseCanExecuteChanged(); ClearCommand.RaiseCanExecuteChanged(); }; } public ObservableCollection<PersonModel> Persons { get; private set; } private BindableCommand _loadCommand = null; public BindableCommand LoadCommand { get { return _loadCommand ?? (_loadCommand = new BindableCommand(LoadExecute, LoadCanExecute)); } } private BindableCommand _clearCommand = null; public BindableCommand ClearCommand { get { return _clearCommand ?? (_clearCommand = new BindableCommand(ClearExecute, ClearCanExecute)); } } private void LoadExecute() { if (!LoadCanExecute()) return; for (int i = 0; i < 1000000; i++) { Persons.Add(new PersonModel { ID = i, Name = "The quick brown fox jumps over a lazy dog." }); } } private bool LoadCanExecute() { if (Persons.Count > 0) return false; return true; } private void ClearExecute() { if (!ClearCanExecute()) return; Persons.Clear(); } private bool ClearCanExecute() { if (Persons.Count == 0) return false; return true; } when I start the program(release version), memory usage in Task Manager is about 10mb(Test12.exe process)
when I click load button, memory raise to about 40mb:
but when I click Clear button, memory not trying to go back start size(10mb), but just stay in 39mb, I wait for 10 minutes it won't come down.
So my question is: how to release memory in WPF correctly?


