python resource 内存资源限制

python resource 内存资源限制

1. 测试脚本 

  mac 中 无效

  linux 中 有效

import os
import time

import psutil
import resource
import sys
try:
    print('---------')
    print(os.getpid())
    print(resource.getrlimit(resource.RLIMIT_AS))
    print('---------')
    # 错误 1024 ** 4 -> 1B
    # 正确 unit B
    resource.setrlimit(resource.RLIMIT_AS, (0.57 * 1024 ** 3, 0.57 * 1024 ** 3))
    print('~~~~~~~~~')
    print(resource.getrlimit(resource.RLIMIT_AS))
    print('~~~~~~~~~')

    print('memory used {}'.format(round(psutil.Process(os.getpid()).memory_full_info().rss, 2)))
    nums = []
    for n in range(10 ** 8):
        # nums.append(n)
        nums.append("a")

    print("ok")
    print(sys.getsizeof(nums))
    print(len(nums))
    print('memory used {}'.format(round(psutil.Process(os.getpid()).memory_full_info().rss, 2)))
    print('==========')

    print(resource.getrlimit(resource.RLIMIT_AS))
    print('==========')
finally:
    print('111111')
    print(sys.getsizeof(nums))
    print(len(nums))
    print(resource.getrlimit(resource.RLIMIT_AS))
    print('memory used {}'.format(round(psutil.Process(os.getpid()).memory_full_info().rss, 2)))
    time.sleep(60)
    print('memory used {}'.format(round(psutil.Process(os.getpid()).memory_full_info().rss, 2)))

 

2. 测试结果

resource rss 设置

进程内存大小【KB】

1* 1024 ** 3

792408

0.8* 1024 ** 3

757432

0.6 * 1024 ** 3

535492

0.57 * 1024 ** 3

535136

0.5 * 1024 ** 3

477052

0.47 * 1024 ** 3

425316

0.4 * 1024 ** 3

338324

   

 

3. 结论

  • resource.setrlimit(resource.RLIMIT_AS, (soft, hard))

   soft, hard 单位是B

  • 如若设置运行内存为1GB, 则resource soft,hard 应略大于该值 1* 1024 ** 3
posted on 2020-11-13 09:45  zj_Rules  阅读(1362)  评论(0编辑  收藏  举报