Skip to content

Commit

Permalink
custom multiple programs
Browse files Browse the repository at this point in the history
  • Loading branch information
0vm authored Jul 22, 2023
1 parent ab7bdcb commit 468a2a7
Showing 1 changed file with 55 additions and 56 deletions.
111 changes: 55 additions & 56 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
using System.Linq;
using System.Management;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace RAMLIMITER
{
Expand All @@ -17,6 +15,61 @@ class Program
[DllImport("kernel32.dll")]
static extern bool SetProcessWorkingSetSize(IntPtr proc, int min, int max);

// Method to get all processes matching the custom process name
public static List<Process> GetCustom(string processName)
{
return Process.GetProcessesByName(processName).ToList();
}

// Method to limit RAM usage of multiple custom processes
static void CustomRamLimiter(int min, int max)
{
Console.WriteLine("Type Process Names Separated by Commas (e.g., chrome,obs,discord):");
string processNamesInput = Console.ReadLine();
List<string> processNames = processNamesInput.Split(',').Select(p => p.Trim().ToLower()).ToList();

List<Process> processesToLimit = new List<Process>();
foreach (var processName in processNames)
{
processesToLimit.AddRange(GetCustom(processName));
}

foreach (var process in processesToLimit)
{
new Thread(() =>
{
Thread.CurrentThread.IsBackground = true;
while (!process.HasExited)
{
GC.Collect();
GC.WaitForPendingFinalizers();
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
SetProcessWorkingSetSize(process.Handle, min, max);
}

var wmiObject = new ManagementObjectSearcher("select * from Win32_OperatingSystem");

var memoryValues = wmiObject.Get().Cast<ManagementObject>().Select(mo => new
{
FreePhysicalMemory = Double.Parse(mo["FreePhysicalMemory"].ToString()),
TotalVisibleMemorySize = Double.Parse(mo["TotalVisibleMemorySize"].ToString())
}).FirstOrDefault();

if (memoryValues != null)
{
var percent = ((memoryValues.TotalVisibleMemorySize - memoryValues.FreePhysicalMemory) / memoryValues.TotalVisibleMemorySize) * 100;
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(process.ProcessName + ": Total RAM usage: {0}", percent);
Thread.Sleep(3000);
}
}
}).Start();
}

Thread.Sleep(-1);
}

// Method to get the process ID of Discord
public static int GetDiscord()
{
Expand Down Expand Up @@ -49,22 +102,6 @@ public static int GetChrome()
return chromeId;
}

// Method to get the process ID of a custom process
public static int GetCustom()
{
int OBSId = -1;
long workingSet2 = 0;
foreach (Process OBS in Process.GetProcessesByName(variableger))
{
if (OBS.WorkingSet64 > workingSet2)
{
workingSet2 = OBS.WorkingSet64;
OBSId = OBS.Id;
}
}
return OBSId;
}

// Method to get the process ID of OBS
public static int GetOBS()
{
Expand Down Expand Up @@ -270,44 +307,6 @@ static void OBSRamLimiter(int min, int max)
}
}

// Method to limit RAM usage of a custom process
static void CustomRamLimiter(int min, int max)
{
Console.WriteLine("Type Process Name Like Chrome or OBS");
variableger = Console.ReadLine();

while (GetCustom() != -1)
{
if (GetCustom() != -1)
{
GC.Collect();
GC.WaitForPendingFinalizers();

if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
SetProcessWorkingSetSize(Process.GetProcessById(GetCustom()).Handle, min, max);
}

var wmiObject = new ManagementObjectSearcher("select * from Win32_OperatingSystem");

var memoryValues = wmiObject.Get().Cast<ManagementObject>().Select(mo => new {
FreePhysicalMemory = Double.Parse(mo["FreePhysicalMemory"].ToString()),
TotalVisibleMemorySize = Double.Parse(mo["TotalVisibleMemorySize"].ToString())
}).FirstOrDefault();

if (memoryValues != null)
{
var percent = ((memoryValues.TotalVisibleMemorySize - memoryValues.FreePhysicalMemory) / memoryValues.TotalVisibleMemorySize) * 100;
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(variableger + ": Total RAM usage: {0}", percent);
Thread.Sleep(3000);
}

Thread.Sleep(1);
}
}
}

static void Main(string[] args)
{
new Thread(() => // taken from pinger, originally from zf9
Expand Down

0 comments on commit 468a2a7

Please sign in to comment.