利用python实现RAMMap自动释放内存

RAMMap这款软件释放运行内存的效果特别好,不过每次使用必须挨个选项点,太麻烦。

最近发现可以用cmd调用其外部接口,所以灵机一动用python写个程序实现自动清理

创建两个文件,一个主程序用于控制,另一个是自动清理内存的文件

RAMMapRUN.py  (主程序)

import ctypes
import sys
import wx
from wx import adv

# 用于创建托盘图标
import pystray
from PIL import Image
from pystray import MenuItem

# 用于调用cmd
import subprocess


automatic = False
empty_path = 6


'''
def RAMMap_run():
	cmd = 'RAMMap-64bit -Ew'
	res = subprocess.Popen(cmd)
	output_str = res.read()

	print(output_str)
'''


# 启动和关闭自动释放内存的文件
def Automatic():
	global automatic, i
	if automatic == True:
		automatic = False
		subprocess.Popen('taskkill /F /IM _Ea_.exe',shell=True) #强行终止进程
		#托盘气泡提示
		adv.NotificationMessage("RAMMap-64bit", message="已结束释放内存\n Automatic turn off").Show()
	elif automatic == False:
		automatic = True
		subprocess.Popen("start _Ea_.exe",shell=True)#启动(也可以用.py)
		# adv.NotificationMessage("").SetIcon(wx.Icon("ASNO.png")).Show()
		#托盘气泡提示
		adv.NotificationMessage("RAMMap-64bit", message="正在自动释放内存\n Automatic have already turn on").Show()


def Empty(path):
	# cmd调用RAMMap外部接口
	if path == 0:
		# 这里一定要阻塞运行,否则RAMMAP会报错
		subprocess.Popen("RAMMap-64bit -Ew",shell=True).wait()
		subprocess.Popen("RAMMap-64bit -Es",shell=True).wait()
		subprocess.Popen("RAMMap-64bit -Em",shell=True).wait()
		subprocess.Popen("RAMMap-64bit -Et",shell=True).wait()
		subprocess.Popen("RAMMap-64bit -E0",shell=True).wait()
	elif path == 1:
		subprocess.Popen("RAMMap-64bit -Ew",shell=True)
	elif path == 2:
		subprocess.Popen("RAMMap-64bit -Es",shell=True)
	elif path == 3:
		subprocess.Popen("RAMMap-64bit -Em",shell=True)
	elif path == 4:
		subprocess.Popen("RAMMap-64bit -Et",shell=True)
	elif path == 5:
		subprocess.Popen("RAMMap-64bit -E0",shell=True)


# 判断点击的选项
def click_menu(icon, item):
	global empty_path

	print("点击了", item)
	if str(item) == 'Empty automatic':
		Automatic()
	elif str(item) == 'Empty all':
		empty_path = 0
	elif str(item) == 'Empty Ew':
		empty_path = 1
	elif str(item) == 'Empty Es':
		empty_path = 2
	elif str(item) == 'Empty Em':
		empty_path = 3
	elif str(item) == 'Empty Et':
		empty_path = 4
	elif str(item) == 'Empty E0':
		empty_path = 5

	Empty(empty_path)
	empty_path = 6


# 关闭程序
def on_exit(icon, item):
	icon.stop()


def is_admin():
	try:
		return ctypes.windll.shell32.IsUserAnAdmin()
	except:
		return False


app = wx.App(0)
# flag = adv.NotificationMessage.SetFlags("ASNO.png")

# 以管理员运行
if is_admin():
	# 创建托盘
	menu = (MenuItem(text='Empty all', action=click_menu),
	MenuItem(text='Empty Ew', action=click_menu),
	MenuItem(text='Empty Es', action=click_menu),
	MenuItem(text='Empty Em', action=click_menu),
	MenuItem(text='Empty Et', action=click_menu),
	MenuItem(text='Empty E0', action=click_menu),
	MenuItem(text='Empty automatic', action=click_menu),
	MenuItem(text='退出', action=on_exit),
	)
	image = Image.open("ASNO.png")# 托盘显示的图标
	icon = pystray.Icon("name", image, "RAMMap-64bit\nRUN", menu)# 鼠标放在托盘上显示的文字
	icon.run()
else:
	ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)

_Ea_.py

import subprocess
import time


# 自动清理,1分钟一次
while True:
	# cmd阻塞运行,调用RAMMap
	subprocess.Popen("RAMMap-64bit -Ew", shell=True).wait()
	subprocess.Popen("RAMMap-64bit -Es", shell=True).wait()
	subprocess.Popen("RAMMap-64bit -Em", shell=True).wait()
	subprocess.Popen("RAMMap-64bit -Et", shell=True).wait()
	subprocess.Popen("RAMMap-64bit -E0", shell=True).wait()
	time.sleep(60)

然后分别打包,和rammap放在同一目录下(别忘了程序的图标),运行RAMMapRUN

# 调用RAMMap要根据自己rammap软件的名称  如果叫RAMMap,就用"RAMMap -Ew"

我的叫RAMMap-64bit,所以程序中写的都是"RAMMap-64bit -Ew", 所以注意修改

 

运行效果 

 https://download.csdn.net/download/ALIENWKRE/87453637 程序下载(已打包)

程序源码下载RAMMap自动清理程序(含python源代码)-Python文档类资源-CSDN文库

Rammap 是一款由微软开发的系统工具软件,用于帮助用户监测和管理计算机的内存使用情况。它提供了一个直观的界面,可以显示各种内存信息,以便用户更好地了解计算机内存的使用情况并进行优化。 Rammap 可以自动运行的原因有以下几点: 首先,作为一个系统工具软件,Rammap 的运行对于计算机的性能优化和稳定性非常重要。通过自动运行,Rammap 可以在计算机启动时立即监测内存使用,及时识别和解决潜在的内存问题。这样可以有效减少计算机运行时的错误和崩溃,并且提高计算机的整体性能。 其次,Rammap自动运行还可以帮助用户及时了解计算机在运行过程中的内存状况。它可以自动在系统托盘中显示内存使用情况的图标,用户只需将鼠标悬停在图标上就可以查看详细的内存信息。这样用户可以随时关注内存使用的变化,及时采取措施,例如关闭一些占用内存较多的程序,释放内存空间,从而确保计算机的稳定性和性能。 最后,Rammap自动运行还可以帮助用户节省时间和精力。用户不需要每次手动打开 Rammap 进行内存监测,而是只需要设置一次,Rammap 就会在计算机启动时自动开始监测。这样可以提高用户的工作效率,让用户更加专注于具体的任务和工作内容。 综上所述,Rammap 自动运行具有非常重要的实际意义,它可以帮助用户更好地了解计算机内存的使用情况,及时解决内存问题,提高计算机的性能和稳定性,并且节省用户的时间和精力。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值