Skip to content

Commit 9acbe29

Browse files
committed
* Code optimisations
1 parent 2dc1528 commit 9acbe29

File tree

7 files changed

+110
-136
lines changed

7 files changed

+110
-136
lines changed
File renamed without changes.

MemPlus/Classes/MemPlus.cs renamed to MemPlus/Classes/RAM/MemPlus.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using System.Runtime.InteropServices;
66
using System.Security.Principal;
77

8-
namespace MemPlus.Classes
8+
namespace MemPlus.Classes.RAM
99
{
1010
/// <summary>
1111
/// System Cache Information structure for x86 working set
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using System.Timers;
4+
using System.Windows.Threading;
5+
using Microsoft.VisualBasic.Devices;
6+
using Syncfusion.UI.Xaml.Gauges;
7+
8+
namespace MemPlus.Classes.RAM
9+
{
10+
internal class RamController
11+
{
12+
private readonly Timer _ramTimer;
13+
private readonly Dispatcher _dispatcher;
14+
private readonly SfCircularGauge _gauge;
15+
private readonly ComputerInfo _info;
16+
17+
internal double RamUsage { get; private set; }
18+
internal double RamUsagePercentage { get; private set; }
19+
internal double RamTotal { get; private set; }
20+
internal double RamSavings { get; private set; }
21+
22+
internal RamController(Dispatcher dispatcher, SfCircularGauge gauge, int timerInterval)
23+
{
24+
if (timerInterval <= 0) throw new ArgumentException("Timer interval cannot be less than or equal to zero!");
25+
26+
RamSavings = 0;
27+
28+
_info = new ComputerInfo();
29+
_dispatcher = dispatcher ?? throw new ArgumentException("Dispatcher cannot be null!");
30+
_gauge = gauge ?? throw new ArgumentException("Gauge cannot be null!");
31+
32+
_ramTimer = new Timer();
33+
_ramTimer.Elapsed += OnTimedEvent;
34+
_ramTimer.Interval = timerInterval;
35+
}
36+
37+
internal void EnableMonitor()
38+
{
39+
if (_ramTimer.Enabled) return;
40+
41+
_ramTimer.Enabled = true;
42+
OnTimedEvent(null, null);
43+
}
44+
45+
internal void DisableMonitor()
46+
{
47+
_ramTimer.Enabled = false;
48+
}
49+
50+
private void OnTimedEvent(object source, ElapsedEventArgs e)
51+
{
52+
UpdateRamUsage();
53+
_dispatcher.Invoke(() =>
54+
{
55+
_gauge.Scales[0].Pointers[0].Value = RamUsagePercentage;
56+
_gauge.GaugeHeader = "RAM usage (" + RamUsagePercentage.ToString("F2") + "%)";
57+
});
58+
}
59+
60+
internal async Task ClearMemory(bool filesystemcache)
61+
{
62+
await Task.Run(() =>
63+
{
64+
bool wasEnabled = _ramTimer.Enabled;
65+
if (wasEnabled)
66+
{
67+
DisableMonitor();
68+
}
69+
70+
UpdateRamUsage();
71+
72+
double oldUsage = RamUsage;
73+
74+
//Clear working set of all processes that the user has access to
75+
MemPlus.EmptyWorkingSetFunction();
76+
//Clear file system cache
77+
MemPlus.ClearFileSystemCache(filesystemcache);
78+
79+
UpdateRamUsage();
80+
double newUsage = RamUsage;
81+
82+
RamSavings = oldUsage - newUsage;
83+
84+
if (wasEnabled)
85+
{
86+
EnableMonitor();
87+
}
88+
});
89+
}
90+
91+
private void UpdateRamUsage()
92+
{
93+
double total = Convert.ToDouble(_info.TotalPhysicalMemory);
94+
double usage = total - Convert.ToDouble(_info.AvailablePhysicalMemory);
95+
double perc = usage / total * 100;
96+
97+
RamUsage = usage;
98+
RamUsagePercentage = perc;
99+
RamTotal = total;
100+
}
101+
}
102+
}

MemPlus/Classes/RamController.cs

Lines changed: 0 additions & 48 deletions
This file was deleted.

MemPlus/Classes/RamMonitor.cs

Lines changed: 0 additions & 77 deletions
This file was deleted.

MemPlus/MemPlus.csproj

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,9 @@
6969
<DependentUpon>App.xaml</DependentUpon>
7070
<SubType>Code</SubType>
7171
</Compile>
72-
<Compile Include="Classes\RamController.cs" />
73-
<Compile Include="Classes\MemPlus.cs" />
74-
<Compile Include="Classes\RamMonitor.cs" />
75-
<Compile Include="Classes\StyleManager.cs" />
72+
<Compile Include="Classes\RAM\RamController.cs" />
73+
<Compile Include="Classes\RAM\MemPlus.cs" />
74+
<Compile Include="Classes\GUI\StyleManager.cs" />
7675
<Compile Include="Windows\MainWindow.xaml.cs">
7776
<DependentUpon>MainWindow.xaml</DependentUpon>
7877
<SubType>Code</SubType>

MemPlus/Windows/MainWindow.xaml.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Windows;
33
using MemPlus.Classes;
4+
using MemPlus.Classes.RAM;
45

56
namespace MemPlus.Windows
67
{
@@ -10,18 +11,15 @@ namespace MemPlus.Windows
1011
/// </summary>
1112
public partial class MainWindow
1213
{
13-
private readonly RamMonitor _monitor;
1414
private readonly RamController _ramController;
1515

1616
public MainWindow()
1717
{
1818
InitializeComponent();
1919
ChangeVisualStyle();
2020

21-
_monitor = new RamMonitor(Dispatcher, CgRamUsage);
22-
_monitor.Start();
23-
24-
_ramController = new RamController(_monitor);
21+
_ramController = new RamController(Dispatcher, CgRamUsage, 5000);
22+
_ramController.EnableMonitor();
2523
}
2624

2725
internal void ChangeVisualStyle()
@@ -33,7 +31,7 @@ private async void BtnClearMemory_OnClick(object sender, RoutedEventArgs e)
3331
{
3432
try
3533
{
36-
await _ramController.Clear(ChbFileSystemCache.IsChecked != null && ChbFileSystemCache.IsChecked.Value);
34+
await _ramController.ClearMemory(ChbFileSystemCache.IsChecked != null && ChbFileSystemCache.IsChecked.Value);
3735
double ramSavings = _ramController.RamSavings / 1024 / 1024;
3836
if (ramSavings < 0)
3937
{

0 commit comments

Comments
 (0)