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中@property和property函数常见使用方法示例

本文实例讲述了python中@property和property函数常见使用方法。分享给大家供大家参考,具体如下: 1、基本的@property使用,可以把函数当做属性用 class...

Python中shapefile转换geojson的示例

shapefile转换geojson import shapefile import codecs from json import dumps # read the shapefi...

python自定义类并使用的方法

本文实例讲述了python自定义类并使用的方法。分享给大家供大家参考。具体如下: class Person: def __init__(self, first, middle,...

python 实现selenium断言和验证的方法

最近在学习自动化测试,网上资料是挺多的,但是都是很基础的,想深入一点了解就没有资料了。于是开始自己研究。 这两天在看selenium验证和断言方面的资料。 断言就是判断是否跟预期结果一致...

Python格式化字符串f-string概览(小结)

简介 f-string,亦称为格式化字符串常量(formatted string literals),是Python3.6新引入的一种字符串格式化方法,该方法源于PEP 498 – Li...