Files
SpdUp/Services/RamOptimizationService.cs
T
Phil-icyou 19ba425e79 Initial commit: SpdUp Windows Gaming Booster v1.0.0
- .NET 8 WPF app with full MVVM architecture
- One-click boost: CPU, RAM, GPU, network, services, timer resolution
- Real-time system monitor (LibreHardwareMonitor)
- Auto game detection (30+ games)
- RAM optimizer with kernel-level cleanup
- Network optimizer (TCP registry tweaks)
- Service manager, process manager, shader cache cleaner
- Desktop widget overlay
- Debug log viewer
- Custom dark gaming theme with trans pride colors
- 84 unit tests (xUnit)
- Inno Setup installer script
- Landing page website
2026-03-08 17:34:45 +01:00

112 lines
3.4 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Diagnostics;
using System.Runtime.InteropServices;
using SpdUp.Core;
namespace SpdUp.Services;
/// <summary>
/// Performs real memory optimizations: working-set trimming + standby-list purge.
/// Requires administrator privileges.
/// </summary>
public sealed class RamOptimizationService
{
/// <summary>
/// Trim working sets of all accessible processes pushes unused pages to the standby list.
/// </summary>
public RamCleanupResult TrimWorkingSets()
{
int trimmed = 0, failed = 0;
var ownPid = Environment.ProcessId;
foreach (var proc in Process.GetProcesses())
{
try
{
if (proc.Id == ownPid || proc.Id == 0 || proc.Id == 4) continue;
var hProcess = NativeMethods.OpenProcess(
NativeMethods.PROCESS_SET_INFORMATION | NativeMethods.PROCESS_QUERY_INFORMATION,
false, proc.Id);
if (hProcess == IntPtr.Zero) { failed++; continue; }
try
{
if (NativeMethods.EmptyWorkingSet(hProcess))
trimmed++;
else
failed++;
}
finally
{
NativeMethods.CloseHandle(hProcess);
}
}
catch
{
failed++;
}
finally
{
proc.Dispose();
}
}
Logger.Info($"Working-set trim complete: {trimmed} trimmed, {failed} skipped.");
return new RamCleanupResult(trimmed, failed, 0);
}
/// <summary>
/// Purge the standby memory list. Requires SeProfileSingleProcessPrivilege.
/// </summary>
public bool PurgeStandbyList()
{
try
{
NativeMethods.EnablePrivilege(NativeMethods.SE_PROF_SINGLE_PROCESS_NAME);
int command = (int)NativeMethods.SYSTEM_MEMORY_LIST_COMMAND.MemoryPurgeStandbyList;
int status = NativeMethods.NtSetSystemInformation(
NativeMethods.SystemMemoryListInformation,
ref command,
Marshal.SizeOf<int>());
if (status == 0)
{
Logger.Info("Standby list purged successfully.");
return true;
}
Logger.Warn($"NtSetSystemInformation returned NTSTATUS 0x{status:X8}");
return false;
}
catch (Exception ex)
{
Logger.Error("Failed to purge standby list", ex);
return false;
}
}
/// <summary>
/// Full RAM optimisation: trim working sets + purge standby.
/// </summary>
public RamCleanupResult FullCleanup()
{
var result = TrimWorkingSets();
var purged = PurgeStandbyList();
return result with { StandbyPurged = purged ? 1 : 0 };
}
/// <summary>
/// Returns current memory statistics.
/// </summary>
public static (ulong totalMB, ulong availMB, uint loadPercent) GetMemoryStatus()
{
var mem = new NativeMethods.MEMORYSTATUSEX { dwLength = (uint)Marshal.SizeOf<NativeMethods.MEMORYSTATUSEX>() };
NativeMethods.GlobalMemoryStatusEx(ref mem);
return (mem.ullTotalPhys / 1048576, mem.ullAvailPhys / 1048576, mem.dwMemoryLoad);
}
}
public record struct RamCleanupResult(int TrimmedCount, int FailedCount, int StandbyPurged);