-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
310 lines (265 loc) · 13.3 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
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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* MemClear by Invise Labs / Authors: Mike Lierman
* Copyright Invise Labs 2023.
* Open-source under Creative Commons Non-Commercial Use.
* For use in commercial and for-profit applications, please contact us.
* We can be reached at [email protected].
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Threading;
using System.Collections.Generic;
using Microsoft.VisualBasic.Logging;
using System.IO;
using System.Windows.Forms;
namespace MemClear
{
#region Native Structures
//- (SYSTEM_CACHE_INFORMATION)
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct SYSTEM_CACHE_INFORMATION
{
public long CurrentSize, PeakSize;
public long PageFaultCount;
public long MinimumWorkingSet, MaximumWorkingSet;
public long Unused1, Unused2, Unused3, Unused4;
}
//- (SYSTEM_CACHE_INFORMATION_64_BIT)
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct SYSTEM_CACHE_INFORMATION_64_BIT
{
public long CurrentSize, PeakSize;
public long PageFaultCount;
public long MinimumWorkingSet, MaximumWorkingSet;
public long Unused1, Unused2, Unused3, Unused4;
}
//- (TokPriv1Luid)
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct TokPriv1Luid
{
public int Count, Attr;
public long Luid;
}
#endregion
public class Program
{
#region Declarations
//- (Declaration of constants)
const int SE_PRIVILEGE_ENABLED = 2;
const string SeIncreaseQuotaName = "SeIncreaseQuotaPrivilege";
const string SeProfileSingleName = "SeProfileSingleProcessPrivilege";
const int SystemFileCacheInformation = 0x0015;
const int SystemMemoryListInformation = 0x0050;
const int MemoryPurgeStandbyList = 4;
const int MemoryEmptyWorkingSets = 2;
//- Imports
[DllImport("advapi32.dll", SetLastError = true)]
internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
[DllImport("advapi32.dll", SetLastError = true)]
internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
[DllImport("ntdll.dll")]
public static extern UInt32 NtSetSystemInformation(int InfoClass, IntPtr Info, int Length);
[DllImport("psapi.dll")]
static extern int EmptyWorkingSet(IntPtr hwProc);
#endregion
static bool output, logEnabled = false;
static string _ = string.Empty;
static string logPath = System.IO.Path.GetDirectoryName(Application.ExecutablePath) + "\\log.txt";
//- Entry Point
static void Main(string[] args)
{
Console.Title = "MemClear by Invise Labs"; ;
Console.WriteLine("MemClear by Invise Labs / Authors: Mike Lierman"); //- AUTHORS LINE - Change if interns/others at Invise contribute
Console.WriteLine("Clears Working Set Memory for all processes. Processes may not honor any changes and may try to reclaim their memory.\n");
//- Gert user input and assing output bool
Console.WriteLine("Press ENTER key to continue. Type O for detailed output, type L to log detailed output, type LO for both.");
var l = Console.ReadLine().ToLower();
output = (l.Contains("o")) ? true : false;
logEnabled = (l.Contains("l")) ? true : false;
//- Begin the log if enabled
if (logEnabled) { Log("MemClear by Invise Labs\nNew Session Started"); }
//- Clear console and get ready
ClearConsole();
var before = new PerformanceCounter("Memory", "Available MBytes", true).RawValue.ToString();
var r = logEnabled ? $"/ Log & Output Enabled: {output}" : (output ? $"/ Output Enabled: {output}" : _);
Console.WriteLine($">> Process has begun. {r}");
EmptyWorkingSet();
ClearFileSystemCache(true);
//- Finished, now get memory free after and write details
var after = new PerformanceCounter("Memory", "Available MBytes", true).RawValue.ToString(); ;
double ram = Math.Round((double)new Microsoft.VisualBasic.Devices.ComputerInfo().TotalPhysicalMemory / 1024 / 1024 / 1024, 2);
//- Write finish text
string finishText = $"\nTotal Physical RAM: {ram} GB" +
$"\nAvailable RAM Before: {before} MB / Available RAM After: {after} MB" +
$"{(logEnabled ? "\n\nLog can be found at "+logPath : _)}" +
$"\n\nPress any key to exit.";
Console.WriteLine(finishText);
//- Log finish text if log enabled
if (logEnabled) { Log(finishText); }
//- Wait for input to exit
Console.ReadKey();
Application.Exit();
}
static void ClearConsole()
{
Console.Clear();
Console.WriteLine("MemClear by Invise Labs / Authors: Mike Lierman"); //- AUTHORS LINE - Change if interns/others at Invise contribute
Console.WriteLine("Clears Working Set Memory for all processes.\n");
//Console.WriteLine("");
}
static void ClearCurrentConsoleLine()
{
int currentLineCursor = Console.CursorTop;
Console.SetCursorPosition(0, Console.CursorTop);
Console.Write(new string(' ', Console.WindowWidth));
Console.SetCursorPosition(0, currentLineCursor);
}
static void Log(string text)
{
try
{
using (var logFile = File.Open(logPath, FileMode.OpenOrCreate))
{
logFile.Seek(0, SeekOrigin.End);
using (var s = new StreamWriter(logFile))
{ s.WriteLine($"{DateTime.Now.ToString()}: {text}"); }
}
}
catch { /* Error when writing to error log, that sucks. Oh well. */ }
}
//- A method to empty the working set of all processes
public static void EmptyWorkingSet()
{
//- Declare variables for process name, success list, and fail list
var pName = _;
var successLst = new List<string>();
var failLst = new List<string>();
//- Get all processes and print their count
var procs = Process.GetProcesses();
Console.SetCursorPosition(0, 4);
ClearCurrentConsoleLine();
Console.Write($">> Processes: {procs.Length}");
// Loop through all processes using foreach instead of for
foreach (var p in procs)
{
//- Check if the process is not null
if (p != null)
{
//- Try to empty the working set of the process and add to the success list
try
{
//- Get the process name and print it
pName = p.ProcessName;
ClearCurrentConsoleLine();
Console.Write($">> {pName}");
//- Empty the working set for the process and wait 5ms
EmptyWorkingSet(p.Handle);
Thread.Sleep(10);
successLst.Add(pName);
}
catch (Exception ex)
{
//- Use string interpolation and conditional operator to format the fail list item, _ means discard
failLst.Add($"{pName}{(output ? $": {ex.Message}" : _)}");
if (logEnabled) { Log($"{pName}: {ex.Message}"); }
}
}
}
//- Print the final status and the success and fail lists
ClearCurrentConsoleLine();
Console.WriteLine($">> FINISHED. Cleared memory from {successLst.Count} processes, failed {failLst.Count}");
if (output || logEnabled)
{
string text="\n=====Begin Output====="+
$"\nMemory Freed From: {successLst.Count}"+
"\n_________________________" +
//- Use string.Join instead of a loop to print the list items
string.Join(Environment.NewLine, successLst)+
$"\n\nnFailed: {failLst.Count}"+
"\n_________________________" +
$"\n{string.Join(Environment.NewLine, failLst)}";
if (output) { Console.WriteLine(text); }
if(logEnabled) { Log(text); }
}
}
//- A method to clear the file system cache and optionally the standby list
public static void ClearFileSystemCache(bool clearStandby)
{
try
{
//- Try to increase the quota privilege
if (SetIncreasePrivilege(SeIncreaseQuotaName))
{
uint result; //- Result of the system call
int cacheSize; //- Size of the cache information structure
GCHandle handle; //- Handle to the pinned object
//- Check OS bit size and create the cacheInfo variable
object cacheInfo = Environment.Is64BitOperatingSystem switch
{
true => new SYSTEM_CACHE_INFORMATION
{
MinimumWorkingSet = uint.MaxValue,
MaximumWorkingSet = uint.MaxValue
},
false => new SYSTEM_CACHE_INFORMATION_64_BIT
{
MinimumWorkingSet = -1L,
MaximumWorkingSet = -1L
}
};
cacheSize = Marshal.SizeOf(cacheInfo); //- Cache structure size
handle = GCHandle.Alloc(cacheInfo, GCHandleType.Pinned); //- Pin structure and get a handle
result = NtSetSystemInformation(SystemFileCacheInformation, handle.AddrOfPinnedObject(), cacheSize);
handle.Free(); //- Free the handle
// Check the result and throw an exception if it is not zero
if (result != 0)
{ throw new Exception("NtSetSystemInformation(SystemFileCacheInformation) error: ", new Win32Exception(Marshal.GetLastWin32Error())); }
}
// If the clear standby list parameter is true and the profile privilege can be increased, then clear the standby list
if (clearStandby && SetIncreasePrivilege(SeProfileSingleName))
{
int memStandbySize = Marshal.SizeOf(MemoryPurgeStandbyList); //- Size of memory standby list
GCHandle handle = GCHandle.Alloc(MemoryPurgeStandbyList, GCHandleType.Pinned); //- Pin structure and get a handle
uint result = NtSetSystemInformation(SystemMemoryListInformation, handle.AddrOfPinnedObject(), memStandbySize);
handle.Free(); //- Free the handle
if (result != 0) //- Result should be zero
{ throw new Exception("NtSetSystemInformation(SystemMemoryListInformation) Exception: ", new Win32Exception(Marshal.GetLastWin32Error())); }
}
}
catch (Exception ex)
{
string outText = "Failed to clear File System Cache.";
Console.WriteLine(outText);
//- If the output variable is true, print the exception details
if (output) { Console.WriteLine($"\n{outText}\n{ex.ToString()}"); }
if (logEnabled) { Log($"{outText}\n{ex.ToString()}"); }
}
}
//- Set Increase Priv, bool return success or fail
private static bool SetIncreasePrivilege(string privName)
{
try
{
using (WindowsIdentity winID = WindowsIdentity.GetCurrent(TokenAccessLevels.Query | TokenAccessLevels.AdjustPrivileges))
{
TokPriv1Luid tok = new TokPriv1Luid();
tok.Count = 1;
tok.Luid = 0L;
tok.Attr = SE_PRIVILEGE_ENABLED;
//- Looks up the LUID of the privilege
if (!LookupPrivilegeValue(null, privName, ref tok.Luid))
{ throw new Exception("LookupPrivilegeValue error: ", new Win32Exception(Marshal.GetLastWin32Error())); }
//- Bool variable indicates whether adjusting the privilege failed or not
if (!AdjustTokenPrivileges(winID.Token, false, ref tok, 0, IntPtr.Zero, IntPtr.Zero))
{ throw new Exception("AdjustTokenPrivileges error: ", new Win32Exception(Marshal.GetLastWin32Error())); }
else { return true; }
}
}
catch (Exception ex) { Console.WriteLine($"{(output ? $"Fatal error occured, cannot continue.\n{ex.Message}" : "Fatal SePrivilege error occurred, cannot continue. Press any key to exit.")}"); }
return false;
}
}
}