Python小工具之消耗系统指定大小内存的方法

yipeiwu_com6年前Python基础

工作中需要根据某个应用程序具体吃了多少内存来决定执行某些操作,所以需要写个小工具来模拟应用程序使用内存情况,下面是我写的一个Python脚本的实现。

#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import re
import time

def print_help():
  print 'Usage: '
  print ' python mem.py 100MB'
  print ' python mem.py 1GB'

if __name__ == "__main__":
  if len(sys.argv) == 2:
    pattern = re.compile('^(\d*)([M|G]B)$')
    match = pattern.match(sys.argv[1].upper())
    if match:
      num = int(match.group(1))
      unit = match.group(2)
      if unit == 'MB':
        s = ' ' * (num * 1024 * 1024)
      else:
        s = ' ' * (num * 1024 * 1024 * 1024)

      time.sleep(10000)
    else:
      print_help()
  else:
    print_help()

使用方法如下:

python mem.py 100M
python mem.py 1G

以上这篇Python小工具之消耗系统指定大小内存的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python嵌套函数使用外部函数变量的方法(Python2和Python3)

python嵌套函数使用外部函数变量的方法,Python2和Python3均可使用 python3 def b(): b = 1 def bchange(): nonlo...

python清除字符串中间空格的实例讲解

1、使用字符串函数replace >>> a = 'hello world' >>> a.replace(' ', '') 'helloworld...

python简单实现基于SSL的IRC bot实例

本文实例讲述了python简单实现基于SSL的 IRC bot。分享给大家供大家参考。具体如下: #!/usr/bin/python # -*- coding: utf8 -*- i...

python处理按钮消息的实例详解

python处理按钮消息的实例详解

python处理按钮消息的实例详解            最新学习Python的基础知...

django连接mysql数据库及建表操作实例详解

django连接mysql数据库及建表操作实例详解

本文实例讲述了django连接mysql数据库及建表操作。分享给大家供大家参考,具体如下: django连接mysql数据库需要在project同名的目录下面的__init__.py里面...