Skip to content

Commit 4e3986d

Browse files
committed
* Splitbutton to control memory clearing
1 parent ae94235 commit 4e3986d

File tree

3 files changed

+125
-37
lines changed

3 files changed

+125
-37
lines changed

MemPlus/Business/Classes/RAM/RamController.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,64 @@ await Task.Run(async () =>
292292
_logController.AddLog(new ApplicationLog("Done clearing RAM memory"));
293293
}
294294

295+
/// <summary>
296+
/// Clear the working set of all processes, excluding the exclusion list
297+
/// </summary>
298+
/// <returns>Nothing</returns>
299+
internal async Task ClearWorkingSets()
300+
{
301+
_logController.AddLog(new ApplicationLog("Clearing process working sets"));
302+
303+
await Task.Run(async () =>
304+
{
305+
UpdateRamUsage();
306+
307+
double oldUsage = RamUsage;
308+
309+
_ramOptimizer.EmptyWorkingSetFunction(_processExceptionList);
310+
311+
await Task.Delay(10000);
312+
313+
UpdateRamUsage();
314+
UpdateGuiControls();
315+
316+
double newUsage = RamUsage;
317+
318+
RamSavings = oldUsage - newUsage;
319+
});
320+
321+
_logController.AddLog(new ApplicationLog("Done clearing process working sets"));
322+
}
323+
324+
/// <summary>
325+
/// Clear the FileSystem cache
326+
/// </summary>
327+
/// <returns>Nothing</returns>
328+
internal async Task ClearFileSystemCaches()
329+
{
330+
_logController.AddLog(new ApplicationLog("Clearing FileSystem cache"));
331+
332+
await Task.Run(async () =>
333+
{
334+
UpdateRamUsage();
335+
336+
double oldUsage = RamUsage;
337+
338+
_ramOptimizer.ClearFileSystemCache(ClearStandbyCache);
339+
340+
await Task.Delay(5000);
341+
342+
UpdateRamUsage();
343+
UpdateGuiControls();
344+
345+
double newUsage = RamUsage;
346+
347+
RamSavings = oldUsage - newUsage;
348+
});
349+
350+
_logController.AddLog(new ApplicationLog("Done clearing FileSystem cache"));
351+
}
352+
295353
/// <summary>
296354
/// Update RAM usage statistics
297355
/// </summary>

MemPlus/Views/Windows/MainWindow.xaml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,16 +194,19 @@
194194
<RowDefinition Height="Auto" />
195195
<RowDefinition Height="Auto" />
196196
<RowDefinition Height="Auto" />
197-
<RowDefinition Height="Auto" />
198-
<RowDefinition Height="*" />
199197
</Grid.RowDefinitions>
200198

201-
<Label Content="Total physical memory:" FontSize="14" Margin="10,5" />
202-
<Label x:Name="LblTotalPhysicalMemory" Grid.Row="2" Content="" FontSize="14" Foreground="Green" Margin="10,5" />
203-
<Label Grid.Row="3" Content="Used physical memory:" FontSize="14" Margin="10,5" />
204-
<Label x:Name="LblAvailablePhysicalMemory" Grid.Row="4" Content="" FontSize="14" Foreground="Red" Margin="10,5" />
199+
<Label Grid.Row="0" Content="Total physical memory:" FontSize="14" Margin="10,5" />
200+
<Label Grid.Row="1" x:Name="LblTotalPhysicalMemory" Content="" FontSize="14" Foreground="Green" Margin="10,5" />
201+
<Label Grid.Row="2" Content="Used physical memory:" FontSize="14" Margin="10,5" />
202+
<Label Grid.Row="3" x:Name="LblAvailablePhysicalMemory" Content="" FontSize="14" Foreground="Red" Margin="10,5" />
205203

206-
<Button Grid.Row="5" x:Name="BtnClearMemory" Content="Clear memory" Click="BtnClearMemory_OnClick" Margin="10,5" FontSize="14" MinHeight="25" />
204+
<syncfusion:SplitButtonAdv Grid.Row="4" SmallIcon="../../Resources/Images/ram_tab.png" Label="Clear memory" x:Name="BtnClearMemory" Click="BtnClearMemory_OnClick" IsMultiLine="False">
205+
<syncfusion:DropDownMenuGroup>
206+
<syncfusion:DropDownMenuItem Header="Clear Working sets" Click="ClearWorkingSetsDropDownMenuItem_OnClick"/>
207+
<syncfusion:DropDownMenuItem Header="Clear FileSystem cache" Click="ClearFileSystemCacheDropDownMenuItem_OnClick"/>
208+
</syncfusion:DropDownMenuGroup>
209+
</syncfusion:SplitButtonAdv>
207210
</Grid>
208211
</Grid>
209212
</Grid>

MemPlus/Views/Windows/MainWindow.xaml.cs

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -242,37 +242,9 @@ internal void ChangeVisualStyle()
242242
/// </summary>
243243
/// <param name="sender">The object that called this method</param>
244244
/// <param name="e">The RoutedEventArgs</param>
245-
private async void BtnClearMemory_OnClick(object sender, RoutedEventArgs e)
245+
private void BtnClearMemory_OnClick(object sender, RoutedEventArgs e)
246246
{
247-
_logController.AddLog(new ApplicationLog("Clearing RAM Memory"));
248-
249-
try
250-
{
251-
BtnClearMemory.IsEnabled = false;
252-
253-
await _ramController.ClearMemory();
254-
double ramSavings = _ramController.RamSavings / 1024 / 1024;
255-
if (ramSavings < 0)
256-
{
257-
ramSavings = Math.Abs(ramSavings);
258-
_logController.AddLog(new RamLog("RAM usage increase: " + ramSavings.ToString("F2") + " MB"));
259-
MessageBox.Show("Looks like your RAM usage has increased with " + ramSavings.ToString("F2") + " MB!", "MemPlus", MessageBoxButton.OK, MessageBoxImage.Information);
260-
}
261-
else
262-
{
263-
_logController.AddLog(new RamLog("RAM usage decrease: " + ramSavings.ToString("F2") + " MB"));
264-
MessageBox.Show("You saved " + ramSavings.ToString("F2") + " MB of RAM!", "MemPlus", MessageBoxButton.OK, MessageBoxImage.Information);
265-
}
266-
267-
BtnClearMemory.IsEnabled = true;
268-
}
269-
catch (Exception ex)
270-
{
271-
_logController.AddLog(new ApplicationLog(ex.Message));
272-
MessageBox.Show(ex.Message, "MemPlus", MessageBoxButton.OK, MessageBoxImage.Error);
273-
}
274-
275-
_logController.AddLog(new ApplicationLog("Done clearing RAM memory"));
247+
ClearMemory(0);
276248
}
277249

278250
/// <summary>
@@ -676,5 +648,60 @@ private void RestartMenuItem_OnClick(object sender, RoutedEventArgs e)
676648
MessageBox.Show(ex.Message, "MemPlus", MessageBoxButton.OK, MessageBoxImage.Error);
677649
}
678650
}
651+
652+
private void ClearWorkingSetsDropDownMenuItem_OnClick(object sender, RoutedEventArgs e)
653+
{
654+
ClearMemory(1);
655+
}
656+
657+
private void ClearFileSystemCacheDropDownMenuItem_OnClick(object sender, RoutedEventArgs e)
658+
{
659+
ClearMemory(2);
660+
}
661+
662+
private async void ClearMemory(int index)
663+
{
664+
_logController.AddLog(new ApplicationLog("Clearing RAM Memory"));
665+
666+
try
667+
{
668+
BtnClearMemory.IsEnabled = false;
669+
670+
switch (index)
671+
{
672+
case 0:
673+
await _ramController.ClearMemory();
674+
break;
675+
case 1:
676+
await _ramController.ClearWorkingSets();
677+
break;
678+
case 2:
679+
await _ramController.ClearFileSystemCaches();
680+
break;
681+
}
682+
683+
double ramSavings = _ramController.RamSavings / 1024 / 1024;
684+
if (ramSavings < 0)
685+
{
686+
ramSavings = Math.Abs(ramSavings);
687+
_logController.AddLog(new RamLog("RAM usage increase: " + ramSavings.ToString("F2") + " MB"));
688+
MessageBox.Show("Looks like your RAM usage has increased with " + ramSavings.ToString("F2") + " MB!", "MemPlus", MessageBoxButton.OK, MessageBoxImage.Information);
689+
}
690+
else
691+
{
692+
_logController.AddLog(new RamLog("RAM usage decrease: " + ramSavings.ToString("F2") + " MB"));
693+
MessageBox.Show("You saved " + ramSavings.ToString("F2") + " MB of RAM!", "MemPlus", MessageBoxButton.OK, MessageBoxImage.Information);
694+
}
695+
696+
BtnClearMemory.IsEnabled = true;
697+
}
698+
catch (Exception ex)
699+
{
700+
_logController.AddLog(new ApplicationLog(ex.Message));
701+
MessageBox.Show(ex.Message, "MemPlus", MessageBoxButton.OK, MessageBoxImage.Error);
702+
}
703+
704+
_logController.AddLog(new ApplicationLog("Done clearing RAM memory"));
705+
}
679706
}
680707
}

0 commit comments

Comments
 (0)