简介
用python实现简易的大量内存占用程序
环境
- python 2.7
代码
- main.py
#!/usr/bin/python
import time
huge_items = range(108000000)
while True:
print('sleep.')
time.sleep(60)
运行
python2 main.py
结果确认
- ps命令确认
ps -elfy | grep pytho[n]
...
D huuinn 6324 5685 27 80 0 2062972 641697 lock_p 14:56 pts/2 00:00:04 python2 main.py
虚机机分配的内容为3G, 程序运行时,大概占用2G左右的内存
Killed出现时
huuinn@ubuntu:~/project/TAV/00015_PYTHON_SYSTEM_MEMORY (master)*$ python2 main.py
Killed
当程序被oom-killer杀掉时,通过dmesg命令来查看
...
[ 4625.062913] Out of memory: Kill process 6430 (python2) score 509 or sacrifice child
[ 4625.062938] Killed process 6430 (python2) total-vm:3178344kB, anon-rss:2269632kB, file-rss:12kB, shmem-rss:0kB
注意点
- 可根据机器内存大小,调节range里的值
- 可以用来做机器的性能测试等