99
1010namespace MemPlus . Classes . RAM
1111{
12+ /// <summary>
13+ /// Sealed class containing methods and interaction logic in terms of RAM
14+ /// </summary>
1215 internal sealed class RamController
1316 {
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>
1425 private readonly Timer _ramTimer ;
26+ /// <summary>
27+ /// The LogController object that can be used to add logs
28+ /// </summary>
1529 private readonly LogController _logController ;
16-
30+ /// <summary>
31+ /// The Dispatcher object that can be used to update GUI components
32+ /// </summary>
1733 private readonly Dispatcher _dispatcher ;
34+ /// <summary>
35+ /// The SfCircularGauge object that can be used to present RAM usage statistics
36+ /// </summary>
1837 private readonly SfCircularGauge _gauge ;
38+ /// <summary>
39+ /// The Label object that can be used to show the total physical memory
40+ /// </summary>
1941 private readonly Label _lblTotal ;
42+ /// <summary>
43+ /// The Label object that can be used to show the available physical memory
44+ /// </summary>
2045 private readonly Label _lblAvailable ;
21-
46+ /// <summary>
47+ /// The ComputerInfo object that can be used to retrieve RAM usage statistics
48+ /// </summary>
2249 private readonly ComputerInfo _info ;
50+ #endregion
2351
52+ #region Properties
53+ /// <summary>
54+ /// Property containing how much RAM is being used
55+ /// </summary>
2456 internal double RamUsage { get ; private set ; }
57+ /// <summary>
58+ /// Property containing the percentage of RAM that is being used
59+ /// </summary>
2560 internal double RamUsagePercentage { get ; private set ; }
61+ /// <summary>
62+ /// Property containing the total amount of RAM available
63+ /// </summary>
2664 internal double RamTotal { get ; private set ; }
65+ /// <summary>
66+ /// Property containing how much RAM was saved during the last optimisation
67+ /// </summary>
2768 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>
2980 internal RamController ( Dispatcher dispatcher , SfCircularGauge gauge , Label lblTotal , Label lblAvailable , int timerInterval , LogController logController )
3081 {
3182 _logController = logController ?? throw new ArgumentNullException ( nameof ( logController ) ) ;
@@ -42,44 +93,72 @@ internal RamController(Dispatcher dispatcher, SfCircularGauge gauge, Label lblTo
4293 _lblTotal = lblTotal ?? throw new ArgumentNullException ( nameof ( lblTotal ) ) ;
4394 _lblAvailable = lblAvailable ?? throw new ArgumentNullException ( nameof ( lblAvailable ) ) ;
4495
96+ _ramOptimizer = new RamOptimizer ( _logController ) ;
97+
4598 _ramTimer = new Timer ( ) ;
4699 _ramTimer . Elapsed += OnTimedEvent ;
47100 _ramTimer . Interval = timerInterval ;
48101
49102 _logController . AddLog ( new ApplicationLog ( "Done initializing RamController" ) ) ;
50103 }
51104
105+ /// <summary>
106+ /// Enable RAM usage monitoring
107+ /// </summary>
52108 internal void EnableMonitor ( )
53109 {
54110 if ( _ramTimer . Enabled ) return ;
55111 _ramTimer . Enabled = true ;
56- OnTimedEvent ( null , null ) ;
112+
113+ UpdateRamUsage ( ) ;
114+ UpdateGuiControls ( ) ;
115+
57116 _logController . AddLog ( new ApplicationLog ( "The RAM monitor has been enabled" ) ) ;
58117 }
59118
119+ /// <summary>
120+ /// Disable RAM usage monitoring
121+ /// </summary>
60122 internal void DisableMonitor ( )
61123 {
62124 _ramTimer . Enabled = false ;
63125 _logController . AddLog ( new ApplicationLog ( "The RAM monitor has been disabled" ) ) ;
64126 }
65127
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 ( )
67132 {
68- _logController . AddLog ( new ApplicationLog ( "RAM monitor timer has been called" ) ) ;
69-
70- UpdateRamUsage ( ) ;
71-
72133 _dispatcher . Invoke ( ( ) =>
73134 {
74135 _gauge . Scales [ 0 ] . Pointers [ 0 ] . Value = RamUsagePercentage ;
75136 _gauge . GaugeHeader = "RAM usage (" + RamUsagePercentage . ToString ( "F2" ) + "%)" ;
76137 _lblTotal . Content = ( RamTotal / 1024 / 1024 / 1024 ) . ToString ( "F2" ) + " GB" ;
77138 _lblAvailable . Content = ( RamUsage / 1024 / 1024 / 1024 ) . ToString ( "F2" ) + " GB" ;
78139 } ) ;
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 ( ) ;
79153
80154 _logController . AddLog ( new ApplicationLog ( "Finished RAM monitor timer" ) ) ;
81155 }
82156
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>
83162 internal async Task ClearMemory ( bool filesystemcache )
84163 {
85164 _logController . AddLog ( new ApplicationLog ( "Clearing RAM memory" ) ) ;
@@ -90,10 +169,8 @@ await Task.Run(async () =>
90169
91170 double oldUsage = RamUsage ;
92171
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 ) ;
97174
98175 await Task . Delay ( 10000 ) ;
99176
@@ -106,6 +183,9 @@ await Task.Run(async () =>
106183 _logController . AddLog ( new ApplicationLog ( "Done clearing RAM memory" ) ) ;
107184 }
108185
186+ /// <summary>
187+ /// Update RAM usage statistics
188+ /// </summary>
109189 private void UpdateRamUsage ( )
110190 {
111191 _logController . AddLog ( new ApplicationLog ( "Updating RAM usage" ) ) ;
0 commit comments