// MaverickOom.exe — pressao de commit dosada, com liberacao imediata. // Leva o Windows a ~90% do commit limit e observa o alvo sair sozinho. // Nao abre handle no alvo: so Process.GetProcessesByName (barato, sem guard). // csc /nologo /optimize+ /out:MaverickOom.exe MaverickOom.cs // MaverickOom.exe --target nvstreamer --max-gb 70 --step-mb 256 --ceiling 0.90 --hold 30 using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Runtime.InteropServices; using System.Text; using System.Threading; internal static class MaverickOom { [StructLayout(LayoutKind.Sequential)] struct MEMORYSTATUSEX { public uint dwLength; public uint dwMemoryLoad; public ulong ullTotalPhys; public ulong ullAvailPhys; public ulong ullTotalPageFile; public ulong ullAvailPageFile; public ulong ullTotalVirtual; public ulong ullAvailVirtual; public ulong ullAvailExtendedVirtual; } [DllImport("kernel32.dll", SetLastError = true)] static extern bool GlobalMemoryStatusEx(ref MEMORYSTATUSEX st); const int PAGE_READWRITE = 0x04; const int MEM_COMMIT = 0x1000; const int MEM_RESERVE = 0x2000; const int MEM_RELEASE = 0x8000; [DllImport("kernel32.dll", SetLastError = true)] static extern IntPtr VirtualAlloc(IntPtr addr, UIntPtr size, uint type, uint protect); [DllImport("kernel32.dll", SetLastError = true)] static extern bool VirtualFree(IntPtr addr, UIntPtr size, uint type); static readonly List Blocks = new List(); static readonly StringBuilder LogBuf = new StringBuilder(); static string LogPath = "MaverickOom.log"; static volatile bool StopRequested = false; static void Log(string m) { string line = DateTime.Now.ToString("HH:mm:ss.fff") + " " + m; LogBuf.AppendLine(line); Console.WriteLine(line); try { File.WriteAllText(LogPath, LogBuf.ToString()); } catch { } } static double CommitRatio(out ulong used, out ulong limit) { var st = new MEMORYSTATUSEX(); st.dwLength = (uint)Marshal.SizeOf(typeof(MEMORYSTATUSEX)); GlobalMemoryStatusEx(ref st); limit = st.ullTotalPageFile; used = limit - st.ullAvailPageFile; return limit == 0 ? 0 : (double)used / limit; } static bool Alive(string name) { try { return Process.GetProcessesByName(name).Length > 0; } catch { return false; } } static void Release() { if (Blocks.Count == 0) return; long freed = 0; foreach (IntPtr p in Blocks) { if (p == IntPtr.Zero) continue; // MEM_RELEASE exige size 0; devolve a reserva inteira if (VirtualFree(p, UIntPtr.Zero, MEM_RELEASE)) freed++; } Log("liberados " + freed + "/" + Blocks.Count + " blocos"); Blocks.Clear(); } static int Main(string[] args) { string target = "nvstreamer"; double maxGb = 70; int stepMb = 256; double ceiling = 0.90; int hold = 30; for (int i = 0; i < args.Length; i++) { string a = args[i]; if (a == "--target" && i + 1 < args.Length) target = args[++i]; else if (a == "--max-gb" && i + 1 < args.Length) double.TryParse(args[++i], out maxGb); else if (a == "--step-mb" && i + 1 < args.Length) int.TryParse(args[++i], out stepMb); else if (a == "--ceiling" && i + 1 < args.Length) double.TryParse(args[++i], out ceiling); else if (a == "--hold" && i + 1 < args.Length) int.TryParse(args[++i], out hold); } string dir = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName); LogPath = Path.Combine(dir ?? ".", "MaverickOom.log"); Log("=== MaverickOom target=" + target + " max=" + maxGb + "GB step=" + stepMb + "MB ceiling=" + ceiling + " hold=" + hold + "s"); Log("user=" + Environment.UserName + " session=" + Process.GetCurrentProcess().SessionId); ulong used, limit; double r = CommitRatio(out used, out limit); Log(string.Format("commit inicial: {0:F1}/{1:F1} GB ({2:P1})", used / 1073741824.0, limit / 1073741824.0, r)); bool targetAtStart = Alive(target); Log("alvo " + target + " vivo no inicio: " + targetAtStart); Console.CancelKeyPress += (s, e) => { e.Cancel = true; StopRequested = true; Log("Ctrl+C: liberando..."); }; long allocated = 0; long maxBytes = (long)(maxGb * 1073741824.0); bool targetDied = false; int stepBytes = stepMb * 1024 * 1024; try { while (!StopRequested) { r = CommitRatio(out used, out limit); if (r >= ceiling) { Log(string.Format("teto atingido: {0:P1} — parando de alocar", r)); break; } if (allocated >= maxBytes) { Log("limite --max-gb atingido: " + (allocated / 1073741824.0) + " GB"); break; } IntPtr p = VirtualAlloc(IntPtr.Zero, (UIntPtr)stepBytes, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); if (p == IntPtr.Zero) { Log("VirtualAlloc falhou err=" + Marshal.GetLastWin32Error() + " após " + (allocated / 1073741824.0) + " GB — commit esgotado"); break; } Blocks.Add(p); allocated += stepBytes; // TOCA TODAS as paginas: sem isso o Windows so reserva commit e nao ocupa RAM int touched = 0; for (int off = 0; off < stepBytes; off += 4096) { Marshal.WriteByte(p, off, 1); touched++; } if ((Blocks.Count % 16) == 0) Log(" (toque: " + touched + " paginas neste bloco)"); if ((Blocks.Count % 16) == 0) { r = CommitRatio(out used, out limit); ulong physUsed = 0; var ms = new MEMORYSTATUSEX(); ms.dwLength = (uint)Marshal.SizeOf(typeof(MEMORYSTATUSEX)); if (GlobalMemoryStatusEx(ref ms)) physUsed = ms.ullTotalPhys - ms.ullAvailPhys; Log(string.Format("alocado {0:F1} GB | commit {1:P1} ({2:F1}/{3:F1} GB) | phys {4:F1} GB", allocated / 1073741824.0, r, used / 1073741824.0, limit / 1073741824.0, physUsed / 1073741824.0)); } if (targetAtStart && !Alive(target)) { targetDied = true; Log("*** ALVO MORREU durante a pressao (alocado " + (allocated / 1073741824.0) + " GB) ***"); break; } Thread.Sleep(15); } Log("segurando pressao por " + hold + "s..."); var end = DateTime.UtcNow.AddSeconds(hold); while (DateTime.UtcNow < end && !StopRequested) { if (targetAtStart && !Alive(target)) { targetDied = true; Log("*** ALVO MORREU durante a espera ***"); break; } Thread.Sleep(250); } } finally { Release(); r = CommitRatio(out used, out limit); Log(string.Format("commit final: {0:F1}/{1:F1} GB ({2:P1})", used / 1073741824.0, limit / 1073741824.0, r)); Log("veredito: " + (targetDied ? "ALVO CAIU SOZINHO (OOM)" : "alvo sobreviveu a pressao")); Log("alvo vivo no fim: " + Alive(target)); } return targetDied ? 10 : 0; } }