Skip to content

Instantly share code, notes, and snippets.

@benevides
Forked from AveYo/.FreeStandbyMemory.bat
Created September 30, 2021 13:34
Show Gist options
  • Select an option

  • Save benevides/d48898800c71113b75e0947cee2196b7 to your computer and use it in GitHub Desktop.

Select an option

Save benevides/d48898800c71113b75e0947cee2196b7 to your computer and use it in GitHub Desktop.
FreeStandbyMemory.bat - ninja edits at https://pastebin.com/Kj36ug5h
<# :: FreeStandbyMemory by AveYo, v6: fileless powershell task run, revised snippet, advanced schedule, builtin add_remove
@echo off
set/a CLEAR_EVERY_MINUTES=5
set/a CLEAR_WHEN_UNDER_MB=512
set/a FILESYSTEMCACHEALSO=1
:: check_admin_rights
title FreeStandbyMemory.bat
reg query "HKEY_USERS\S-1-5-20\Environment" /v TEMP >nul 2>nul || (
color 0c & echo. & echo PERMISSION DENIED! Right-click %~nx0 ^& Run as administrator
timeout /t 60 & color 0f & title %COMSPEC% & exit/b
)
:: add_remove whenever script is run again
schtasks /query /tn FreeStandbyMemory >nul 2>nul && (
echo.
schtasks /Delete /TN "FreeStandbyMemory" /f 2>nul
reg delete HKLM\Software\AveYo /v FreeStandbyMemory /f 2>nul
del /f /q "%Windir%\FreeStandbyMemory.bat" 2>nul
color 0c &echo. &echo REMOVED! Run script again to add fileless schedule!
timeout /t -1 &color 0f &title %COMSPEC% &exit/b
)
echo.
echo HINT: Can check if working by forcing a clear manually
echo - directly in PowerShell (Admin):
echo/ iex (Get-ItemProperty Registry::HKLM\SOFTWARE\AveYo).FreeStandbyMemory
echo - directly in Command Prompt (Admin):
echo/ powershell -noprofile -c "& {iex (Get-ItemProperty Registry::HKLM\SOFTWARE\AveYo).FreeStandbyMemory}"
echo - with self-elevating via any cmd / powershell / .bat / .lnk:
<nul set/p= powershell -noprofile -c "start powershell -ArgumentList '-noprofile -c &
echo/ {iex (Get-ItemProperty Registry::HKLM\SOFTWARE\AveYo).FreeStandbyMemory}' -verb RunAs -WindowStyle Hidden"
echo.
:: setup advanced schedule
set/a FREEMEM=1024*%CLEAR_WHEN_UNDER_MB%
set "c1=if( (gwmi win32_OperatingSystem).FreePhysicalMemory -lt %FREEMEM% ) {"
set "c2= iex (Get-ItemProperty Registry::HKLM\SOFTWARE\AveYo).FreeStandbyMemory }"
set "schedule=/Create /RU "System" /NP /RL HIGHEST /F /SD "01/01/2001" /ST "01:00:00" "
schtasks %schedule% /SC MINUTE /MO %CLEAR_EVERY_MINUTES% /TN "FreeStandbyMemory" /TR "powershell.exe -noprofile -c \"%c1%%c2%\""
set "sset=$s=New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -Priority 4 -StartWhenAvailable;"
set "stopexisting=$s.CimInstanceProperties['MultipleInstances'].Value=3;"
powershell -noprofile -c "%sset% %stopexisting% $null=Set-ScheduledTask -TaskName FreeStandbyMemory -Settings $s"
:: export $Snippet to registry for fileless powershell task run
if %FILESYSTEMCACHEALSO% GTR 0 (set "INCLUDE_FILESYSTEMCACHE=$true") else set "INCLUDE_FILESYSTEMCACHE=$false"
set "r1=$txt=(([io.file]::ReadAllText(\"%~f0\") -split '/\*_\*/')[1]).replace(\"`r`n\",'');"
set "r2=$Snippet='$Snippet='''+ $txt +'''; Add-Type -TypeDefinition $Snippet -Language CSharp;"
set "r3=[FreeStandbyMemory.PInvoke]::ClearStandbyCache(%INCLUDE_FILESYSTEMCACHE%);'"
set "reg_add=$key='HKLM:\Software\AveYo'; $null=New-Item -Path $key -Force; New-ItemProperty"
powershell -noprofile -c "%r1% %r2% %r3%; %reg_add% -Force -Path $key -Name 'FreeStandbyMemory' -Value $Snippet;"
:: trigger task, force a manual clear and finish setup
schtasks /Run /TN "FreeStandbyMemory"
powershell -noprofile -c "& {iex (Get-ItemProperty Registry::HKLM\SOFTWARE\AveYo).FreeStandbyMemory}"
echo Clearing StandbyMemory every %CLEAR_EVERY_MINUTES% minutes ONLY if available memory goes under %CLEAR_WHEN_UNDER_MB% MB
echo ADDED! Run "%~nx0" again to remove fileless schedule!
timeout /t -1
exit /b
# Based on "PowerShell wrapper script for clear StandBy memory without RAMMap" by Alexander Korotkov
# Implemented SetSystemFileCacheSize and NtSetSystemInformation suggestions by Maks.K
# Using RtlAdjustPrivilege, stripped output, sanitized by AveYo
#>
$Snippet = @"
/*_*/
using System;
using System.Runtime.InteropServices;
namespace FreeStandbyMemory
{
public class PInvoke
{
const uint SE_INCREASE_QUOTA_PRIVILEGE = 0x00000005;
const uint SE_PROF_SINGLE_PROCESS_PRIVILEGE = 0x0000000d;
const int SystemFileCacheInformation = 0x0015;
const int SystemMemoryListInformation = 0x0050;
static int MemoryPurgeStandbyList = 0x0004;
static bool retv = false;
[DllImport("ntdll.dll")]
static extern uint RtlAdjustPrivilege(ulong Privilege, bool Enable, bool CurrentThread, ref bool RetValue);
[DllImport("ntdll.dll")]
static extern uint NtSetSystemInformation(int InfoClass, ref int Info, int Length);
[DllImport("kernel32")]
static extern bool SetSystemFileCacheSize(IntPtr MinimumFileCacheSize, IntPtr MaximumFileCacheSize, int Flags);
public static void ClearStandbyCache(bool ClearFileSystemCache)
{
try
{
RtlAdjustPrivilege(SE_INCREASE_QUOTA_PRIVILEGE, true, false, ref retv);
RtlAdjustPrivilege(SE_PROF_SINGLE_PROCESS_PRIVILEGE, true, false, ref retv);
if (ClearFileSystemCache) SetSystemFileCacheSize(IntPtr.Subtract(IntPtr.Zero, 1), IntPtr.Subtract(IntPtr.Zero, 1), 0);
NtSetSystemInformation(SystemMemoryListInformation, ref MemoryPurgeStandbyList, Marshal.SizeOf(MemoryPurgeStandbyList));
}
catch (Exception)
{
}
}
}
}
/*_*/
"@
Add-Type -TypeDefinition $Snippet -Language CSharp;
[FreeStandbyMemory.PInvoke]::ClearStandbyCache($true);
# 2018.10.02
* text=auto
* eol=crlf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment