// RAMCleaner.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
//使用EmptyWorkingSet/SetProcessWorkingSetSize清理系统内存
//注:一切只是一个假象,将程序工作内存写入Windows的页面文件中反而会导致系统变慢
#define MB_UNIT
#if defined KB_UNIT
#define DIV (1024ULL)
#define WIDTH 10
#elif defined MB_UNIT
#define DIV (1024ULL * 1024ULL)
#define WIDTH 7
#elif defined GB_UNIT
#define DIV (1024ULL * 1024ULL * 1024ULL)
#define WIDTH 4
#else
#error One of KB_UNIT, MB_UNIT, GB_UNIT needs to be defined!
#endif
void PrintMemoryStatus()
{
MEMORYSTATUSEX statex;
statex.dwLength = sizeof(statex);
if (::GlobalMemoryStatusEx(&statex))
{
#if defined KB_UNIT
_tprintf(_T("There is %*ld percent of memory in use.\n"), WIDTH, statex.dwMemoryLoad);
_tprintf(_T("There are %*I64d total KB of physical memory.\n"), WIDTH, statex.ullTotalPhys / DIV);
_tprintf(_T("There are %*I64d free KB of physical memory.\n"), WIDTH, statex.ullAvailPhys / DIV);
_tprintf(_T("There are %*I64d total KB of paging file.\n"), WIDTH, statex.ullTotalPageFile / DIV);
_tprintf(_T("There are %*I64d free KB of paging file.\n"), WIDTH, statex.ullAvailPageFile / DIV);
_tprintf(_T("There are %*I64d total KB of virtual memory.\n"), WIDTH, statex.ullTotalVirtual / DIV);
_tprintf(_T("There are %*I64d free KB of virtual memory.\n"), WIDTH, statex.ullAvailVirtual / DIV);
//_tprintf(_T("There are %*I64d free KB of extended memory.\n"), WIDTH, statex.ullAvailExtendedVirtual / DIV);
#elif defined MB_UNIT
_tprintf(_T("There is %*ld percent of memory in use.\n"), WIDTH, statex.dwMemoryLoad);
_tprintf(_T("There are %*I64d total MB of physical memory.\n"), WIDTH, statex.ullTotalPhys / DIV);
_tprintf(_T("There are %*I64d free MB of physical memory.\n"), WIDTH, statex.ullAvailPhys / DIV);
_tprintf(_T("There are %*I64d total MB of paging file.\n"), WIDTH, statex.ullTotalPageFile / DIV);
_tprintf(_T("There are %*I64d free MB of paging file.\n"), WIDTH, statex.ullAvailPageFile / DIV);
_tprintf(_T("There are %*I64d total MB of virtual memory.\n"), WIDTH, statex.ullTotalVirtual / DIV);
_tprintf(_T("There are %*I64d free MB of virtual memory.\n"), WIDTH, statex.ullAvailVirtual / DIV);
//_tprintf(_T("There are %*I64d free MB of extended memory.\n"), WIDTH, statex.ullAvailExtendedVirtual / DIV);
#elif defined GB_UNIT
_tprintf(_T("There is %*ld percent of memory in use.\n"), WIDTH, statex.dwMemoryLoad);
_tprintf(_T("There are %*I64d total GB of physical memory.\n"), WIDTH, statex.ullTotalPhys / DIV);
_tprintf(_T("There are %*I64d free GB of physical memory.\n"), WIDTH, statex.ullAvailPhys / DIV);
_tprintf(_T("There are %*I64d total GB of paging file.\n"), WIDTH, statex.ullTotalPageFile / DIV);
_tprintf(_T("There are %*I64d free GB of paging file.\n"), WIDTH, statex.ullAvailPageFile / DIV);
_tprintf(_T("There are %*I64d total GB of virtual memory.\n"), WIDTH, statex.ullTotalVirtual / DIV);
_tprintf(_T("There are %*I64d free GB of virtual memory.\n"), WIDTH, statex.ullAvailVirtual / DIV);
//_tprintf(_T("There are %*I64d free GB of extended memory.\n"), WIDTH, statex.ullAvailExtendedVirtual / DIV);
#endif
_tprintf(_T("\n"));
}
else
{
_tprintf(_T("GlobalMemoryStatusEx Failed!\n\n"));
}
}
BOOL EnableDebugPrivilege()
{
BOOL bRet = FALSE;
HANDLE hToken;
if (::OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
{
LUID luid;
if (::LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &luid))
{
TOKEN_PRIVILEGES tp;
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (::AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL))
{
bRet = TRUE;
}
}
::CloseHandle(hToken);
}
return bRet;
}
BOOL EmptyAllProcess()
{
BOOL bRet = FALSE;
HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnap != INVALID_HANDLE_VALUE)
{
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
if (::Process32First(hProcessSnap, &pe32))
{
do
{
HANDLE hProcess = ::OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID);
if (hProcess)
{
::SetProcessWorkingSetSize(hProcess, (SIZE_T)-1, (SIZE_T)-1);
::CloseHandle(hProcess);
}
} while (::Process32Next(hProcessSnap, &pe32));
bRet = TRUE;
}
::CloseHandle(hProcessSnap);
}
return bRet;
}
int main()
{
HICON hIcon = ::LoadIcon(::GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1));
HWND hWnd = ::GetConsoleWindow();
::SendMessage(hWnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
::SendMessage(hWnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
_tprintf(_T("Before Cleaning:\n"));
PrintMemoryStatus();
LARGE_INTEGER t1, t2, tc;
::QueryPerformanceFrequency(&tc);
::QueryPerformanceCounter(&t1);
EnableDebugPrivilege();
BOOL bRet = EmptyAllProcess();
::QueryPerformanceCounter(&t2);
if (bRet)
{
_tprintf(_T("Clean Completed, in %lf seconds!\n\n"), (double)(t2.QuadPart - t1.QuadPart) / (double)tc.QuadPart);
_tprintf(_T("After Cleaning:\n"));
PrintMemoryStatus();
}
else
{
_tprintf(_T("Clean Failed!\n\n"));
}
system("pause");
return 0;
}
paschen
- 粉丝: 1743
- 资源: 9
最新资源
- Java项目-基于 Java+MySql+Swing图书管管理系统(视频+源码).zip
- 施工人员吊车推出车检测28-YOLO(v5至v9)、COCO、Darknet、VOC数据集合集.rar
- ART框架自动多步推理与工具利用提升大型语言模型能力
- 大规模API调用的自反思层级代理模型AnyTool研究与应用
- Agent-as-a-Judge: 使用智能体评估代码生成任务的有效性
- 强化大型语言模型作为智能体的能力研究:引入AgentTuning方法及其应用效果评估
- 断裂检测20-YOLO(v5至v9)、COCO、CreateML、Darknet、Paligemma、TFRecord、VOC数据集合集.rar
- AgentOhana统一数据和训练流水线提高自主代理学习效果
- 房屋租赁系统源码 SpringBoot + Vue 实现全功能解析
- 基于大型语言模型的自主智能体研究综述
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈