pythonごみ回収、メモリ占有量判断、手動回収メモリ、二
13765 ワード
以下を例として、コンピュータメモリを判断し、プログラムメモリを解放する.
MemoryReleaser(600)をrelease_memory()コメントを削除すると、プログラムはずっと大きなメモリを消費します.
プログラムで使用した
if psutil.Process().memory_percent() > 0:
# coding=utf8
import time
import psutil, gc, commands,os
from logger_until import LoggerUntil
from until import keep_circulating
logger = LoggerUntil(name="Monitor").getlog(logfilename='Monitor.log', loglevel=2, add_StreamHandler=1)
need_monitor_procces_names = [ ## cmd ,
'touna0627.py',
'dailiip.py',
'redis-server',
'mongod',
]
class Monitor(object):
def __init__(self):
self.specified_process_list = self.get_specified_process()
self.current_process = self.get_current_process()
@staticmethod
def print_all_cmdlines():
for pid in psutil.pids():
p = psutil.Process(pid)
print p.cmdline()
@staticmethod
def get_specified_process():
all_pids = psutil.pids()
process_list = []
for pid in all_pids:
p = psutil.Process(pid)
p_cmdline = p.cmdline()
for argx in p_cmdline:
for name in need_monitor_procces_names:
if argx.find(name) > -1:
if p.status() != 'stopped':
process_list.append(p)
p_pid_set = set()
process_list2 = []
for p in process_list:
if p.pid not in p_pid_set:
process_list2.append(p)
p_pid_set.add(p.pid)
return process_list2
@staticmethod
def monitor_system():
psutil.cpu_percent()
time.sleep(1)
mem = psutil.virtual_memory()
mem_total = mem.total / 1000000
mem_available = mem.available / 1000000
mem_percent_str = str(mem.percent) + '%'
cpu_count = psutil.cpu_count()
cpu_percent_str = str(psutil.cpu_percent()) + '%'
msg = ' :{0}M , :{1}M, :{2}, cpu :{3}, cpu :{4}
'.format(mem_total, mem_available, mem_percent_str, cpu_count, cpu_percent_str)
logger.info(msg)
def monitor_specified_process(self):
for p in self.specified_process_list:
p.cpu_percent(None)
time.sleep(1)
for p in self.specified_process_list:
# p = psutil.Process(0)
""":type :psutil.Process"""
cmdline_str = ' '.join(p.cmdline()).ljust(60, ' ')
p_cpu_percent_str = str(round(p.cpu_percent(), 2)) + '%'
p_memory_percent_str = str(round(p.memory_percent(), 2)) + '%'
p_strated_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(p.create_time()))
p_pid_str = str(p.pid)
msg = ' ' + cmdline_str + ' pid :' + p_pid_str + ' cpu :' + p_cpu_percent_str + ' :' + p_memory_percent_str \
+ ' :' + p_strated_time
logger.info(msg)
@staticmethod
def get_current_process():
return psutil.Process()
def is_need_release(self,threshold):
print self.current_process.memory_percent()
if self.current_process.memory_percent() < threshold:
return 0
else:
logger.info(' %s id ' % self.current_process.pid)
return 1
def free_current_process_memory(self, threshold):
""" python """
if self.is_need_release(threshold) == 1:
gc.collect()
class MemoryReleaser():
def __init__(self,threshold,cmd_name_str='touna0627.py'):
"""
:type threshold:float
"""
self.threshold = threshold #
self.cmd_name_str =cmd_name_str
self.stutus, self.output = self.__get_memory_available()
def __get_memory_available(self):
# status, output = commands.getstatusoutput("free -m | grep Mem | awk '{print $4}'") ##shell
status, output = commands.getstatusoutput("ps aux | grep %s | sort -k4,4nr|head -1| awk '{print $4}'"%(self.cmd_name_str)) ##shell
return status, output
def release_memory(self):
if float(self.output) > self.threshold:
logger.info(' {}% , '.format(self.output))
gc.collect()
@keep_circulating(10)
def monitoring():
MemoryReleaser(40).release_memory() ###
monitor = Monitor()
monitor.monitor_specified_process()
monitor.monitor_system()
if __name__ == "__main__":
pass
a = list(range(10000000))
del a
time.sleep(30)
monitoring()
MemoryReleaser(600)をrelease_memory()コメントを削除すると、プログラムはずっと大きなメモリを消費します.
プログラムで使用した
free -m | grep Mem | awk '{print $7}'
。
psutil , psutil , 。
MemoryReleaser(600).release_memory() monitoring , 。
if psutil.Process().memory_percent() > 0:
gc.collect()
。
if true 。