public static long GetPhysicallyInstalledSystemMemory() { if (NativeMethods.GetPhysicallyInstalledSystemMemory(out var totalMemoryInKilobytes)) { return(checked (Convert.ToInt64(totalMemoryInKilobytes) * KiBiByte)); } var memory = MEMORYSTATUSEX.Create(); if (NativeMethods.GlobalMemoryStatusEx(ref memory)) { return(Convert.ToInt64(memory.ullTotalPhys)); } throw new InvalidOperationException( @"Failed to determine the size of the physically installed system memory.", new Win32Exception(Marshal.GetLastWin32Error())); }
static void CheckMemoryEnterPoint() { CHECK: var status = MEMORYSTATUSEX.Create(); if (GlobalMemoryStatusEx(ref status)) { if (status.ullAvailPhys < (ulong)PreservedMemory) { WriteLine("Avaliable Memory = {0:##,###} < {1:##,###} .", status.ullAvailPhys, PreservedMemory); KillApplications(TargetPrcesses); } Console.Title = string.Format("Avaliable Memory: {0:##,###}/{1:##,###}", status.ullAvailPhys, status.ullTotalPhys); } // Check memory every 5 seconds. Thread.Sleep(5000); goto CHECK; }