经常看见昂少在那里用内存整理工具,感觉很有意思,然后就看了下那个工具,目测是通过释放所谓的多余的内存空间,来达到增加可用内存的容量,让电脑更加的流畅~

 

但是这样真的有用么,兔兔就去查了一些资料,包括某开源的内存整理的源码~最后才抓住了真相~

内存整理工具一般是调用了SetProcessWorkingSetSize()函数,这个函数是做什么的呢?直接查阅MSDN可以知道:

Using the SetProcessWorkingSetSize function to set an application\’s minimum and maximum working set sizes does not guarantee that the requested memory will be reserved, or that it will remain resident at all times. When the application is idle, or a low-memory situation causes a demand for memory, the operating system can reduce the application\’s working set. An application can use the VirtualLock function to lock ranges of the application\’s virtual address space in memory; however, that can potentially degrade the performance of the system.

When you increase the working set size of an application, you are taking away physical memory from the rest of the system. This can degrade the performance of other applications and the system as a whole. It can also lead to failures of operations that require physical memory to be present; for example, creating processes, threads, and kernel pool. Thus, you must use the SetProcessWorkingSetSize function carefully. You must always consider the performance of the whole system when you are designing an application.

一下子就暴露了嘿嘿~

SetProcessWorkingSetSize()函数的原型是:
BOOL SetProcessWorkingSetSize(
HANDLE hProcess,//hprocess进程句柄
SIZE_T dwMinimumWorkingSetSize,
SIZE_T dwMaximumWorkingSetSize
);

其实我们很轻松的就可以看出,该函数对于逻辑内存并没有做除改变,改变的仅仅是虚拟内存而已~SetProcessWorkingSetSize()的用法并不是所谓的”释放内存”,而是让程序在后台工作时尽量把本进程的物理内存占用控制在某一范围,以给别的进程让出物理内存.而且值得注意的是,还有所谓的自动清理内存的软件,让软件运行在后台,自动清理,其实就是用了一个time,强制把内存的程序转到虚拟内存之上~

这些所谓的内存整理软件也就是让咱过过眼瘾而已~Just so so

 

#include <stdio.h>
#include <windows.h>
#include <tlhelp32.h>

int main()
{
PROCESSENTRY32 pentry = {sizeof(pentry)};
HANDLE hPSnap =::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
BOOL bMore = ::Process32First(hPSnap,&pentry);

while(bMore)
{
if(strcmp(“xunyou.exe”,pentry.szExeFile) == NULL)
{

HANDLE hProcess = ::OpenProcess(PROCESS_SET_QUOTA,
FALSE,
pentry.th32ProcessID);

if(hProcess != NULL)
::SetProcessWorkingSetSize(hProcess, -1, -1);
else
break;
}
bMore = ::Process32Next(hPSnap,&pentry);
}

::CloseHandle(hPSnap);
return 0;
}

 

1 对 “内存整理工具的真相”的想法;

评论被关闭。