-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathProgram.cs
More file actions
521 lines (469 loc) · 20.6 KB
/
Copy pathProgram.cs
File metadata and controls
521 lines (469 loc) · 20.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Threading;
using System.Threading.Tasks;
namespace RAMLIMITER
{
class Program
{
// Use EmptyWorkingSet which is the documented way to trim another processes working set
[DllImport("psapi.dll", SetLastError = true)]
static extern bool EmptyWorkingSet(IntPtr hProcess);
// GlobalMemoryStatusEx to get system memory stats (lightweight)
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool GlobalMemoryStatusEx(ref MEMORYSTATUSEX lpBuffer);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
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;
}
static bool IsAdmin()
{
var id = WindowsIdentity.GetCurrent();
var principal = new WindowsPrincipal(id);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
static void ElevatePrivileges(string[] args)
{
if (IsAdmin())
return;
var arguments = args == null || args.Length == 0 ? string.Empty : string.Join(" ", args);
var psi = new ProcessStartInfo
{
UseShellExecute = true,
WorkingDirectory = Environment.CurrentDirectory,
FileName = Assembly.GetEntryAssembly().Location,
Arguments = arguments,
Verb = "runas"
};
Console.Clear();
Console.WriteLine("RAM Limiter does not currently have admin. privileges.\nWould you like to run as admin? (y/n)");
if (Console.ReadKey(true).Key == ConsoleKey.Y)
{
try
{
Process.Start(psi);
Environment.Exit(0);
}
catch (Exception ex)
{
Console.WriteLine("Could not elevate program.\n" + ex.Message);
Thread.Sleep(2000);
}
}
}
// Choose the process instance with the largest working set (safely)
static int GetLargestProcessIdByName(string name)
{
try
{
var procs = Process.GetProcessesByName(name);
int bestId = -1;
long bestWs = -1;
foreach (var p in procs)
{
try
{
if (p.WorkingSet64 > bestWs)
{
bestWs = p.WorkingSet64;
bestId = p.Id;
}
}
catch
{
// accessing WorkingSet64 can throw for protected processes; ignore
}
finally
{
p.Dispose();
}
}
return bestId;
}
catch
{
return -1;
}
}
// Async monitor that trims working set periodically using EmptyWorkingSet
static async Task MonitorProcessAsync(string name, int intervalMs, CancellationToken ct)
{
name = name?.Trim().ToLower();
if (string.IsNullOrEmpty(name))
return;
double lastReportedLoad = -1;
var reportThrottle = TimeSpan.FromSeconds(5);
var lastReport = DateTime.MinValue;
while (!ct.IsCancellationRequested)
{
int pid = GetLargestProcessIdByName(name);
if (pid == -1)
{
if (DateTime.UtcNow - lastReport > reportThrottle)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"{name}: not running");
Console.ResetColor();
lastReport = DateTime.UtcNow;
}
try { await Task.Delay(intervalMs, ct); } catch (TaskCanceledException) { break; }
continue;
}
try
{
using (var proc = Process.GetProcessById(pid))
{
// Attempt to trim the working set
try
{
if (!EmptyWorkingSet(proc.Handle))
{
// fail silently but log occasionally
if (DateTime.UtcNow - lastReport > reportThrottle)
{
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine($"{name}: EmptyWorkingSet failed (pid {pid})");
Console.ResetColor();
lastReport = DateTime.UtcNow;
}
}
}
catch (Exception ex)
{
if (DateTime.UtcNow - lastReport > reportThrottle)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"{name}: error trimming process (pid {pid}): {ex.Message}");
Console.ResetColor();
lastReport = DateTime.UtcNow;
}
}
}
// Get system memory usage using GlobalMemoryStatusEx (cheap)
var mem = new MEMORYSTATUSEX();
mem.dwLength = (uint)Marshal.SizeOf(typeof(MEMORYSTATUSEX));
if (GlobalMemoryStatusEx(ref mem))
{
double percent = mem.dwMemoryLoad; // already percentage used (0-100)
// Only print when it changes noticeably or throttle frequency
if (Math.Abs(percent - lastReportedLoad) > 1.0 || DateTime.UtcNow - lastReport > reportThrottle)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine($"{name.ToUpper()}: Total RAM usage: {percent:F2}% (pid {pid})");
Console.ResetColor();
lastReportedLoad = percent;
lastReport = DateTime.UtcNow;
}
}
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Error handling {name}: {ex.Message}");
Console.ResetColor();
}
try { await Task.Delay(intervalMs, ct); } catch (TaskCanceledException) { break; }
}
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine($"Stopped monitoring {name}");
Console.ResetColor();
}
class Config
{
public string[] Processes { get; set; }
public int IntervalMs { get; set; } = 5000;
public bool AutoStart { get; set; } = false;
}
static void PrintHelp()
{
Console.WriteLine("Usage: RAMLimiter.exe [options]");
Console.WriteLine("Options:");
Console.WriteLine(" --processes p1,p2 Comma-separated process names to monitor");
Console.WriteLine(" --interval <ms> Interval in milliseconds between trims (default 5000)");
Console.WriteLine(" --autostart Start monitors immediately and run headless");
Console.WriteLine(" --config <path> Path to JSON config file (defaults to ./config.json)");
Console.WriteLine(" --help Show this help");
}
static Config LoadConfig(string path)
{
if (!File.Exists(path))
return null;
try
{
var text = File.ReadAllText(path);
var cfg = new Config();
// crude JSON parsing: processes array
var procList = TryGetJsonArray(text, "processes");
if (procList != null) cfg.Processes = procList.ToArray();
var interval = TryGetJsonNumber(text, "intervalMs");
if (interval.HasValue) cfg.IntervalMs = (int)interval.Value;
var autostart = TryGetJsonBool(text, "autostart");
if (autostart.HasValue) cfg.AutoStart = autostart.Value;
return cfg;
}
catch
{
return null;
}
}
static List<string> TryGetJsonArray(string text, string key)
{
try
{
var idx = text.IndexOf('"' + key + '"', StringComparison.OrdinalIgnoreCase);
if (idx == -1) return null;
var start = text.IndexOf('[', idx);
if (start == -1) return null;
var end = text.IndexOf(']', start);
if (end == -1) return null;
var inside = text.Substring(start + 1, end - start - 1);
var items = new List<string>();
int i = 0;
while (i < inside.Length)
{
// find next quoted string
while (i < inside.Length && inside[i] != '"') i++;
if (i >= inside.Length) break;
int s = i + 1;
int e = inside.IndexOf('"', s);
if (e == -1) break;
var item = inside.Substring(s, e - s).Trim();
if (!string.IsNullOrEmpty(item)) items.Add(item);
i = e + 1;
}
return items;
}
catch { return null; }
}
static double? TryGetJsonNumber(string text, string key)
{
try
{
var idx = text.IndexOf('"' + key + '"', StringComparison.OrdinalIgnoreCase);
if (idx == -1) return null;
var colon = text.IndexOf(':', idx);
if (colon == -1) return null;
int i = colon + 1;
while (i < text.Length && (char.IsWhiteSpace(text[i]) || text[i] == '\"')) i++;
int j = i;
while (j < text.Length && (char.IsDigit(text[j]) || text[j] == '.' || text[j] == '-')) j++;
var numS = text.Substring(i, j - i);
if (double.TryParse(numS, out var v)) return v;
return null;
}
catch { return null; }
}
static bool? TryGetJsonBool(string text, string key)
{
try
{
var idx = text.IndexOf('"' + key + '"', StringComparison.OrdinalIgnoreCase);
if (idx == -1) return null;
var colon = text.IndexOf(':', idx);
if (colon == -1) return null;
int i = colon + 1;
while (i < text.Length && char.IsWhiteSpace(text[i])) i++;
var rest = text.Substring(i).TrimStart();
if (rest.StartsWith("true", StringComparison.OrdinalIgnoreCase)) return true;
if (rest.StartsWith("false", StringComparison.OrdinalIgnoreCase)) return false;
return null;
}
catch { return null; }
}
static void ParseAndApplyArgs(string[] args, out Config cfg, out bool showHelp, out bool runHeadless)
{
cfg = new Config();
showHelp = false;
runHeadless = false;
string configPath = Path.Combine(Directory.GetCurrentDirectory(), "config.json");
for (int i = 0; i < args.Length; i++)
{
var a = args[i];
if (string.Equals(a, "--help", StringComparison.OrdinalIgnoreCase) || string.Equals(a, "-h", StringComparison.OrdinalIgnoreCase))
{
showHelp = true; return;
}
if (string.Equals(a, "--config", StringComparison.OrdinalIgnoreCase) && i + 1 < args.Length)
{
configPath = args[++i];
continue;
}
if (string.Equals(a, "--processes", StringComparison.OrdinalIgnoreCase) && i + 1 < args.Length)
{
cfg.Processes = args[++i].Split(',').Select(s => s.Trim()).Where(s => !string.IsNullOrEmpty(s)).ToArray();
continue;
}
if (string.Equals(a, "--interval", StringComparison.OrdinalIgnoreCase) && i + 1 < args.Length)
{
if (int.TryParse(args[++i], out var ms)) cfg.IntervalMs = Math.Max(100, ms);
continue;
}
if (string.Equals(a, "--autostart", StringComparison.OrdinalIgnoreCase))
{
cfg.AutoStart = true; runHeadless = true; continue;
}
}
var fileCfg = LoadConfig(configPath);
if (fileCfg != null)
{
// apply file values only when not provided by CLI (CLI already wrote into cfg.Processes/IntervalMs/AutoStart)
if ((cfg.Processes == null || cfg.Processes.Length == 0) && fileCfg.Processes != null)
cfg.Processes = fileCfg.Processes;
if (cfg.IntervalMs == 0 && fileCfg.IntervalMs != 0)
cfg.IntervalMs = fileCfg.IntervalMs;
if (!cfg.AutoStart && fileCfg.AutoStart)
cfg.AutoStart = fileCfg.AutoStart;
}
// default to empty processes array
if (cfg.Processes == null) cfg.Processes = new string[0];
}
static int Main(string[] args)
{
ParseAndApplyArgs(args, out var cfg, out var showHelp, out var runHeadless);
if (showHelp)
{
PrintHelp();
return 0;
}
ElevatePrivileges(args);
var monitors = new Dictionary<string, CancellationTokenSource>(StringComparer.OrdinalIgnoreCase);
// If autostart is requested, start monitors from config and run until keypress
if (cfg.AutoStart && cfg.Processes.Length > 0)
{
foreach (var p in cfg.Processes)
StartMonitor(p, cfg.IntervalMs > 0 ? cfg.IntervalMs : 5000, monitors);
Console.WriteLine("Autostart monitors running. Press Enter to exit.");
Console.ReadLine();
foreach (var cts in monitors.Values) try { cts.Cancel(); } catch { }
return 0;
}
bool running = true;
while (running)
{
// existing interactive menu (keeps behavior)
Console.Clear();
Console.WriteLine("Limit Discord: 1");
Console.WriteLine("Limit Chrome: 2");
Console.WriteLine("Limit OBS: 3");
Console.WriteLine("Limit Discord & Chrome: 4");
Console.WriteLine("Limit Custom: 5");
Console.WriteLine("Stop monitoring a process: 6");
Console.WriteLine("List active monitors: 7");
Console.WriteLine("Exit: 0");
var key = Console.ReadKey(true).Key;
switch (key)
{
case ConsoleKey.D1:
case ConsoleKey.NumPad1:
StartMonitor("discord", 5000, monitors);
break;
case ConsoleKey.D2:
case ConsoleKey.NumPad2:
StartMonitor("chrome", 5000, monitors);
break;
case ConsoleKey.D3:
case ConsoleKey.NumPad3:
StartMonitor("obs64", 5000, monitors);
break;
case ConsoleKey.D4:
case ConsoleKey.NumPad4:
StartMonitor("discord", 5000, monitors);
StartMonitor("chrome", 5000, monitors);
break;
case ConsoleKey.D5:
case ConsoleKey.NumPad5:
Console.WriteLine("Enter process names separated by commas (e.g., chrome, obs64, discord):");
var line = Console.ReadLine() ?? string.Empty;
var names = line.Split(',').Select(s => s.Trim()).Where(s => !string.IsNullOrEmpty(s));
foreach (var n in names)
StartMonitor(n, 3000, monitors);
break;
case ConsoleKey.D6:
case ConsoleKey.NumPad6:
Console.WriteLine("Enter process name to stop monitoring:");
var stopName = Console.ReadLine() ?? string.Empty;
StopMonitor(stopName.Trim(), monitors);
break;
case ConsoleKey.D7:
case ConsoleKey.NumPad7:
Console.WriteLine("Active monitors:");
foreach (var k in monitors.Keys)
Console.WriteLine(" - " + k);
Console.WriteLine("Press any key to continue...");
Console.ReadKey(true);
break;
case ConsoleKey.D0:
case ConsoleKey.NumPad0:
running = false;
break;
default:
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Invalid input. Try again.");
Thread.Sleep(1000);
Console.ResetColor();
break;
}
}
// Shutdown monitors
foreach (var cts in monitors.Values)
{
try { cts.Cancel(); } catch { }
}
// allow tasks to finish briefly
Thread.Sleep(500);
return 0;
}
static void StartMonitor(string name, int intervalMs, Dictionary<string, CancellationTokenSource> monitors)
{
if (string.IsNullOrWhiteSpace(name))
return;
name = name.Trim().ToLower();
if (monitors.ContainsKey(name))
{
Console.WriteLine($"Already monitoring {name}");
Thread.Sleep(800);
return;
}
var cts = new CancellationTokenSource();
monitors[name] = cts;
Task.Run(() => MonitorProcessAsync(name, intervalMs, cts.Token));
Console.WriteLine($"Started monitoring {name}");
Thread.Sleep(500);
}
static void StopMonitor(string name, Dictionary<string, CancellationTokenSource> monitors)
{
if (string.IsNullOrWhiteSpace(name))
return;
name = name.Trim().ToLower();
if (!monitors.TryGetValue(name, out var cts))
{
Console.WriteLine($"Not monitoring {name}");
Thread.Sleep(800);
return;
}
try
{
cts.Cancel();
}
catch { }
monitors.Remove(name);
Console.WriteLine($"Stopped monitoring {name}");
Thread.Sleep(500);
}
}
}