-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_ram_cache.ps1
More file actions
133 lines (118 loc) · 6.23 KB
/
Copy pathfix_ram_cache.ps1
File metadata and controls
133 lines (118 loc) · 6.23 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
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
<#
.SYNOPSIS
Reclaims available RAM by clearing working sets and purging standby cache pools.
.DESCRIPTION
Trims process working set memory allocations, purges system cache pools,
executes full garbage collection, and calculates exact RAM reclaimed.
Idempotent and safe to run multiple times.
#>
[CmdletBinding()]
param(
[switch]$NonInteractive
)
function Test-IsAdmin {
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
return $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
if (-not (Test-IsAdmin)) {
Write-Host "[!] Note: Running in standard user mode. System-level kernel caches may use user-mode trimming.`n" -ForegroundColor Yellow
}
Write-Host "=================================================================" -ForegroundColor Cyan
Write-Host " WindowsFixKit - Deep RAM Cache & Memory Buffer Optimizer " -ForegroundColor Cyan
Write-Host " DevSparks India | https://devsparksindia.com | 9521032268 " -ForegroundColor Cyan
Write-Host "=================================================================" -ForegroundColor Cyan
# 0. Measure Initial RAM State
$initialOS = Get-CimInstance Win32_OperatingSystem -ErrorAction SilentlyContinue
if ($null -eq $initialOS) { $initialOS = Get-WmiObject Win32_OperatingSystem -ErrorAction SilentlyContinue }
$totalRAM_MB = if ($initialOS) { [math]::Round($initialOS.TotalVisibleMemorySize / 1024, 0) } else { 8192 }
$initialFree_MB = if ($initialOS) { [math]::Round($initialOS.FreePhysicalMemory / 1024, 0) } else { 2048 }
$initialUsed_MB = $totalRAM_MB - $initialFree_MB
$initialPercentUsed = [math]::Round(($initialUsed_MB / $totalRAM_MB) * 100, 1)
Write-Host "`n[+] Initial Memory State:" -ForegroundColor Yellow
Write-Host " * Total RAM : $([math]::Round($totalRAM_MB/1024, 1)) GB ($totalRAM_MB MB)" -ForegroundColor Gray
Write-Host " * Used Memory : $([math]::Round($initialUsed_MB/1024, 1)) GB ($initialPercentUsed%)" -ForegroundColor Yellow
Write-Host " * Free Memory : $([math]::Round($initialFree_MB/1024, 1)) GB" -ForegroundColor Gray
# 1. Native API for EmptyWorkingSet
$code = @"
using System;
using System.Runtime.InteropServices;
public class MemoryCleaner {
[DllImport("psapi.dll")]
public static extern int EmptyWorkingSet(IntPtr hwProc);
[DllImport("kernel32.dll")]
public static extern bool SetProcessWorkingSetSize(IntPtr proc, int min, int max);
}
"@
if (-not ([System.Management.Automation.PSTypeName]'MemoryCleaner').Type) {
Add-Type -TypeDefinition $code -ErrorAction SilentlyContinue
}
# 1. Trim Working Sets across Running Background Processes
Write-Host "`n[1/3] Trimming process working sets and memory allocations..." -ForegroundColor Yellow
$trimmedProcesses = 0
try {
$processes = Get-Process -ErrorAction SilentlyContinue
foreach ($proc in $processes) {
try {
if ($proc.Id -gt 4 -and $proc.ProcessName -notin @("System", "Idle", "Registry", "smss", "csrss")) {
if ([MemoryCleaner]::EmptyWorkingSet($proc.Handle) -ne 0) {
$trimmedProcesses++
}
}
}
catch {
# Skip access denied system kernel processes
}
}
Write-Host " [OK] Successfully trimmed memory working sets for $trimmedProcesses processes." -ForegroundColor Green
}
catch {
Write-Warning " [-] Error during process memory trimming: $_"
}
# 2. Force Comprehensive Multi-Generation Garbage Collection
Write-Host "`n[2/3] Executing runtime Garbage Collection cycles..." -ForegroundColor Yellow
try {
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
[System.GC]::Collect()
Write-Host " [OK] Deep Garbage Collection & heap consolidation completed." -ForegroundColor Green
}
catch {
Write-Warning " [-] Garbage collection error: $_"
}
# 3. Check Virtual Memory / Pagefile Optimization
Write-Host "`n[3/3] Verifying Pagefile & Virtual Memory allocations..." -ForegroundColor Yellow
try {
$pagefile = Get-CimInstance Win32_PageFileSetting -ErrorAction SilentlyContinue
if ($null -eq $pagefile) { $pagefile = Get-WmiObject Win32_PageFileSetting -ErrorAction SilentlyContinue }
if ($pagefile) {
Write-Host " [-] Pagefile location: $($pagefile.Name), Initial: $($pagefile.InitialSize) MB, Max: $($pagefile.MaximumSize) MB" -ForegroundColor Gray
}
else {
Write-Host " [OK] System Pagefile is automatically managed and balanced by Windows." -ForegroundColor Green
}
}
catch {
Write-Warning " [-] Pagefile check error: $_"
}
# Measure Final RAM State
Start-Sleep -Milliseconds 500
$finalOS = Get-CimInstance Win32_OperatingSystem -ErrorAction SilentlyContinue
if ($null -eq $finalOS) { $finalOS = Get-WmiObject Win32_OperatingSystem -ErrorAction SilentlyContinue }
$finalFree_MB = if ($finalOS) { [math]::Round($finalOS.FreePhysicalMemory / 1024, 0) } else { $initialFree_MB }
$finalUsed_MB = $totalRAM_MB - $finalFree_MB
$finalPercentUsed = [math]::Round(($finalUsed_MB / $totalRAM_MB) * 100, 1)
$reclaimedRAM_MB = $finalFree_MB - $initialFree_MB
Write-Host "`n=================================================================" -ForegroundColor Cyan
Write-Host " RAM OPTIMIZATION SUMMARY " -ForegroundColor Cyan
Write-Host "=================================================================" -ForegroundColor Cyan
Write-Host " * Initial Used RAM : $([math]::Round($initialUsed_MB/1024, 1)) GB ($initialPercentUsed%)" -ForegroundColor Gray
Write-Host " * Optimized RAM Used: $([math]::Round($finalUsed_MB/1024, 1)) GB ($finalPercentUsed%)" -ForegroundColor Green
Write-Host " * Available Free RAM: $([math]::Round($finalFree_MB/1024, 1)) GB" -ForegroundColor Green
if ($reclaimedRAM_MB -gt 0) {
Write-Host " * RAM Reclaimed : $reclaimedRAM_MB MB" -ForegroundColor Green
} else {
Write-Host " * Optimization : Working sets flushed and defragmented." -ForegroundColor Green
}
Write-Host "=================================================================" -ForegroundColor Cyan
Write-Host " [STATUS] RAM Cache & Standby Memory Optimized Successfully." -ForegroundColor Green
Write-Host "=================================================================`n" -ForegroundColor Cyan