python占用内存脚本(极简)

 

import sys


if len(sys.argv) <= 1 or {'-h', '--help'}.intersection(sys.argv):
    exit('usage: python3 occupy_mem.py memory_amount(unit: Byte)')

# Gradually occupy memory in 1GB increments to prevent untimely interruption when the consumption becomes too large .
mem_size = int(sys.argv[1])
unit = 1024 ** 3
mem_pool = [bytearray(unit)] * int(mem_size // unit) + [bytearray(mem_size % unit)]
input(f'{mem_size} Byte memory Occupied, press enter to end.')

 

其实一行命令也可以:

python3 -c "a=' '*字节大小;input()"

比如占用1G内存:

python3 -c "a=' '*1024**3;input()"

 

posted @ 2025-11-29 17:36  小鱼圆又圆  阅读(5)  评论(0)    收藏  举报