首页 > 解决方案 > 如何测量 multiprocessing.shared_memory.SharedMemory 分配/使用的实际共享内存?

问题描述

我尝试使用“/usr/bin/time -v”来测量以下代码的实际内存使用情况。但是发现无论给 SharedMemory(..) 的大小是多少,报告的“最大驻留集大小 (kbytes)”都是相同的。

from multiprocessing.shared_memory import SharedMemory

data_bytes_len = 10 ** 8
shm = SharedMemory(create=True, size=data_bytes_len)
print("size of shm:", shm.size)
shm.unlink()

见下面的输出

大小为 100:

size of shm: 100
        Command being timed: "python shm.py"
        User time (seconds): 0.04
        System time (seconds): 0.00
        Percent of CPU this job got: 100%
        Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.05
        Average shared text size (kbytes): 0
        Average unshared data size (kbytes): 0
        Average stack size (kbytes): 0
        Average total size (kbytes): 0
        Maximum resident set size (kbytes): 13124
        Average resident set size (kbytes): 0
        Major (requiring I/O) page faults: 0
        Minor (reclaiming a frame) page faults: 2809
        Voluntary context switches: 0
        Involuntary context switches: 2
        Swaps: 0
        File system inputs: 0
        File system outputs: 0
        Socket messages sent: 0
        Socket messages received: 0
        Signals delivered: 0
        Page size (bytes): 4096
        Exit status: 0

大小如果 100000000

size of shm: 100000000
        Command being timed: "python shm.py"
        User time (seconds): 0.03
        System time (seconds): 0.00
        Percent of CPU this job got: 100%
        Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.04
        Average shared text size (kbytes): 0
        Average unshared data size (kbytes): 0
        Average stack size (kbytes): 0
        Average total size (kbytes): 0
        Maximum resident set size (kbytes): 12996
        Average resident set size (kbytes): 0
        Major (requiring I/O) page faults: 0
        Minor (reclaiming a frame) page faults: 2811
        Voluntary context switches: 0
        Involuntary context switches: 2
        Swaps: 0
        File system inputs: 0
        File system outputs: 0
        Socket messages sent: 0
        Socket messages received: 0
        Signals delivered: 0
        Page size (bytes): 4096
        Exit status: 0

标签: pythonperformancememoryshared-memorymeasurement

解决方案


推荐阅读