本帖最后由 yunruifuzhu 于 2021-5-8 11:02 编辑
由于windows 2008 服务器上,sqlserver经常出现死锁或卡死,经百度 说的 是要用微软的 rammap工具清理系统工作集。
尝试了一下确实立马恢复了,并且内存占用也少了 。
但需要人工去点击,后来经过网上各种搜索,将不同方式的清理工作集的源码合并到一切。效果基本与工具相同了。
[C++] 纯文本查看 复制代码
// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <windows.h>
#include <tlhelp32.h>
#include <psapi.h>
#pragma comment (lib,"psapi.lib")
#pragma comment (lib,"Advapi32.lib")
#define SE_INCREASE_QUOTA_NAME TEXT("SeIncreaseQuotaPrivilege")
bool EnableSpecificPrivilege(LPCTSTR lpPrivilegeName)
{
HANDLE hToken = NULL;
TOKEN_PRIVILEGES Token_Privilege;
BOOL bRet = TRUE;
do
{
if (0 == OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
{
//OutputDebugView(_T("OpenProcessToken Error"));
bRet = FALSE;
break;
}
if (0 == LookupPrivilegeValue(NULL, lpPrivilegeName, &Token_Privilege.Privileges[0].Luid))
{
//OutputDebugView(_T("LookupPrivilegeValue Error"));
bRet = FALSE;
break;
}
Token_Privilege.PrivilegeCount = 1;
Token_Privilege.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
//Token_Privilege.Privileges[0].Luid.LowPart=17;//SE_BACKUP_PRIVILEGE
//Token_Privilege.Privileges[0].Luid.HighPart=0;
if (0 == AdjustTokenPrivileges(hToken, FALSE, &Token_Privilege, sizeof(Token_Privilege), NULL, NULL))
{
//OutputDebugView(_T("AdjustTokenPrivileges Error"));
bRet = FALSE;
break;
}
} while (false);
if (NULL != hToken)
{
CloseHandle(hToken);
}
return bRet;
}
bool AdjustTokenPrivilegesForNT()
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
// Get a token for this process.
OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);
// Get the LUID for the EmptyWorkingSet privilege.
LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
// Get the EmptyWorkingSet privilege for this process.
return AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
}
BOOL EmptyAllSet()
{
HANDLE SnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (SnapShot == NULL)
{
return FALSE;
}
PROCESSENTRY32 ProcessInfo;//声明进程信息变量
ProcessInfo.dwSize = sizeof(ProcessInfo);//设置ProcessInfo的大小
//返回系统中第一个进程的信息
BOOL Status = Process32First(SnapShot, &ProcessInfo);
while (Status)
{
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, TRUE, ProcessInfo.th32ProcessID);
if (hProcess)
{
//printf("正在清理进程ID:%d 的内存\n",ProcessInfo.th32ProcessID);
SetProcessWorkingSetSize(hProcess, -1, -1);
//内存整理
EmptyWorkingSet(hProcess);
CloseHandle(hProcess);
}
//获取下一个进程的信息
Status = Process32Next(SnapShot, &ProcessInfo);
}
return TRUE;
}
void deepClean()
{
const int num = 1000;
int size = 500000;
int *a[num];
for (int i = 0; i < num; i++)
{
a[i] = new int[size];
}
for (int i = 0; i < num; i++)
{
delete a[i];
}
}
int main()
{
printf("AdjustTokenPrivilegesForNT - 提权:%d\n", AdjustTokenPrivilegesForNT());
printf("清理工作集:%d\n", EmptyAllSet());
printf("SE_INCREASE_QUOTA_NAME - 提权:%d\n", EnableSpecificPrivilege(SE_INCREASE_QUOTA_NAME));
printf("清理系统文件缓存:%d\n", SetSystemFileCacheSize(-1, -1, 0));
printf("手动2G内存生成释放:\n");
deepClean();
printf("清理成功,自动退出!");
//getchar();
}
运行需要管理员身份运行,不然提权会失败的哦!
成品:
ConsoleApplication1.rar
(126.98 KB, 下载次数: 329)
源码经过本人些许需改,代码很乱但很实用。需要的给个评分谢谢
|