Tea9源码网 - 免费分享精品源码、教程、软件|全网干货资源收集,免费下载。

开源Windows清理工作集清理内存

发布:资源收集工2021-6-4 8:41分类: 其它源码 标签: 垃圾清理 VC

由于windows 2008 服务器上,sqlserver经常出现死锁或卡死,经百度 说的 是要用微软的 rammap工具清理系统工作集。 尝试了一下确实立马恢复了,并且内存占用也少了 。但需要人工去点击,后来经过网上各种搜索,将不同方式的清理工作集的源码合并到一切。效果基本与工具相同了。

20210604084144.png

源代码:

// 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();
}
下载地址-成品 - 运行需要管理员身份运行,不然提权会失败的哦!
蓝奏网盘 百度网盘(提取码:tea9) 
温馨提示如有转载或引用以上内容之必要,敬请将本文链接作为出处标注,谢谢合作!

已有 29 人阅读