Skip to content

Commit 1f00cd3

Browse files
committed
Add project files.
1 parent b63e9c4 commit 1f00cd3

37 files changed

+4225
-0
lines changed

Compact RAM Cleaner.sln

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.6.33815.320
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Compact RAM Cleaner", "Compact RAM Cleaner\Compact RAM Cleaner.csproj", "{99DB9455-6C3E-4A4A-80D3-787A764751E1}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{99DB9455-6C3E-4A4A-80D3-787A764751E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{99DB9455-6C3E-4A4A-80D3-787A764751E1}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{99DB9455-6C3E-4A4A-80D3-787A764751E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{99DB9455-6C3E-4A4A-80D3-787A764751E1}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {FEABB3D2-8716-4E27-91D2-BFFB03CA9234}
24+
EndGlobalSection
25+
EndGlobal

Compact RAM Cleaner/App.config

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
5+
</startup>
6+
</configuration>
+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.Runtime.InteropServices;
4+
using System.Security.Principal;
5+
using System.Threading.Tasks;
6+
using static Compact_RAM_Cleaner.Memory;
7+
8+
namespace Compact_RAM_Cleaner
9+
{
10+
public static class Cleaner
11+
{
12+
[DllImport("psapi.dll")] static extern int EmptyWorkingSet([In] IntPtr obj0);
13+
[DllImport("advapi32.dll", SetLastError = true)] internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
14+
[DllImport("advapi32.dll", SetLastError = true)] internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
15+
[DllImport("ntdll.dll")] static extern uint NtSetSystemInformation(int InfoClass, IntPtr Info, int Length);
16+
[StructLayout(LayoutKind.Sequential, Pack = 1)] internal struct TokPriv1Luid { public int Count; public long Luid; public int Attr; }
17+
[StructLayout(LayoutKind.Sequential, Pack = 1)]
18+
struct SYSTEM_CACHE_INFORMATION
19+
{
20+
public long CurrentSize;
21+
public long PeakSize;
22+
public long PageFaultCount;
23+
public long MinimumWorkingSet;
24+
public long MaximumWorkingSet;
25+
public long Unused1;
26+
public long Unused2;
27+
public long Unused3;
28+
public long Unused4;
29+
}
30+
31+
static readonly bool _is64Bit = Environment.Is64BitOperatingSystem;
32+
33+
static bool _duringCleaning;
34+
static bool _duringAutoCleaning;
35+
static int _autoCleanerValue;
36+
37+
public static async void EnableAutoCleaner(int value)
38+
{
39+
_autoCleanerValue = value;
40+
if (_duringAutoCleaning) return;
41+
_duringAutoCleaning = true;
42+
43+
while (_duringAutoCleaning)
44+
{
45+
var used = (int)((TotalPhysicalMemory - AvailablePhysicalMemory) * 100 / TotalPhysicalMemory);
46+
47+
if (used >= _autoCleanerValue)
48+
Clear();
49+
50+
await Task.Delay(30000);
51+
}
52+
}
53+
54+
public static void DisableAutoCleaner()
55+
{
56+
_duringAutoCleaning = false;
57+
}
58+
59+
public static async void ClearRAM()
60+
{
61+
if (_duringCleaning) return;
62+
_duringCleaning = true;
63+
64+
var before = AvailablePhysicalMemory;
65+
66+
Clear();
67+
68+
if (Popup.ShowCleaningResult)
69+
Popup.Show(AvailablePhysicalMemory - before);
70+
71+
await Task.Delay(2000);
72+
_duringCleaning = false;
73+
}
74+
75+
static void Clear()
76+
{
77+
var processes = Process.GetProcesses();
78+
for (int i = 0; i < processes.Length; i++)
79+
{
80+
try { EmptyWorkingSet(processes[i].Handle); }
81+
catch { }
82+
}
83+
}
84+
85+
public static void ClearCache()
86+
{
87+
try
88+
{
89+
if (SetIncreasePrivilege("SeIncreaseQuotaPrivilege"))
90+
{
91+
var sc = new SYSTEM_CACHE_INFORMATION { MinimumWorkingSet = _is64Bit ? -1L : uint.MaxValue, MaximumWorkingSet = _is64Bit ? -1L : uint.MaxValue };
92+
var sys = Marshal.SizeOf(sc);
93+
var gcHandle = GCHandle.Alloc(sc, GCHandleType.Pinned);
94+
var num = NtSetSystemInformation(0x0015, gcHandle.AddrOfPinnedObject(), sys);
95+
gcHandle.Free();
96+
}
97+
98+
if (SetIncreasePrivilege("SeProfileSingleProcessPrivilege"))
99+
{
100+
var sys = Marshal.SizeOf(4);
101+
var gcHandle = GCHandle.Alloc(4, GCHandleType.Pinned);
102+
var num = NtSetSystemInformation(0x0050, gcHandle.AddrOfPinnedObject(), sys);
103+
gcHandle.Free();
104+
}
105+
}
106+
catch { }
107+
}
108+
109+
static bool SetIncreasePrivilege(string privilegeName)
110+
{
111+
using (var current = WindowsIdentity.GetCurrent(TokenAccessLevels.Query | TokenAccessLevels.AdjustPrivileges))
112+
{
113+
TokPriv1Luid tokPriv1Luid;
114+
tokPriv1Luid.Count = 1;
115+
tokPriv1Luid.Luid = 0L;
116+
tokPriv1Luid.Attr = 2;
117+
if (!LookupPrivilegeValue(null, privilegeName, ref tokPriv1Luid.Luid)) return false;
118+
return AdjustTokenPrivileges(current.Token, false, ref tokPriv1Luid, 0, IntPtr.Zero, IntPtr.Zero);
119+
}
120+
}
121+
}
122+
}

Compact RAM Cleaner/Classes/Memory.cs

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using Microsoft.VisualBasic.Devices;
2+
using System;
3+
using System.Management;
4+
5+
namespace Compact_RAM_Cleaner
6+
{
7+
public static class Memory
8+
{
9+
static readonly ComputerInfo _computerInfo = new ComputerInfo();
10+
public static ulong TotalPhysicalMemory => _computerInfo.TotalPhysicalMemory;
11+
public static ulong AvailablePhysicalMemory => _computerInfo.AvailablePhysicalMemory;
12+
13+
public static double GetPageFileMaxSize()
14+
{
15+
try
16+
{
17+
double size = 0;
18+
using (var query = new ManagementObjectSearcher("SELECT MaximumSize FROM Win32_PageFile"))
19+
{
20+
foreach (var obj in query.Get())
21+
size = (uint)obj.GetPropertyValue("MaximumSize");
22+
return Math.Round(size / 1024, 1);
23+
}
24+
}
25+
26+
catch
27+
{
28+
return 0;
29+
}
30+
}
31+
32+
public static double GetPageFileUsage()
33+
{
34+
try
35+
{
36+
double size = 0;
37+
using (var query = new ManagementObjectSearcher("SELECT CurrentUsage FROM Win32_PageFileUsage"))
38+
{
39+
foreach (var obj in query.Get())
40+
size = (uint)obj.GetPropertyValue("CurrentUsage");
41+
return Math.Round(size / 1024, 1);
42+
}
43+
}
44+
45+
catch
46+
{
47+
return 0;
48+
}
49+
}
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
using System;
2+
using System.Drawing;
3+
using System.Drawing.Drawing2D;
4+
using System.Drawing.Text;
5+
using System.Runtime.InteropServices;
6+
using System.Windows.Forms;
7+
8+
namespace Compact_RAM_Cleaner
9+
{
10+
public class MemoryUsageVisualization
11+
{
12+
[DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
13+
static extern IntPtr CreateRoundRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidthEllipse, int nHeightEllipse);
14+
15+
[DllImport("Gdi32.dll", EntryPoint = "DeleteObject")]
16+
static extern IntPtr DeleteObject(IntPtr hObject);
17+
18+
readonly Panel _panel;
19+
Color _backgroundFillColor;
20+
readonly IMemoryUsageProvider _memoryUsageProvider;
21+
22+
public MemoryUsageVisualization(Panel panel, IMemoryUsageProvider memoryUsageProvider, Action onClick = null)
23+
{
24+
_panel = panel;
25+
_backgroundFillColor = _panel.BackColor;
26+
_memoryUsageProvider = memoryUsageProvider;
27+
28+
PaintPanel();
29+
30+
if (onClick != null)
31+
{
32+
var defaultColor = _backgroundFillColor;
33+
34+
_panel.Click += (s, e) => onClick();
35+
36+
_panel.MouseEnter += (s, e) =>
37+
{
38+
_backgroundFillColor = Color.FromArgb(defaultColor.R + 10, defaultColor.G + 10, defaultColor.B + 10);
39+
_panel.Refresh();
40+
};
41+
42+
_panel.MouseLeave += (s, e) =>
43+
{
44+
_backgroundFillColor = defaultColor;
45+
_panel.Refresh();
46+
};
47+
}
48+
}
49+
50+
public void Update() => _panel.Refresh();
51+
52+
void PaintPanel()
53+
{
54+
int width = _panel.Width - 2;
55+
int height = _panel.Height - 2;
56+
57+
_panel.Paint += (s, e) =>
58+
{
59+
IntPtr ptr = CreateRoundRectRgn(0, 0, _panel.Width, _panel.Height, _panel.Width, _panel.Width);
60+
_panel.Region = Region.FromHrgn(ptr);
61+
DeleteObject(ptr);
62+
63+
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
64+
using (var backgroundPen = new Pen(Color.FromArgb(32, 33, 36), 2))
65+
using (var background = new SolidBrush(_backgroundFillColor))
66+
using (var pen = new Pen(Color.FromArgb(117, 162, 247), 4) { StartCap = LineCap.Round, EndCap = LineCap.Round })
67+
{
68+
e.Graphics.FillPie(background, 5, 5, width - 10, height - 10, -90, 360);
69+
e.Graphics.DrawArc(backgroundPen, 4, 4, width - 8, height - 8, -90, 360);
70+
e.Graphics.DrawArc(pen, 4, 4, width - 8, height - 8, -90, (int)Math.Round(360.0 / 100 * _memoryUsageProvider.CurrentUsage));
71+
}
72+
73+
using (var font = new Font("Tahoma", 12F))
74+
using (var font2 = new Font("Tahoma", 7F))
75+
using (var sf = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center })
76+
{
77+
GetPositions(out var usagePosition, out var percentPosition);
78+
e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
79+
e.Graphics.DrawString(_memoryUsageProvider.CurrentUsageString, font, SystemBrushes.Control, usagePosition, _panel.Height / 2, sf);
80+
e.Graphics.DrawString("%", font2, SystemBrushes.ControlDark, percentPosition, _panel.Height / 2 + 2, sf);
81+
82+
}
83+
};
84+
}
85+
86+
void GetPositions(out int usagePosition, out int percentPosition)
87+
{
88+
usagePosition = _panel.Width / 2;
89+
percentPosition = _panel.Width / 2;
90+
91+
if (_memoryUsageProvider.CurrentUsage > 10 && _memoryUsageProvider.CurrentUsage < 100)
92+
{
93+
usagePosition += -1;
94+
percentPosition += 15;
95+
}
96+
else if (_memoryUsageProvider.CurrentUsage < 10)
97+
{
98+
percentPosition += 10;
99+
}
100+
else
101+
{
102+
usagePosition += -2;
103+
percentPosition += 19;
104+
}
105+
}
106+
}
107+
}

Compact RAM Cleaner/Classes/Popup.cs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace Compact_RAM_Cleaner
2+
{
3+
public class Popup
4+
{
5+
public static bool ShowCleaningResult = true;
6+
7+
public static void Show(string text)
8+
{
9+
new Notify(text).Show();
10+
}
11+
12+
public static void Show(double memoryReleased)
13+
{
14+
if (ShowCleaningResult)
15+
new Notify(memoryReleased).Show();
16+
}
17+
}
18+
}
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using System.Linq;
4+
5+
namespace Compact_RAM_Cleaner
6+
{
7+
public static class SaveSystem
8+
{
9+
static Dictionary<string, string> _data;
10+
11+
public static string GetValue(string key) => _data.TryGetValue(key, out var value) ? value : null;
12+
public static bool TryGetValue(string key, out string value) => _data.TryGetValue(key, out value);
13+
14+
public static bool Load()
15+
{
16+
if (File.Exists(Paths.IniFile))
17+
{
18+
try
19+
{
20+
var data = File.ReadAllLines(Paths.IniFile).Where(x => x.Contains("="));
21+
_data = data.ToDictionary(k => k.Substring(0, k.IndexOf("=")), v => v.Substring(v.IndexOf("=") + 1));
22+
}
23+
catch { }
24+
}
25+
26+
return _data != null;
27+
}
28+
29+
public static void Save(string key, string value)
30+
{
31+
if (File.Exists(Paths.IniFile))
32+
{
33+
var data = File.ReadAllLines(Paths.IniFile).Where(x => !x.Contains($"{key}=")).ToList();
34+
data.Add($"{key}={value}");
35+
using (var sw = File.CreateText(Paths.IniFile))
36+
data.ForEach(x => sw.WriteLine(x));
37+
}
38+
else
39+
{
40+
using (var sw = File.CreateText(Paths.IniFile))
41+
sw.WriteLine($"{key}={value}");
42+
}
43+
}
44+
45+
public static void Delete(string key)
46+
{
47+
if (File.Exists(Paths.IniFile))
48+
{
49+
var data = File.ReadAllLines(Paths.IniFile).Where(x => !x.Contains($"{key}=")).ToList();
50+
using (var sw = File.CreateText(Paths.IniFile))
51+
data.ForEach(x => sw.WriteLine(x));
52+
}
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)