@@ -20,6 +20,7 @@ internal class ComputerService {
2020 public static bool HasSystemWorkingSet => OperatingSystemHelper . IsWindowsXpOrGreater ;
2121 public static bool HasModifiedFileCache => OperatingSystemHelper . IsWindowsXpOrGreater ;
2222 public static bool HasSystemFileCache => OperatingSystemHelper . IsWindowsXpOrGreater ;
23+ public static bool HasRegistryCache => OperatingSystemHelper . IsWindows81OrGreater ;
2324
2425 private WindowsStructs . MemoryStatusEx memoryStatusEx ;
2526
@@ -234,6 +235,27 @@ public void Optimize(Enums.MemoryAreas areas, Enums.OptimizationReason reason) {
234235 }
235236 }
236237
238+ if ( ( areas & Enums . MemoryAreas . RegistryCache ) != 0 ) {
239+ try {
240+ if ( OnOptimizeProgressUpdate != null ) {
241+ value ++ ;
242+ OnOptimizeProgressUpdate ( value , "Registry Cache" ) ;
243+ }
244+
245+ stopwatch . Restart ( ) ;
246+
247+ OptimizeRegistryCache ( ) ;
248+
249+ runtime = runtime . Add ( stopwatch . Elapsed ) ;
250+
251+ infoLog . AppendLine ( string . Format ( infoLogFormat , "Registry Cache" , "Optimized" ,
252+ stopwatch . Elapsed . TotalSeconds , "seconds" ) ) ;
253+ }
254+ catch ( Exception e ) {
255+ errorLog . AppendLine ( string . Format ( errorLogFormat , "Registry Cache" , "Error" , e . GetMessage ( ) ) ) ;
256+ }
257+ }
258+
237259 if ( infoLog . Length > 0 ) {
238260 infoLog . Insert ( 0 ,
239261 $ "{ "Memory areas" . ToUpper ( ) } ({ runtime . TotalSeconds : 0.0} seconds){ Environment . NewLine } { Environment . NewLine } ") ;
@@ -508,6 +530,14 @@ private static void OptimizeSystemFileCache() {
508530 throw new Win32Exception ( Marshal . GetLastWin32Error ( ) ) ;
509531 }
510532
533+ private void OptimizeRegistryCache ( ) {
534+ if ( ! HasRegistryCache )
535+ throw new Exception ( "The Registry Cache optimization is not supported on this version of the operating system" ) ;
536+
537+ if ( NativeMethods . NtSetSystemInformation ( Constants . Windows . SystemInformationClass . SystemRegistryReconciliationInformation , IntPtr . Zero , 0 ) != Constants . Windows . SystemErrorCode . ErrorSuccess )
538+ throw new Win32Exception ( Marshal . GetLastWin32Error ( ) ) ;
539+ }
540+
511541 private static SafeFileHandle OpenVolumeHandle ( string driveLetter ) {
512542 if ( string . IsNullOrWhiteSpace ( driveLetter ) )
513543 return null ;
0 commit comments