|
| 1 | +using System; |
| 2 | +using System.Runtime.InteropServices; |
| 3 | + |
| 4 | +namespace Bloxstrap.Integrations |
| 5 | +{ |
| 6 | + internal static class AutoOptimizeService |
| 7 | + { |
| 8 | + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] |
| 9 | + private struct MEMORYSTATUSEX |
| 10 | + { |
| 11 | + public uint dwLength; |
| 12 | + public uint dwMemoryLoad; |
| 13 | + public ulong ullTotalPhys; |
| 14 | + public ulong ullAvailPhys; |
| 15 | + public ulong ullTotalPageFile; |
| 16 | + public ulong ullAvailPageFile; |
| 17 | + public ulong ullTotalVirtual; |
| 18 | + public ulong ullAvailVirtual; |
| 19 | + public ulong ullAvailExtendedVirtual; |
| 20 | + } |
| 21 | + |
| 22 | + [DllImport("kernel32.dll", SetLastError = true)] |
| 23 | + private static extern bool GlobalMemoryStatusEx(ref MEMORYSTATUSEX lpBuffer); |
| 24 | + |
| 25 | + private static ulong GetTotalPhysicalMemory() |
| 26 | + { |
| 27 | + var mem = new MEMORYSTATUSEX(); |
| 28 | + mem.dwLength = (uint)Marshal.SizeOf(typeof(MEMORYSTATUSEX)); |
| 29 | + if (GlobalMemoryStatusEx(ref mem)) |
| 30 | + return mem.ullTotalPhys; |
| 31 | + return 0; |
| 32 | + } |
| 33 | + |
| 34 | + public static bool CheckAndApply() |
| 35 | + { |
| 36 | + try |
| 37 | + { |
| 38 | + int cpu = Environment.ProcessorCount; |
| 39 | + ulong totalMem = GetTotalPhysicalMemory(); |
| 40 | + // Thresholds: <=2 logical CPUs or <6GB RAM => low-end |
| 41 | + bool lowEnd = cpu <= 2 || (totalMem > 0 && totalMem < 6UL * 1024 * 1024 * 1024); |
| 42 | + if (lowEnd && !App.Settings.Prop.OptimizeForLowEnd) |
| 43 | + { |
| 44 | + App.Settings.Prop.OptimizeForLowEnd = true; |
| 45 | + try { App.Settings.Save(); } catch { } |
| 46 | + return true; |
| 47 | + } |
| 48 | + } |
| 49 | + catch { /* swallow to avoid crash */ } |
| 50 | + return false; |
| 51 | + } |
| 52 | + } |
| 53 | +} |
0 commit comments