The following is an example of determining computer memory and releasing program memory.
#Coding=utf8Import TimeImportPsutil, GC, commands fromLogger_untilImportLoggeruntil fromUntilImportKeep_circulatinglogger= Loggeruntil (name="Monitor"). Getlog (logfilename='Monitor.log', loglevel=2, Add_streamhandler=1) Need_monitor_procces_names= [##需要检测的进程的cmd命令放到这里, support for fuzzy matching 'touna0627.py', 'dailiip.py', 'Redis-server', 'Mongod',]classMonitor (object):def __init__(self): self.specified_process_list=self.get_specified_process () self.current_process=self.get_current_process () @staticmethoddefprint_all_cmdlines (): forPidinchpsutil.pids (): P=Psutil. Process (PID)Printp.cmdline () @staticmethoddefget_specified_process (): All_pids=psutil.pids () process_list= [] forPidinchall_pids:p=Psutil. Process (PID) P_cmdline=P.cmdline () forArgxinchP_cmdline: forNameinchNeed_monitor_procces_names:ifArgx.find (name) >-1: ifP.status ()! ='stopped': Process_list.append (p) P_pid_set=set () Process_list2= [] forPinchprocess_list:ifP.pid not inchp_pid_set:process_list2.append (P) p_pid_set.add (p.pid)returnProcess_list2 @staticmethoddefMonitor_system (): Psutil.cpu_percent () time.sleep (1) Mem=psutil.virtual_memory () mem_total= mem.total/1000000mem_available= mem.available/1000000Mem_percent_str= str (mem.percent) +'%'Cpu_count=psutil.cpu_count () cpu_percent_str= str (psutil.cpu_percent ()) +'%'msg='The total native memory is: {0}m, native available memory is: {1}m, Native memory usage is: {2}, native CPU cores are: {3}, native CPU usage is: {4}\n\n'. Format (mem_total, mem_available, Mem_percent_str, Cpu_count, cpu_percent_str) logger.info (msg)defmonitor_specified_process (self): forPinchself.specified_process_list:p.cpu_percent (None) time.sleep (1) forPinchself.specified_process_list:#p = psutil. Process (0) """: Type:p Sutil. 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='Process'+ Cmdline_str +'The PID is:'+ P_pid_str +'The CPU utilization is:'+ P_cpu_percent_str +'Memory Utilization is:'+P_memory_percent_str+'the start time of the process is:'+p_strated_time logger.info (msg) @staticmethoddefget_current_process ():returnPsutil. Process ()defis_need_release (self,threshold):Printself.current_process.memory_percent ()ifSelf.current_process.memory_percent () <Threshold:return0Else: Logger.info ('reclaim memory for the current%s process ID'%self.current_process.pid)return1deffree_current_process_memory (self, Threshold):"""reclaims the memory of the current process where Python is located""" ifSelf.is_need_release (threshold) = = 1: Gc.collect ()classMemoryreleaser ():def __init__(self,threshold): Self.threshold=threshold Self.stutus, Self.output= self.__get_memory_available() @staticmethoddef __get_memory_available(): status, Output= Commands.getstatusoutput ("free-m | grep Mem | awk ' {print $7} '") # #shell命令查询计算机可用内存returnstatus, Outputdefrelease_memory (self):ifFloat (self.output) <Self.threshold:logger.info ('Computer free memory is%SM, program needs to free memory'%self.output) Gc.collect () @keep_circulating (10)defmonitoring (): Memoryreleaser (600). Release_memory () # # #这一行来释放内存 Monitor=Monitor () monitor.monitor_specified_process () Monitor.monitor_system ( )if __name__=="__main__": Passa= List (range (10000000)) dela monitoring ()
If Memoryreleaser () Release_memory () is commented out, the program will always occupy large memory.
The program uses the
free-m | grep Mem | awk ' {print $7} '
To determine the available memory for your computer.
Although Psutil can determine the memory, but use Psutil to determine the memory, memory can not be recycled.
Put Memoryreleaser () release_memory () into the last line in the monitoring function, and you won't be able to reclaim the memory.
Tried to use it many times.
If Psutil. Process (). Memory_percent () > 0:
Gc.collect ()
This method does not reclaim memory.
Write if true to reclaim memory.
Python garbage collection, determine memory consumption, manually reclaim memory, two