-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
171 lines (143 loc) · 5.76 KB
/
Program.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.IO;
using Microsoft.VisualBasic.Devices;
using System.Diagnostics;
using System.Management;
namespace QuitStealingRamDotNet
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("QSR.Net - .Net/C# port (more of a rewrite) of 0x3F's QuitStealingRam OOM protection");
Config cfg = new Config("qsr.cfg");
ComputerInfo ci;
Process[] processes;
while (true)
{
Thread.Sleep(cfg.IntervalMs);
ci = new ComputerInfo();
if((int)(ci.AvailablePhysicalMemory / 1024 / 1024) <= cfg.LowMemThreshold)
{
Console.WriteLine("MURDER SPREE STARTED");
processes = Process.GetProcesses();
for(int i = 0; i < cfg.Killstreak; i++)
{
Process proc = processes.FirstOrDefault();
for(int p = 0; p < processes.Length; p++)
{
if (processes[p].PrivateMemorySize64 > proc.PrivateMemorySize64)
proc = processes[p];
}
bool kill = false;
if (!cfg.DeathTo)
{
foreach (string s in cfg.Shitlist)
{
if (s.ToLower().Trim().Contains(proc.ProcessName.Trim().ToLower()))
{
kill = true;
break;
}
}
}
else
{
kill = true;
}
if(kill)
{
try
{
KillProc(proc, cfg.DoubleHomicide, cfg.Prolicide);
}
catch (Exception ex)
{
Console.WriteLine("Couldn't kill. Error: " + ex.Message);
}
}
}
processes = null;
}
ci = null;
}
}
static void KillProc(Process p, bool doubleHomicide, bool prolicide)
{
if(!doubleHomicide)
{
if(prolicide)
foreach (Process child in p.GetChildProcesses())
child.Kill();
p.Kill();
return;
}
// If, for whatever reason, this isn't being run on windows, remove all the following code and remove all references and uses of the doublehomicide bool.
string args = string.Format("/PID {0} /F", p.Id);
if (prolicide)
args += " /T";
Process.Start("taskkill.exe", args);
p.Kill();
}
}
public class Config
{
/// <summary>
/// If available system RAM is less than this (in megabytes), the murder spree starts
/// </summary>
public int LowMemThreshold = 256;
/// <summary>
/// Interval between checks in miliseconds
/// </summary>
public int IntervalMs = 2000;
/// <summary>
/// Tries to kill the process in every possible way, disregarding all pleas for mercy
/// </summary>
public bool DoubleHomicide = true;
/// <summary>
/// The amount of processes to kill in descending order from most RAM used
/// </summary>
public int Killstreak = 1;
/// <summary>
/// If true, also murder all the children processes of the process being killed.
/// </summary>
public bool Prolicide = true;
/// <summary>
/// Only processes whose names contain something on this list will be killed
/// This is a comma separated string in the config file
/// It's reccommended to leave itself in the shitlist because it might end up leaking more memory than it saves
/// so it saves your computer from an OOM state by suiciding.
/// </summary>
public readonly string[] Shitlist = { "google chrome", "discord", "quitstealingramdotnet" };
/// <summary>
/// Always kills the process with the highest memory usage regardless of whether or not it's on the shitlist. #DeathTo
/// </summary>
public bool DeathTo = false;
public Config(string configPath)
{
if (!File.Exists(configPath))
{
Console.WriteLine("A config file was not located or misplaced or something. Using the excellent defaults.");
return;
}
}
}
public static class ProcessExtensions
{
public static IEnumerable<Process> GetChildProcesses(this Process process)
{
List<Process> children = new List<Process>();
ManagementObjectSearcher mos = new ManagementObjectSearcher(String.Format("Select * From Win32_Process Where ParentProcessID={0}", process.Id));
foreach (ManagementObject mo in mos.Get())
{
children.Add(Process.GetProcessById(Convert.ToInt32(mo["ProcessID"])));
}
return children;
}
}
}