久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

Windows內(nèi)存清理工具實現(xiàn)——從現(xiàn)象到本質(zhì)

 昵稱974066 2016-10-18

目前,,有很多清理內(nèi)存的工具,,如Wise Memory Optimizer、 MemoryZipperPlus,、SweepRAM等,,360安全衛(wèi)士、騰訊電腦管家,、魯大師等等系統(tǒng)工具也帶有清理內(nèi)存的功能,。

這些工具主要使用Windows提供的API:EmptyWorkingSet 或SetProcessWorkingSetSize 進(jìn)行內(nèi)存清理。

EmptyWorkingSet 強制將進(jìn)程工作集中的內(nèi)存盡可能多地移動到頁面文件中 (Removes as many pages as possible from the working set of the specified process.),,函數(shù)原型為:

BOOL WINAPI EmptyWorkingSet( _In_ HANDLE hProcess );

SetProcessWorkingSetSize可以設(shè)置進(jìn)程工作集中內(nèi)存的最大最小值(Sets the minimum and maximum working set sizes for the specified process.),,函數(shù)原型為:

BOOL WINAPI SetProcessWorkingSetSize( _In_ HANDLE hProcess, _In_ SIZE_T dwMinimumWorkingSetSize, _In_ SIZE_T dwMaximumWorkingSetSize );其中,如果后兩個參數(shù)均為-1時,,該函數(shù)效果與EmptyWorkingSet相同(The working set of the specified process can be emptied by specifying the value (SIZE_T)–1 for both the minimum and maximum working set sizes. This removes as many pages as possible from the working set. The EmptyWorkingSet function can also be used for this purpose.),。

使用上面介紹的API,我們就可以實現(xiàn)自己的內(nèi)存清理工具:

第一步,,提升程序進(jìn)程權(quán)限,,兩個API函數(shù)均需要傳入要清理的線程句柄,該句柄可以通過OpenProcess得到,,而如果進(jìn)程權(quán)限不夠,,將導(dǎo)致打開進(jìn)程失敗。

準(zhǔn)確地說,,不是提升權(quán)限,,而是把令牌中禁用的權(quán)限啟用。MSDN上指出,,如果需要打開其他進(jìn)程并獲得所有權(quán)限,,需要啟用SeDebugPrivilege權(quán)限(To open a handle to another process and obtain full access rights, you must enable the SeDebugPrivilege privilege)。

提升權(quán)限時,,需要用到的API有:OpenProcessToken,、LookupPrivilegeValue、AdjustTokenPrivileges,、CloseHandle,,MSDN上有詳細(xì)的用法講解(Enabling and Disabling Privileges in C++),在這不一一介紹了,,下面給出程序提權(quá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 = 1UL; 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; }第二步就是遍歷系統(tǒng)所有進(jìn)程,,調(diào)用上面兩個函數(shù)傳入的只是一個進(jìn)程的句柄,,也就是說每次調(diào)用只針對于一個進(jìn)程的內(nèi)存空間,所以我們還需要遍歷整個系統(tǒng)的所有進(jìn)程,,然后獲取每個進(jìn)程的句柄,,將該句柄當(dāng)做參數(shù)傳入上面兩個函數(shù)。遍歷系統(tǒng)所有進(jìn)程有很多方法,,這里使用ToolHelp API獲取,。其中用到3個API函數(shù):CreateToolhelp32Snapshot、Process32First,、Process32Next,,MSDN上對其用法已經(jīng)有詳細(xì)的介紹(Taking a Snapshot and Viewing Processes),這里也不再一一介紹,。

第三步自然就是在上面遍歷過程中調(diào)用EmptyWorkingSet 或 SetProcessWorkingSetSize 來實現(xiàn)清理內(nèi)存,。下面給出實現(xiàn)代碼,代碼中使用的是SetProcessWorkingSetSize函數(shù),,懶得包含EmptyWorkingSet需要的Psapi.h頭文件,。

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; }

第四步,為了更加直觀的顯示優(yōu)化后所騰出的內(nèi)存空間,,可以在清理前后獲取系統(tǒng)內(nèi)存使用狀態(tài),。這里用到GlobalMemoryStatusEx函數(shù)來獲取當(dāng)前系統(tǒng)物理內(nèi)存與虛擬內(nèi)存。GlobalMemoryStatus函數(shù)也可以獲取,,但在內(nèi)存超過4GB時得到的結(jié)果不正確(On computers with more than 4 GB of memory, the GlobalMemoryStatus function can return incorrect information, reporting a value of –1 to indicate an overflow. ),。程序中參照MSDN上的例子打印出內(nèi)存的使用狀況。

程序為了簡便,,使用了控制臺工程,,運行后截圖如下:

完整的程序及源代碼下載鏈接:

看了上面介紹,相信大部分人會認(rèn)為這是一個非常有效的方法,,能夠騰出大量內(nèi)存,,明顯減小內(nèi)存的占用率。殊不知,,這種伎倆其實只是一種假象,,所有的努力可能只會讓事情變得更加糟糕!不信繼續(xù)往下看,。

為什么這么說呢,,因為使用上面兩個API并不能減小程序的內(nèi)存,只不過是強制將正在運行的程序工作內(nèi)存寫入Windows的頁面文件,。這樣看似內(nèi)存使用量是下降了,,其實只不過是把內(nèi)存轉(zhuǎn)到了慢速存儲之中。當(dāng)程序再次需要使用到這些內(nèi)存時,,由于內(nèi)存已經(jīng)移入頁面文件,,虛擬內(nèi)存位置被標(biāo)記為了"不存在",,這時將會產(chǎn)生一個缺頁中斷,也就是平常說的頁面錯誤,,這時,,操作系統(tǒng)又不得不重新將這些內(nèi)存由硬盤移到內(nèi)存之中。

比如你正在看一個視頻,,現(xiàn)在先暫停它,接著使用內(nèi)存清理工具來“清理”內(nèi)存,。清理完后,,你再回過頭來繼續(xù)播放剛才的視頻或者執(zhí)行其他程序,你會發(fā)現(xiàn),,程序略有卡頓(對于小內(nèi)存機器尢為明顯),,在卡頓過后,系統(tǒng)的內(nèi)存又有所上升,,一段時間后,,內(nèi)存使用量也慢慢又漲上去了。如果你在剛清理后使用某個程序并在系統(tǒng)任務(wù)管理器里查看進(jìn)程信息(需要在“選擇列”對話框中選中“頁面錯誤增量”),,你會發(fā)現(xiàn)該程序的頁面錯誤增量會明顯地增加,,這個時候系統(tǒng)就正在把內(nèi)存重新搬回到工作集內(nèi)存之中。

有一篇寫得很好的文章,,詳細(xì)說明了這一問題,,文章鏈接:點擊打開鏈接,在此順便也貼出這篇文章:

Why Memory Optimizers and RAM Boosters Are Worse ThanUseless

Many companies want to sell you “memoryoptimizers,” often as part of “PC optimization” programs. These programs areworse than useless — not only will they not speed up your computer, they’llslow it down.

Such programs take advantage ofinexperienced users, making false promises about boosting performance. Inreality, your computer knows how to manage RAM on its own. It will use RAM toincrease your computer’s performance — there’s no point in having RAM sitempty.

Is Your Computer’s RAM Filling Up? That’s Good!

Memory optimizers are based on amisunderstanding. You may look at your computer’s RAM and see it filling up —for example, you may have 4 GB of RAM and see that 3 GB is full with only 1 GBto spare. That can be surprising to some people — look how bloated modernversions of Windows are! How are you ever going to run additional programs withso little memory available?

In reality, modern operating systems arepretty good at managing memory on their own. That 3 GB of used RAM doesn’tnecessarily indicate waste. Instead, your computer uses your RAM to cache datafor faster access. Whether it’s copies of web pages you had open in yourbrowser, applications you previously opened, or any other type of data youmight need again soon, your computer hangs onto it in its RAM. When you needthe data again, your computer doesn’t have to hit your hard drive — it can justload the files from RAM.

Crucially, there’s no point inhaving RAM empty. Even if your RAM is completely full and your computer needsmore of it to run an application, your computer can instantly discard thecached data from your RAM and use that space for the application. There’s nopoint in having RAM sit empty — if it’s empty, it’s being wasted. If it’s full,there’s a good chance it can help speed up program loading times and anythingelse that would use your computer’s hard drive.

Notice that very little RAM is actually“free” in the screenshot below. The RAM is being used as a cache, but it’sstill marked as available for any program that needs to use it.

In the past, full RAM did indicate aproblem. If you were running Windows Vista on a computer with half a gig ofRAM, you could feel the computer constantly slowing down — it had to constantlyread and write to the hard drive, using the hard drive’s page file as aninefficient replacement for RAM. However, modern computers generally haveenough RAM for most users. Even low-end computers generally ship with 4GB of RAM, which should be more than enough unless you’re doing intensive gaming,running multiple virtual machines, or editing videos.

Even if RAM was a problem for you,there’s no reason to use a memory optimizer. Memory optimizers are snake oilthat are useless at best and harmful at worst.

How Memory Optimizers Work

When you use a memory optimizer, you’llsee your computer’s RAM usage go down. This may seem like an easy win — you’vedecreased RAM usage just be pressing a button, after all. But it’s not thatsimple.

Memory optimizers actually work in oneof two ways:

· They call the EmptyWorkingSet Windows API function,forcing running applications to write their working memory to the Windows pagefile.

· They quickly allocate a large amount of memory tothemselves, forcing Windows to discard cached data and write application datato the page file. They then deallocate the memory, leaving it empty.

Both of these tricks will indeed free upRAM, making it empty. However, all this does is slow things down — now theapplications you use will have to get the data they need from the page file,reading from the hard drive and taking longer to work. Any memory being usedfor cache may be discarded, so Windows will have to get the data it needs fromthe hard drive.

In other words, these programs free upfast memory by forcing data you need onto slower memory, where it will have tobe moved back to fast memory again. This makes no sense! All it accomplishes isselling you another system optimization program you don’t need.

If Windows needs RAM, it will push datato the page file or discard cached data, anyway. This all happens automaticallywhen it needs to — there’s no point in slowing things down by forcing it to happenbefore it's necessary.

Like PC cleaning apps, memoryoptimizers are a scam. They appear to be doing something positive to people whodon’t understand how memory management works, yubut they’re actually doingsomething harmful.

How to Actually “Optimize” Your Memory

If you do want to have more availableRAM, skip the memory optimizer. Instead, try to get rid of running applicationsyou don’t need — purge unnecessary programs from your system tray, disableuseless startup programs, and so on.

If you do need more RAM for what you do,try buying some more RAM. RAM is pretty cheap and it’s not too hard to installit yourself using one of the RAM installing guides available online.Just ensure you buy the correct type of RAM for your computer.

文中詳細(xì)分析了這一現(xiàn)象的本質(zhì),。對于現(xiàn)在的操作系統(tǒng),,并不是你內(nèi)存空閑得越多,程序運行得就越快,。就算內(nèi)存真快耗盡,,操作系統(tǒng)也會自動丟棄掉一部分緩存數(shù)據(jù),同時將不頻繁訪問的頁面從工作集中移出,,暫時保存在內(nèi)存中的“轉(zhuǎn)換列表”中,,或者進(jìn)一步換出到頁面文件中,完全沒有理由也沒有必要我們自己在不必要的時候做這些事,。

所以,,上面實現(xiàn)的程序只是一個簡單地展示,以便大家明白清理內(nèi)存的實際原理,,不建議(甚至可以說不應(yīng)該)用于解決內(nèi)存不足的問題,。

現(xiàn)在,基本各種內(nèi)存清理工具使用的都是上面提到的方式,,有的還允許設(shè)置自動清理,,程序每隔一定時間自動進(jìn)行清理...... 當(dāng)你明白了這些工具的實現(xiàn)原理后,,你會發(fā)現(xiàn),這類工具僅僅是一個騙局而已,,它們只不過安慰一下那些不懂內(nèi)存管理的人,,不但沒什么貢獻(xiàn),反而會使你系統(tǒng)變得更慢,。

除了使用這種方式,,有些清理工具還進(jìn)行暴力清理。這類工具自身大量申請內(nèi)存,,快速填滿你的內(nèi)存,,這時你的系統(tǒng)會被迫丟棄大量緩存文件,同時調(diào)用轉(zhuǎn)換操作,,將其他進(jìn)程的內(nèi)存空間轉(zhuǎn)換到虛擬內(nèi)存,。之后,這類工具再突然釋放所申請的大量空間,,讓你覺得騰出了很大的空間,。使用這種暴力清理可能騰出比使用EmptyWorkingSet或SetProcessWorkingSetSize更多的內(nèi)存空間,但在所謂的“清理”過程中的開銷也更大,,最終同樣也只是讓事情變得更加糟糕,。

也有部分清理內(nèi)存工具,還會結(jié)束掉一些閑置的服務(wù)與一些進(jìn)程的殘留項以進(jìn)一步減小內(nèi)存使用量(如360安全衛(wèi)士),。這種方式確實有一定的效果,,能真正地騰出一定的空間。但由于可以結(jié)束的進(jìn)程是有限的,,而且這些進(jìn)程所占內(nèi)存往往不會太大,,通常也不能夠騰出多少內(nèi)存空間,優(yōu)化效果并不會明顯,。

綜上,,我個人認(rèn)為,無論清理內(nèi)存或者內(nèi)存優(yōu)化之類的工具沒什么實用價值與實際意義,。對于內(nèi)存小的機器,,它們只會更加拖慢你的系統(tǒng);對于大內(nèi)存的機器,,就算它們有用,,你也沒有理由去用。所以,,要解決內(nèi)存不夠的問題,,最有效的方式就是——插內(nèi)存條,插內(nèi)存條,,插內(nèi)存條,!

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點,。請注意甄別內(nèi)容中的聯(lián)系方式,、誘導(dǎo)購買等信息,謹(jǐn)防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多