简体   繁体   English

Python多处理分叉的空python进程的私有内存

[英]private memory of an empty python process forked by Python multiprocessing

I'm creating several child processes via Python multiprocessing, but these child processes use a lot heap private memory even when they're empty processes. 我正在通过Python多处理创建多个子进程,但是这些子进程即使在为空进程时也会占用大量堆专用内存。 This situation is getting worse when running on RHEL with THP(Transparent Hugepage) enabled. 在启用了THP(透明大页面)的RHEL上运行时,这种情况变得越来越糟。

  • what's in an empty child process's heap private memory? 空子进程的堆专用内存中有什么?
  • Under linux COW (copy-on-write), shouldn't child process shares all memory as it doesn't created/modify any mem pages? 在Linux COW(写时复制)下,子进程不应该共享所有内存,因为它没有创建/修改任何内存页吗? or the child process is trying to modify/write mem pages, then what kind of data it's trying to modify/write? 还是子进程试图修改/写入内存页面,那么它试图修改/写入哪种数据?
  • or it's due to like python object reference count or something? 还是由于类似python对象引用计数之类的?

Here is a simple example to demo this: 这是一个演示的简单示例:

import os
import multiprocessing

print parent process's heap memory in /proc/<pid>/smaps

def emptyProcess():
    print child process's heap memory in /proc/<pid>/smaps
    return

multiprocessing.Process(name='p1', target=emptyProcess).start()

Output: 输出:

parent:  pid: 20920:   rss:8228864, shr:2781184, priv:5447680, swap:0, pss:6154240

child: pid: 20921:   rss:6397952, shr:5472256, priv:925696, swap:0, pss:3381248

what's in child process's priv memory (925696B, or 664KB in heap)? 子进程的专用内存(925696B,或堆中的664KB)中有什么?

Parent process heap memory: 父进程堆内存:

006cc000-00be4000 rw-p 00000000 00:00 0 [heap] 006cc000-00be4000 rw-p 00000000 00:00 0 [堆]

Size: 5216 kB 大小:5216 kB

Rss: 4120 kB Rss:4120 kB

Pss: 4120 kB pss:4120 kB

Shared_Dirty: 0 kB Shared_Dirty:0 kB

Private_Dirty: 4120 kB 私人肮脏:4120 kB

Referenced: 4120 kB 参考:4120 kB

Anonymous: 4120 kB 匿名:4120 kB

AnonHugePages: 0 kB 页面数:0 kB

KernelPageSize: 4 kB 内核页面大小:4 kB

Child process heap memory: 子进程堆内存:

006cc000-00be4000 rw-p 00000000 00:00 0 [heap] 006cc000-00be4000 rw-p 00000000 00:00 0 [堆]

Size: 5216 kB 大小:5216 kB

Rss: 4396 kB Rss:4396 kB

Pss: 2530 kB 积分:2530 kB

Shared_Dirty: 3732 kB Shared_Dirty:3732 kB

Private_Dirty: 664 kB 私人_脏:664 kB

Referenced: 676 kB 参考:676 kB

Anonymous: 4396 kB 匿名:4396 kB

AnonHugePages: 0 kB 页面数:0 kB

KernelPageSize: 4 kB 内核页面大小:4 kB

One of the primary things in each process is the Python interpreter / VM. 每个过程中的主要内容之一是Python解释器/ VM。 If this were a C program, you'd see a much different picture, but even with an "empty" Python process, you still incur the overhead of an interpreter unless you use threading. 如果这是C程序,您会看到截然不同的画面,但是即使使用“空” Python进程,除非使用线程,否则仍会产生解释器的开销。 Each Python interpreter has a chunk of heap, stack and code, and Python's multiprocessing is a wrapper (as far as I know) around Linux processes; 每个Python解释器都有大量的堆,堆栈和代码,Python的多重处理是Linux进程的包装(据我所知)。 so basically you are dealing with fork() . 所以基本上您正在处理fork() Forking a new process means you get a new Python interpreter. 分叉新进程意味着您将获得一个新的Python解释器。 Even though the OS is pretty smart about Copy On Write, the overhead of the Python interpreters add up. 即使操作系统对于“写时复制”非常聪明,但Python解释器的开销还是会增加的。

My recommendation would be to try Python threads, or switch this to a non-interpreted language to reduce the process overhead. 我的建议是尝试使用Python线程,或将其切换为非解释语言以减少进程开销。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM