9
9
10
10
namespace MemPlus . Classes . RAM
11
11
{
12
+ /// <summary>
13
+ /// Sealed class containing methods and interaction logic in terms of RAM
14
+ /// </summary>
12
15
internal sealed class RamController
13
16
{
17
+ #region Variables
18
+ /// <summary>
19
+ /// The RamOptimizer object that can be called to clear memory
20
+ /// </summary>
21
+ private readonly RamOptimizer _ramOptimizer ;
22
+ /// <summary>
23
+ /// The Timer object that will periodically update RAM usage statistics
24
+ /// </summary>
14
25
private readonly Timer _ramTimer ;
26
+ /// <summary>
27
+ /// The LogController object that can be used to add logs
28
+ /// </summary>
15
29
private readonly LogController _logController ;
16
-
30
+ /// <summary>
31
+ /// The Dispatcher object that can be used to update GUI components
32
+ /// </summary>
17
33
private readonly Dispatcher _dispatcher ;
34
+ /// <summary>
35
+ /// The SfCircularGauge object that can be used to present RAM usage statistics
36
+ /// </summary>
18
37
private readonly SfCircularGauge _gauge ;
38
+ /// <summary>
39
+ /// The Label object that can be used to show the total physical memory
40
+ /// </summary>
19
41
private readonly Label _lblTotal ;
42
+ /// <summary>
43
+ /// The Label object that can be used to show the available physical memory
44
+ /// </summary>
20
45
private readonly Label _lblAvailable ;
21
-
46
+ /// <summary>
47
+ /// The ComputerInfo object that can be used to retrieve RAM usage statistics
48
+ /// </summary>
22
49
private readonly ComputerInfo _info ;
50
+ #endregion
23
51
52
+ #region Properties
53
+ /// <summary>
54
+ /// Property containing how much RAM is being used
55
+ /// </summary>
24
56
internal double RamUsage { get ; private set ; }
57
+ /// <summary>
58
+ /// Property containing the percentage of RAM that is being used
59
+ /// </summary>
25
60
internal double RamUsagePercentage { get ; private set ; }
61
+ /// <summary>
62
+ /// Property containing the total amount of RAM available
63
+ /// </summary>
26
64
internal double RamTotal { get ; private set ; }
65
+ /// <summary>
66
+ /// Property containing how much RAM was saved during the last optimisation
67
+ /// </summary>
27
68
internal double RamSavings { get ; private set ; }
28
-
69
+ #endregion
70
+
71
+ /// <summary>
72
+ /// Initialize a new RamController object
73
+ /// </summary>
74
+ /// <param name="dispatcher">The Dispatcher object that can be used to update GUI components</param>
75
+ /// <param name="gauge">The SfCircularGauge control that can be used to present RAM usage statistics</param>
76
+ /// <param name="lblTotal">The Label control that can be used to display the total available memory statistics</param>
77
+ /// <param name="lblAvailable">The Label control that can be used to display the available memory statistics</param>
78
+ /// <param name="timerInterval">The interval for which RAM usage statistics should be updated</param>
79
+ /// <param name="logController">The LogController object that can be used to add logs</param>
29
80
internal RamController ( Dispatcher dispatcher , SfCircularGauge gauge , Label lblTotal , Label lblAvailable , int timerInterval , LogController logController )
30
81
{
31
82
_logController = logController ?? throw new ArgumentNullException ( nameof ( logController ) ) ;
@@ -42,44 +93,72 @@ internal RamController(Dispatcher dispatcher, SfCircularGauge gauge, Label lblTo
42
93
_lblTotal = lblTotal ?? throw new ArgumentNullException ( nameof ( lblTotal ) ) ;
43
94
_lblAvailable = lblAvailable ?? throw new ArgumentNullException ( nameof ( lblAvailable ) ) ;
44
95
96
+ _ramOptimizer = new RamOptimizer ( _logController ) ;
97
+
45
98
_ramTimer = new Timer ( ) ;
46
99
_ramTimer . Elapsed += OnTimedEvent ;
47
100
_ramTimer . Interval = timerInterval ;
48
101
49
102
_logController . AddLog ( new ApplicationLog ( "Done initializing RamController" ) ) ;
50
103
}
51
104
105
+ /// <summary>
106
+ /// Enable RAM usage monitoring
107
+ /// </summary>
52
108
internal void EnableMonitor ( )
53
109
{
54
110
if ( _ramTimer . Enabled ) return ;
55
111
_ramTimer . Enabled = true ;
56
- OnTimedEvent ( null , null ) ;
112
+
113
+ UpdateRamUsage ( ) ;
114
+ UpdateGuiControls ( ) ;
115
+
57
116
_logController . AddLog ( new ApplicationLog ( "The RAM monitor has been enabled" ) ) ;
58
117
}
59
118
119
+ /// <summary>
120
+ /// Disable RAM usage monitoring
121
+ /// </summary>
60
122
internal void DisableMonitor ( )
61
123
{
62
124
_ramTimer . Enabled = false ;
63
125
_logController . AddLog ( new ApplicationLog ( "The RAM monitor has been disabled" ) ) ;
64
126
}
65
127
66
- private void OnTimedEvent ( object source , ElapsedEventArgs e )
128
+ /// <summary>
129
+ /// Update the GUI controls with the available RAM usage statistics
130
+ /// </summary>
131
+ private void UpdateGuiControls ( )
67
132
{
68
- _logController . AddLog ( new ApplicationLog ( "RAM monitor timer has been called" ) ) ;
69
-
70
- UpdateRamUsage ( ) ;
71
-
72
133
_dispatcher . Invoke ( ( ) =>
73
134
{
74
135
_gauge . Scales [ 0 ] . Pointers [ 0 ] . Value = RamUsagePercentage ;
75
136
_gauge . GaugeHeader = "RAM usage (" + RamUsagePercentage . ToString ( "F2" ) + "%)" ;
76
137
_lblTotal . Content = ( RamTotal / 1024 / 1024 / 1024 ) . ToString ( "F2" ) + " GB" ;
77
138
_lblAvailable . Content = ( RamUsage / 1024 / 1024 / 1024 ) . ToString ( "F2" ) + " GB" ;
78
139
} ) ;
140
+ }
141
+
142
+ /// <summary>
143
+ /// Event that will be called when the timer interval was reached
144
+ /// </summary>
145
+ /// <param name="source">The calling object</param>
146
+ /// <param name="e">The ElapsedEventArgs</param>
147
+ private void OnTimedEvent ( object source , ElapsedEventArgs e )
148
+ {
149
+ _logController . AddLog ( new ApplicationLog ( "RAM monitor timer has been called" ) ) ;
150
+
151
+ UpdateRamUsage ( ) ;
152
+ UpdateGuiControls ( ) ;
79
153
80
154
_logController . AddLog ( new ApplicationLog ( "Finished RAM monitor timer" ) ) ;
81
155
}
82
156
157
+ /// <summary>
158
+ /// Clear all non-essential RAM
159
+ /// </summary>
160
+ /// <param name="filesystemcache">A boolean to indicate whether or not to clear the FileSystem cache</param>
161
+ /// <returns></returns>
83
162
internal async Task ClearMemory ( bool filesystemcache )
84
163
{
85
164
_logController . AddLog ( new ApplicationLog ( "Clearing RAM memory" ) ) ;
@@ -90,10 +169,8 @@ await Task.Run(async () =>
90
169
91
170
double oldUsage = RamUsage ;
92
171
93
- //Clear working set of all processes that the user has access to
94
- MemPlus . EmptyWorkingSetFunction ( ) ;
95
- //Clear file system cache
96
- MemPlus . ClearFileSystemCache ( filesystemcache ) ;
172
+ _ramOptimizer . EmptyWorkingSetFunction ( ) ;
173
+ _ramOptimizer . ClearFileSystemCache ( filesystemcache ) ;
97
174
98
175
await Task . Delay ( 10000 ) ;
99
176
@@ -106,6 +183,9 @@ await Task.Run(async () =>
106
183
_logController . AddLog ( new ApplicationLog ( "Done clearing RAM memory" ) ) ;
107
184
}
108
185
186
+ /// <summary>
187
+ /// Update RAM usage statistics
188
+ /// </summary>
109
189
private void UpdateRamUsage ( )
110
190
{
111
191
_logController . AddLog ( new ApplicationLog ( "Updating RAM usage" ) ) ;
0 commit comments