Skip to content

Commit 9681060

Browse files
committed
* Implemented timed ram optimisation
* Added some code comments * Code improvements
1 parent 101e949 commit 9681060

File tree

2 files changed

+56
-11
lines changed

2 files changed

+56
-11
lines changed

MemPlus/Classes/RAM/RamController.cs

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ internal sealed class RamController
2525
/// </summary>
2626
private readonly Timer _ramTimer;
2727
/// <summary>
28+
/// The Timer object that will automatically Optimize the RAM after a certain interval has passed
29+
/// </summary>
30+
private Timer _ramAutoOptimizeTimer;
31+
/// <summary>
2832
/// The LogController object that can be used to add logs
2933
/// </summary>
3034
private readonly LogController _logController;
@@ -48,6 +52,10 @@ internal sealed class RamController
4852
/// The ComputerInfo object that can be used to retrieve RAM usage statistics
4953
/// </summary>
5054
private readonly ComputerInfo _info;
55+
/// <summary>
56+
/// The list of processes that should be excluded from memory optimisation
57+
/// </summary>
58+
private List<string> _processExceptionList;
5159
#endregion
5260

5361
#region Properties
@@ -89,14 +97,14 @@ internal sealed class RamController
8997
/// <param name="gauge">The SfCircularGauge control that can be used to present RAM usage statistics</param>
9098
/// <param name="lblTotal">The Label control that can be used to display the total available memory statistics</param>
9199
/// <param name="lblAvailable">The Label control that can be used to display the available memory statistics</param>
92-
/// <param name="timerInterval">The interval for which RAM usage statistics should be updated</param>
100+
/// <param name="ramUpdateTimerInterval">The interval for which RAM usage statistics should be updated</param>
93101
/// <param name="logController">The LogController object that can be used to add logs</param>
94-
internal RamController(Dispatcher dispatcher, SfCircularGauge gauge, Label lblTotal, Label lblAvailable, int timerInterval, LogController logController)
102+
internal RamController(Dispatcher dispatcher, SfCircularGauge gauge, Label lblTotal, Label lblAvailable, int ramUpdateTimerInterval, LogController logController)
95103
{
96104
_logController = logController ?? throw new ArgumentNullException(nameof(logController));
97105
_logController.AddLog(new ApplicationLog("Initializing RamController"));
98106

99-
if (timerInterval <= 0) throw new ArgumentException("Timer interval cannot be less than or equal to zero!");
107+
if (ramUpdateTimerInterval <= 0) throw new ArgumentException("Timer interval cannot be less than or equal to zero!");
100108

101109
RamSavings = 0;
102110

@@ -113,17 +121,53 @@ internal RamController(Dispatcher dispatcher, SfCircularGauge gauge, Label lblTo
113121

114122
_ramTimer = new Timer();
115123
_ramTimer.Elapsed += OnTimedEvent;
116-
_ramTimer.Interval = timerInterval;
124+
_ramTimer.Interval = ramUpdateTimerInterval;
117125
_ramTimer.Enabled = false;
118126

119127
_logController.AddLog(new ApplicationLog("Done initializing RamController"));
120128
}
121129

130+
/// <summary>
131+
/// Enable or disable automatic timed RAM optimisation
132+
/// </summary>
133+
/// <param name="enabled">A boolean to indicate whether automatic RAM optimisation should occur or not</param>
134+
/// <param name="interval">The interval for automatic RAM optimisation</param>
135+
internal void AutoOptimizeTimed(bool enabled, int interval)
136+
{
137+
if (_ramAutoOptimizeTimer == null)
138+
{
139+
_ramAutoOptimizeTimer = new Timer();
140+
_ramAutoOptimizeTimer.Elapsed += RamAutoOptimizeTimerOnElapsed;
141+
}
142+
143+
_ramAutoOptimizeTimer.Interval = interval;
144+
_ramAutoOptimizeTimer.Enabled = enabled;
145+
}
146+
147+
/// <summary>
148+
/// Event that will be called when the timer interval was reached
149+
/// </summary>
150+
/// <param name="sender">The object that called this method</param>
151+
/// <param name="elapsedEventArgs">The ElapsedEventArgs</param>
152+
private async void RamAutoOptimizeTimerOnElapsed(object sender, ElapsedEventArgs elapsedEventArgs)
153+
{
154+
await ClearMemory();
155+
}
156+
157+
/// <summary>
158+
/// Set the list of processes that should excluded from RAM optimisation
159+
/// </summary>
160+
/// <param name="processExceptionList">The list of processes that should be excluded from RAM optimisation</param>
161+
internal void SetProcessExceptionList(List<string> processExceptionList)
162+
{
163+
_processExceptionList = processExceptionList;
164+
}
165+
122166
/// <summary>
123167
/// Set the interval for the RAM Monitor updates
124168
/// </summary>
125169
/// <param name="interval">The amount of miliseconds before an update should occur</param>
126-
internal void SetTimerInterval(int interval)
170+
internal void SetRamUpdateTimerInterval(int interval)
127171
{
128172
_ramTimer.Interval = interval;
129173
}
@@ -187,9 +231,8 @@ private void OnTimedEvent(object source, ElapsedEventArgs e)
187231
/// <summary>
188232
/// Clear all non-essential RAM
189233
/// </summary>
190-
/// <param name="exceptionsList">A list of processes that should be excluded from memory optimisation</param>
191-
/// <returns></returns>
192-
internal async Task ClearMemory(List<string> exceptionsList)
234+
/// <returns>Nothing</returns>
235+
internal async Task ClearMemory()
193236
{
194237
_logController.AddLog(new ApplicationLog("Clearing RAM memory"));
195238

@@ -199,7 +242,7 @@ await Task.Run(async () =>
199242

200243
double oldUsage = RamUsage;
201244

202-
_ramOptimizer.EmptyWorkingSetFunction(exceptionsList);
245+
_ramOptimizer.EmptyWorkingSetFunction(_processExceptionList);
203246

204247
if (ClearFileSystemCache)
205248
{

MemPlus/Windows/MainWindow.xaml.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,11 @@ internal void LoadProperties()
101101
MniOnTop.IsChecked = Properties.Settings.Default.Topmost;
102102
MniRamMonitor.IsChecked = Properties.Settings.Default.RamMonitor;
103103

104+
_ramController.SetProcessExceptionList(Properties.Settings.Default.ProcessExceptions);
104105
_ramController.ClearFileSystemCache = Properties.Settings.Default.FileSystemCache;
105106
_ramController.ClearStandbyCache = Properties.Settings.Default.StandByCache;
106-
_ramController.SetTimerInterval(Properties.Settings.Default.RamMonitorInterval);
107+
_ramController.SetRamUpdateTimerInterval(Properties.Settings.Default.RamMonitorInterval);
108+
_ramController.AutoOptimizeTimed(Properties.Settings.Default.AutoOptimizeTimed, Properties.Settings.Default.AutoOptimizeTimedInterval);
107109

108110
if (Properties.Settings.Default.RamMonitor)
109111
{
@@ -195,7 +197,7 @@ private async void BtnClearMemory_OnClick(object sender, RoutedEventArgs e)
195197
{
196198
BtnClearMemory.IsEnabled = false;
197199

198-
await _ramController.ClearMemory(Properties.Settings.Default.ProcessExceptions);
200+
await _ramController.ClearMemory();
199201
double ramSavings = _ramController.RamSavings / 1024 / 1024;
200202
if (ramSavings < 0)
201203
{

0 commit comments

Comments
 (0)