@@ -94,6 +94,14 @@ internal sealed class RamController : IDisposable
9494 /// Property displaying whether the .NET garbage collector should be invoked or not
9595 /// </summary>
9696 internal bool InvokeGarbageCollector { private get ; set ; }
97+ /// <summary>
98+ /// Property displaying whether RAM memory should be completely filled or not
99+ /// </summary>
100+ internal bool FillRam { get ; set ; }
101+ /// <summary>
102+ /// The amount of times the FillRam method should run
103+ /// </summary>
104+ internal int FillRamMaxRuns { private get ; set ; }
97105 #endregion
98106
99107 #region Delegates
@@ -235,7 +243,14 @@ internal void EnableMonitor()
235243 _ramTimer . Enabled = true ;
236244 RamMonitorEnabled = true ;
237245
238- UpdateRamUsage ( ) ;
246+ try
247+ {
248+ UpdateRamUsage ( ) ;
249+ }
250+ catch ( Exception ex )
251+ {
252+ _logController . AddLog ( new ErrorLog ( ex . Message ) ) ;
253+ }
239254
240255 _logController . AddLog ( new ApplicationLog ( "The RAM monitor has been enabled" ) ) ;
241256 }
@@ -260,7 +275,14 @@ private void OnTimedEvent(object source, ElapsedEventArgs e)
260275 {
261276 _logController . AddLog ( new ApplicationLog ( "RAM monitor timer has been called" ) ) ;
262277
263- UpdateRamUsage ( ) ;
278+ try
279+ {
280+ UpdateRamUsage ( ) ;
281+ }
282+ catch ( Exception ex )
283+ {
284+ _logController . AddLog ( new ErrorLog ( ex . Message ) ) ;
285+ }
264286
265287 _logController . AddLog ( new ApplicationLog ( "Finished RAM monitor timer" ) ) ;
266288 }
@@ -276,32 +298,44 @@ internal async Task ClearMemory()
276298
277299 await Task . Run ( async ( ) =>
278300 {
279- UpdateRamUsage ( ) ;
301+ try
302+ {
303+ UpdateRamUsage ( ) ;
280304
281- double oldUsage = _ramUsageHistory [ _ramUsageHistory . Count - 1 ] . TotalUsed ;
305+ double oldUsage = _ramUsageHistory [ _ramUsageHistory . Count - 1 ] . TotalUsed ;
282306
283- if ( EmptyWorkingSets )
284- {
285- _ramOptimizer . EmptyWorkingSetFunction ( _processExceptionList ) ;
286- await Task . Delay ( 10000 ) ;
287- }
307+ if ( FillRam )
308+ {
309+ _ramOptimizer . FillRam ( _info , FillRamMaxRuns ) ;
310+ }
288311
289- if ( ClearFileSystemCache )
290- {
291- _ramOptimizer . ClearFileSystemCache ( ClearStandbyCache ) ;
292- }
312+ if ( EmptyWorkingSets )
313+ {
314+ _ramOptimizer . EmptyWorkingSetFunction ( _processExceptionList ) ;
315+ await Task . Delay ( 10000 ) ;
316+ }
293317
294- if ( InvokeGarbageCollector )
295- {
296- GC . Collect ( ) ;
297- GC . WaitForPendingFinalizers ( ) ;
298- }
318+ if ( ClearFileSystemCache )
319+ {
320+ _ramOptimizer . ClearFileSystemCache ( ClearStandbyCache ) ;
321+ }
299322
300- UpdateRamUsage ( ) ;
323+ if ( InvokeGarbageCollector )
324+ {
325+ GC . Collect ( ) ;
326+ GC . WaitForPendingFinalizers ( ) ;
327+ }
301328
302- double newUsage = _ramUsageHistory [ _ramUsageHistory . Count - 1 ] . TotalUsed ;
329+ UpdateRamUsage ( ) ;
303330
304- RamSavings = oldUsage - newUsage ;
331+ double newUsage = _ramUsageHistory [ _ramUsageHistory . Count - 1 ] . TotalUsed ;
332+
333+ RamSavings = oldUsage - newUsage ;
334+ }
335+ catch ( Exception ex )
336+ {
337+ _logController . AddLog ( new ErrorLog ( ex . Message ) ) ;
338+ }
305339 } ) ;
306340
307341 if ( ClearClipboard )
@@ -324,19 +358,26 @@ internal async Task ClearWorkingSets()
324358
325359 await Task . Run ( async ( ) =>
326360 {
327- UpdateRamUsage ( ) ;
361+ try
362+ {
363+ UpdateRamUsage ( ) ;
328364
329- double oldUsage = _ramUsageHistory [ _ramUsageHistory . Count - 1 ] . TotalUsed ;
365+ double oldUsage = _ramUsageHistory [ _ramUsageHistory . Count - 1 ] . TotalUsed ;
330366
331- _ramOptimizer . EmptyWorkingSetFunction ( _processExceptionList ) ;
367+ _ramOptimizer . EmptyWorkingSetFunction ( _processExceptionList ) ;
332368
333- await Task . Delay ( 10000 ) ;
369+ await Task . Delay ( 10000 ) ;
334370
335- UpdateRamUsage ( ) ;
371+ UpdateRamUsage ( ) ;
336372
337- double newUsage = _ramUsageHistory [ _ramUsageHistory . Count - 1 ] . TotalUsed ;
373+ double newUsage = _ramUsageHistory [ _ramUsageHistory . Count - 1 ] . TotalUsed ;
338374
339- RamSavings = oldUsage - newUsage ;
375+ RamSavings = oldUsage - newUsage ;
376+ }
377+ catch ( Exception ex )
378+ {
379+ _logController . AddLog ( new ErrorLog ( ex . Message ) ) ;
380+ }
340381 } ) ;
341382
342383 RamClearingCompletedEvent ? . Invoke ( ) ;
@@ -354,17 +395,24 @@ internal async Task ClearFileSystemCaches()
354395
355396 await Task . Run ( ( ) =>
356397 {
357- UpdateRamUsage ( ) ;
398+ try
399+ {
400+ UpdateRamUsage ( ) ;
358401
359- double oldUsage = _ramUsageHistory [ _ramUsageHistory . Count - 1 ] . TotalUsed ;
402+ double oldUsage = _ramUsageHistory [ _ramUsageHistory . Count - 1 ] . TotalUsed ;
360403
361- _ramOptimizer . ClearFileSystemCache ( ClearStandbyCache ) ;
404+ _ramOptimizer . ClearFileSystemCache ( ClearStandbyCache ) ;
362405
363- UpdateRamUsage ( ) ;
406+ UpdateRamUsage ( ) ;
364407
365- double newUsage = _ramUsageHistory [ _ramUsageHistory . Count - 1 ] . TotalUsed ;
408+ double newUsage = _ramUsageHistory [ _ramUsageHistory . Count - 1 ] . TotalUsed ;
366409
367- RamSavings = oldUsage - newUsage ;
410+ RamSavings = oldUsage - newUsage ;
411+ }
412+ catch ( Exception ex )
413+ {
414+ _logController . AddLog ( new ErrorLog ( ex . Message ) ) ;
415+ }
368416 } ) ;
369417
370418 RamClearingCompletedEvent ? . Invoke ( ) ;
@@ -383,6 +431,10 @@ private void UpdateRamUsage()
383431 double usage = total - Convert . ToDouble ( _info . AvailablePhysicalMemory ) ;
384432 double percentage = usage / total * 100 ;
385433
434+ if ( double . IsNaN ( total ) || double . IsInfinity ( total ) ) throw new ArgumentException ( nameof ( total ) ) ;
435+ if ( double . IsNaN ( usage ) || double . IsInfinity ( usage ) ) throw new ArgumentException ( nameof ( usage ) ) ;
436+ if ( double . IsNaN ( percentage ) || double . IsInfinity ( percentage ) ) throw new ArgumentException ( nameof ( percentage ) ) ;
437+
386438 RamUsage newUsage = new RamUsage ( usage , total , percentage ) ;
387439 if ( EnableRamStatistics )
388440 {
0 commit comments