Created
June 3, 2021 03:26
-
-
Save orange-in-space/d8cfceda3bb1fc46a1d52cda67842d9b to your computer and use it in GitHub Desktop.
EmptyWorkingSetCommand This is the so-called Memory Cleaner. コマンドラインなメモリクリーナー><
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Runtime.InteropServices; | |
namespace EmptyWorkingSetCommand | |
{ | |
class Program | |
{ | |
// EmptyWorkingSetCommand | |
// (c) orange_in_space 2021 | |
// License: CC0 | |
// This is the so-called Memory Cleaner. | |
// Build an example | |
// %windir%\Microsoft.NET\Framework\v3.5\csc.exe /out:OutputFolder\emptyworkingset.exe /target:exe ThisFileFullPathName.cs | |
// (Give it a command name of your choice) | |
// | |
// Description in japanese | |
// メモリクリーナーなコマンド>< | |
// csc.exeかVisual Studioでビルドして使ってね!><; | |
// | |
// -allで全部のプロセス、 | |
// -topで一番メモリ使ってるやつ(ゲームとか) | |
// -othersで一番以外(つまりゲーム以外とか) | |
// 引数は /hoge でも -hogeでもおk | |
// システムのプロセスとかに対しても行いたい場合は管理者権限で実行してください>< | |
// | |
static void Main(string[] args) | |
{ | |
if (args.Contains("-all", new CmdArgsEqualityComparer())) | |
{ | |
EwsAllProcesses(); | |
} | |
else if (args.Contains("-top", new CmdArgsEqualityComparer())) | |
{ | |
EwsTopProcess(); | |
} | |
else if (args.Contains("-others", new CmdArgsEqualityComparer())) | |
{ | |
EwsOtherProcesses(); | |
} | |
else | |
{ | |
Console.WriteLine("[-all] All Processes."); | |
Console.WriteLine("[-top] Process using the most memory."); | |
Console.WriteLine("[-others] other than the top"); | |
} | |
} | |
private static void EwsAllProcesses() | |
{ | |
Process[] processes = Process.GetProcesses(); | |
EmptyWorkingSet(processes); | |
} | |
private static void EwsTopProcess() | |
{ | |
Process[] processes = Process.GetProcesses(); | |
Process[] target = new Process[] { processes.OrderByDescending(p => p.WorkingSet64).First() }; | |
EmptyWorkingSet(target); | |
} | |
private static void EwsOtherProcesses() | |
{ | |
Process[] processes = Process.GetProcesses(); | |
IEnumerable<Process> targets = processes.OrderByDescending(p => p.WorkingSet64).Skip(1); | |
EmptyWorkingSet(targets); | |
} | |
private static void EmptyWorkingSet(IEnumerable<Process> processes) | |
{ | |
foreach (Process target in processes) | |
{ | |
try | |
{ | |
if (!target.HasExited) | |
{ | |
if (EmptyWorkingSet(target.Handle)) | |
{ | |
target.Refresh(); | |
long workingset_after = target.WorkingSet64; | |
} | |
else | |
{ | |
throw new Win32Exception(Marshal.GetLastWin32Error()); | |
} | |
} | |
} | |
catch (Win32Exception win32ex) | |
{ | |
//don't worry | |
//not have permissions error, maybe><; | |
// | |
} | |
} | |
} | |
[DllImport("psapi")] | |
public static extern bool EmptyWorkingSet(IntPtr hProcess); | |
private class CmdArgsEqualityComparer : IEqualityComparer<string> | |
{ | |
public bool Equals(string x, string y) | |
{ | |
return string.Equals(ToCompareString(x), ToCompareString(y), StringComparison.OrdinalIgnoreCase); | |
} | |
public int GetHashCode(string obj) | |
{ | |
return ToCompareString(obj).GetHashCode(); | |
} | |
private static string ToCompareString(string src) | |
{ | |
if (src.First() == '/') | |
{ | |
return "-" + src.Substring(1); | |
} | |
else | |
{ | |
return src; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment