Skip to content

Commit 93e8dde

Browse files
committed
Optimize RegistryCache
1 parent 26b9ebe commit 93e8dde

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

winMemoryOptimizer/Engine/ComputerService.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

winMemoryOptimizer/Engine/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public static class SystemInformationClass {
2424
public const int SystemCombinePhysicalMemoryInformation = 130; // 0x82
2525
public const int SystemFileCacheInformation = 21; // 0x15
2626
public const int SystemMemoryListInformation = 80; // 0x50
27+
public const int SystemRegistryReconciliationInformation = 155; // 0x9B
2728
}
2829

2930
public static class SystemMemoryListCommand {

winMemoryOptimizer/Engine/Enums.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public enum MemoryAreas {
2323
SystemWorkingSet = 32,
2424
ModifiedFileCache = 64,
2525
SystemFileCache = 128,
26+
RegistryCache = 256,
2627
}
2728

2829
public enum MemoryUnit {

winMemoryOptimizer/TrayApplicationContext.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ private static Enums.MemoryAreas GetEnabledMemoryAreas() {
218218
Settings.MemoryAreas &= ~Enums.MemoryAreas.ModifiedFileCache;
219219
if (!ComputerService.HasSystemFileCache)
220220
Settings.MemoryAreas &= ~Enums.MemoryAreas.SystemFileCache;
221+
if (!ComputerService.HasRegistryCache)
222+
Settings.MemoryAreas &= ~Enums.MemoryAreas.RegistryCache;
221223
return Settings.MemoryAreas;
222224
}
223225

@@ -238,6 +240,8 @@ private static int GetEnabledMemoryAreasCount() {
238240
result++;
239241
if (Settings.MemoryAreas.HasFlag(Enums.MemoryAreas.SystemFileCache))
240242
result++;
243+
if (Settings.MemoryAreas.HasFlag(Enums.MemoryAreas.RegistryCache))
244+
result++;
241245
return result;
242246
}
243247

@@ -522,6 +526,9 @@ private void AddMenuItems() {
522526
optimizationTypesMenu.DropDownItems.Add(new ToolStripMenuItem("System File Cache", null, (_, _) => {
523527
ToggleMemoryArea(Enums.MemoryAreas.SystemFileCache);
524528
}) { Tag = Enums.MemoryAreas.SystemFileCache });
529+
optimizationTypesMenu.DropDownItems.Add(new ToolStripMenuItem("Registry Cache", null, (_, _) => {
530+
ToggleMemoryArea(Enums.MemoryAreas.RegistryCache);
531+
}) { Tag = Enums.MemoryAreas.RegistryCache });
525532
}
526533
optimizationTypesMenu.DropDown.Closing += OnContextMenuStripClosing;
527534
UpdateAreasMenuItems();

0 commit comments

Comments
 (0)