Write in front
Security Butler Assistant or something like that always comes with a memory accelerator ball, which has the function of shutting down processes and cleaning up memory. Although in essence, memory cleaning is just a cover and has no actual effect, but after you perform deep cleaning, the visible memory decrease is indeed very decompressing. However, having to open the security software every time to enjoy the pleasure of decompression is really a waste of money, so I wanted to try writing it myself for fun.
Memory cleaning
Here I use python to write.
After searching, I determined two solutions, one is to clean up through the memory recycling mechanism that comes with Windows, and the other is to clean up through an API EmptyWorkingSet provided by Windows. Let’s compare it below.
Windows’ own memory recycling mechanism
Trigger Windows’ memory recycling mechanism by applying for a large amount of memory to clean up the memory.
import psutil import ctypes mem = psutil.virtual_memory() size = mem.total // 2 buffer = ctypes.create_string_buffer(size) del buffer
Here the total memory is obtained through psutil, and a buffer of half the total is created through the ctypes library, and then deleted immediately.
However, this will cause temporary lags and visible memory increases. And it was not cleaned cleanly. It would not be smooth at all to give up this plan decisively.
EmptyWorkingSet
EmptyWorkingSet is an API of Windows, which is used to clear the working set in the memory of the specified process. Define the physical memory used by the program as dirty pages and transfer them to the memory swap file on the disk. Clear the physical memory occupied by the process. When the process needs it again, it will be loaded from the external memory again. Then the loading process may cause temporary lag.
Most acceleration balls on the market should use this technology.
It is also very convenient to implement through the psutil library and win32-related libraries.
import win32api import win32con import win32process import psutil for pid in psutil.pids(): try: handle = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, False, pid) win32process.SetProcessWorkingSetSize(handle, -1, -1) win32api.EmptyWorkingSet(handle) exceptException: continue
After comparing with a certain housekeeper, it was found that the effect was not much different. They may have done some optimization of memory organization.
Process terminated
Killing processes is even simpler. In order to avoid problems here, I only keep their parent processes, which is still implemented through psutil.
import psutil pid_set = set() processes = [] # Only traverse running processes for pid in psutil.pids(): if pid in pid_set: continue try: proc = psutil.Process(pid) except psutil.NoSuchProcess: continue # Only process the parent process proc_p = proc.parent() if proc_p is not None: proc = proc_p if proc.pid in pid_set: continue pid_set.add(proc.pid) # Generate process information process_info = proc.as_dict(attrs=['pid', 'name', 'username', 'status', 'memory_info']) process.append(process_info)
Killing the process is achieved through the following code. The pid is the pid of the specified process.
psutil.Process(pid).kill()
At this point, the core functions are completed.
Complete development
Since it is an acceleration ball, it definitely needs an interface. Here I drew a picture in PS, and then wrote an interface using PyQt5 (I was really nauseous while writing it)
The last effect is as follows
Double-click to clean up memory
Right-click to open process management, and right-click on the process management interface to close it.
Only the top fifteen processes that occupy the most memory will be displayed each time, and the process will be killed by clicking on it.
At the same time, for the sake of fun, I also set up single scores and achievements. When you reach 100 points, you can unlock the title of Blue Screen Witness. (dog head)
Project address: github.com/SSRemex/Mem…
Write at the end
This project is just for fun, do not use it in daily life for fear of problems.
Once again, the memory cleaning function has no effect on the system, because Windows’ own mechanism does not require memory cleaning at all. On the contrary, memory cleaning may cause some lag problems (because the data needs to be re-read).
This article is reproduced from https://juejin.cn/post/7283719464897052733. If there is any infringement, please contact us for deletion.
———————————END——————- ——–
Digression
In this era of big data, how can you keep up with scripting without mastering a programming language? Python, the hottest programming language at the moment, has a bright future! If you also want to keep up with the times and improve yourself, please take a look.
Interested friends will receive a complete set of Python learning materials, including interview questions, resume information, etc. See below for details.
CSDN gift package:The most complete “Python learning materials” on the entire network are given away for free! (Safe link, click with confidence)
1. Python learning routes in all directions
The technical points in all directions of Python have been compiled to form a summary of knowledge points in various fields. Its usefulness is that you can find corresponding learning resources according to the following knowledge points to ensure that you learn more comprehensively.
2. Essential development tools for Python
The tools have been organized for you, and you can get started directly after installation!
3. Latest Python study notes
When I learn a certain basic and have my own understanding ability, I will read some books or handwritten notes compiled by my seniors. These notes record their understanding of some technical points in detail. These understandings are relatively unique and can be learned. to a different way of thinking.
4. Python video collection
Watch a comprehensive zero-based learning video. Watching videos is the fastest and most effective way to learn. It is easy to get started by following the teacher’s ideas in the video, from basic to in-depth.
5. Practical cases
What you learn on paper is ultimately shallow. You must learn to type along with the video and practice it in order to apply what you have learned into practice. At this time, you can learn from some practical cases.
6. Interview Guide
Resume template
CSDN gift package:The most complete “Python learning materials” on the entire network are given away for free! (Safe link, click with confidence)
If there is any infringement, please contact us for deletion.