python占用内存脚本(极简)

python占用内存脚本(极简)

 

import sysif 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.')