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设计】。

相关文章

利用一个简单的例子窥探CPython内核的运行机制

我最近花了一些时间在探索CPython,并且我想要在这里分享我的一些冒险经历。Allison Kaptur的excellent guide to getting started with...

解决Python requests库编码 socks5代理的问题

解决Python requests库编码 socks5代理的问题

编码问题 response = requests.get(URL, params=params, headers=headers, timeout=10) print '...

python实现图片压缩代码实例

前言 项目中大量用到图片加载,由于图片太大,加载速度很慢,因此需要对文件进行统一压缩 一:导入包 from PIL import Image import os 二:获取图片文件...

Python使用filetype精确判断文件类型

filetype.py Small and dependency free Python package to infer file type and MIME type checkin...

Eclipse中Python开发环境搭建简单教程

Eclipse中Python开发环境搭建简单教程

一、背景介绍   Eclipse是一款基于Java的可扩展开发平台。其官方下载中包括J2EE方向版本、Java方向版本、C/C++方向版本、移动应用方向版本等诸多版本。除此之外,Ecli...