-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
158 lines (118 loc) · 4.81 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import gc
import os
import signal
import subprocess
import sys
import time
import platform
import psutil
def free_mem_win():
print("Current operating system is Windows")
confirm = input("Do you want to start memory free? (y/n): ")
if confirm != 'y':
return
# 程序本身占用的内存
own_process = psutil.Process(os.getpid())
own_mem_info = own_process.memory_info()
own_mem_used = own_mem_info.rss
# 释放前内存占用
pre_free_mem = psutil.virtual_memory()
pre_free_total = pre_free_mem.total
pre_free_used = pre_free_mem.used - own_mem_used
print(f"Before free: Used mem {pre_free_used / 1024 / 1024:.2f}MB / Total mem {pre_free_total / 1024 / 1024:.2f}MB")
processes = psutil.process_iter()
for proc in processes:
status = proc.status()
# 清理进程状态是 zombie 或 stopped
if status in ['zombie', 'stopped']:
print(f"{proc.name()} is currently {proc.status()}, mem usage {proc.memory_info().rss / 1024 / 1024:.2f}MB")
proc.kill()
# sleep 2秒,让系统稳定后再获取内存数据
time.sleep(2)
# 释放后内存占用
post_mem = psutil.virtual_memory()
post_total = post_mem.total
post_used = post_mem.used - own_mem_used
print(f"After free: Used mem {post_used / 1024 / 1024:.2f}MB / Total mem {post_total / 1024 / 1024:.2f}MB")
input("Memory Free completed. Press Enter key to exit...")
def free_mem_linux():
print("Current operating system is Linux")
confirm = input("Do you want to start memory free? (y/n): ")
if confirm != 'y':
return
# 程序本身占用的内存
own_mem = psutil.Process(os.getpid()).memory_info().rss
# 释放前内存占用
pre_free_mem = psutil.virtual_memory()
pre_free_total = pre_free_mem.total
pre_free_used = pre_free_mem.used - own_mem
print(f"Before free: Used mem {pre_free_used / 1024 / 1024:.2f}MB / Total mem {pre_free_total / 1024 / 1024:.2f}MB")
# 解析/proc文件系统
for pid in os.listdir('/proc'):
# 检查pid是否只包含数字
if not pid.isdigit():
continue
# 跳过pid小于等于100的进程
if int(pid) <= 100:
continue
try:
p = psutil.Process(int(pid))
status = p.status()
# 不清理进程状态是 running 或 sleeping 或 disk sleep
if status in ['running', 'sleeping', 'disk sleep']:
continue
print(f"{p.name()} is currently {p.status()}, mem usage {p.memory_info().rss / 1024 / 1024:.2f}MB")
os.kill(int(pid), signal.SIGKILL)
except IOError:
continue
# 执行垃圾回收
gc.collect()
# sleep 2秒,让系统稳定后再获取内存数据
time.sleep(2)
# 释放后内存占用
post_mem = psutil.virtual_memory()
post_total = post_mem.total
post_used = post_mem.used - own_mem
print(f"After free: Used mem {post_used / 1024 / 1024:.2f}MB / Total mem {post_total / 1024 / 1024:.2f}MB")
input("Memory Free completed. Press Enter key to exit...")
def free_mem_mac():
print("Current operating system is macOS")
confirm = input("Do you want to start memory free? (y/n): ")
if confirm != 'y':
return
# 程序本身占用的内存
own_process = psutil.Process()
own_mem_info = own_process.memory_info()
own_mem_used = own_mem_info.rss
# 释放前内存占用
pre_free_mem = psutil.virtual_memory()
pre_free_total = pre_free_mem.total
pre_free_used = pre_free_mem.used - own_mem_used
print(f"Before free: Used mem {pre_free_used / 1024 / 1024:.2f}MB / Total mem {pre_free_total / 1024 / 1024:.2f}MB")
try:
subprocess.run(["purge", "-c"], check=True)
except subprocess.CalledProcessError as e:
print("Error occurred while executing memory freeing command:", e)
return
# sleep 2秒,让系统稳定后再获取内存数据
time.sleep(2)
# 释放后内存占用
post_mem = psutil.virtual_memory()
post_total = post_mem.total
post_used = post_mem.used - own_mem_used
print(f"After free: Used mem {post_used / 1024 / 1024:.2f}MB / Total mem {post_total / 1024 / 1024:.2f}MB")
input("Memory Free completed. Press Enter key to exit...")
def get_platform():
if sys.platform not in platform.platform:
return sys.platform
return platform.platform[sys.platform]
if __name__ == '__main__':
platform = get_platform()
if platform == 'Windows':
free_mem_win()
elif platform == 'Linux':
free_mem_linux()
elif platform == 'OS X':
free_mem_mac()
else:
print('Unknown OS')