This repository was archived by the owner on Jan 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
61 lines (57 loc) · 2.09 KB
/
Program.cs
File metadata and controls
61 lines (57 loc) · 2.09 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
using System;
using System.Diagnostics;
using System.Threading;
using System.IO;
namespace vcDWMFix
{
class Program
{
static LogWriter log = new LogWriter();//create a logger
static void Main(string[] args)
{
const int loopTimer = 10000; //10 sec.
const int memtoKill = 1073741824; //1 GiB
while (true)
{
DWMLoop(memtoKill);
Thread.Sleep(loopTimer);
}
}
static void DWMLoop(int memToKill)
{
Process killDWM = Process.GetProcessesByName("dwm")[0];//Use only the first process in the array,
//even though there may be multiple of them (e.g. on shared family PC)
long processPagedMemory = killDWM.PagedMemorySize64; //Getting process paged memory size
if (processPagedMemory >= memToKill)
{
//Making a log entry and killing the process. System restarts it automatically.
log.WriteToLog("right now DWM allocated " + (processPagedMemory / 1048576) + "MiB, killing");
killDWM.Kill();
}
}
}
class LogWriter
{
readonly string logLocation = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\log-" + TimeNow.Replace(':', '-') + ".txt";
public LogWriter()
{
WriteToLog("Starting log");
}
public void WriteToLog(string Message)
{
StackTrace stackTrace = new StackTrace();
string callerFunction = stackTrace.GetFrame(1).GetMethod().Name;
string composedMessage = TimeNow + " - " + callerFunction + ": " + Message;
SWWrite(composedMessage);
}
private void SWWrite(string text)
{
using (StreamWriter writeLog = new StreamWriter(logLocation, true))
{
writeLog.WriteLine(text);
writeLog.Flush();
}
}
static string TimeNow => DateTime.Now.ToString();
}
}