简体   繁体   English

监视子进程的内存使用情况

[英]Monitor memory usage of child process

I have a Linux daemon that forks a few children and monitors them for crashes (restarting as needed). 我有一个Linux守护程序,该守护程序派生了几个孩子并监视它们的崩溃(根据需要重新启动)。 It will be great if the parent could monitor the memory usage of child processes - to detect memory leaks and restart child processes when the go beyond a certain size. 如果父级可以监视子进程的内存使用情况,则可以很好-检测内存泄漏并在超出特定大小时重新启动子进程。 How can I do this? 我怎样才能做到这一点?

You should be able to get detailed memory information out of /proc/{PID}/status: 您应该能够从/ proc / {PID} / status中获取详细的内存信息:

Name:   bash
State:  S (sleeping)
Tgid:   6053
Pid:    6053
PPid:   6050
TracerPid:  0
Uid:    1007    1007    1007    1007
Gid:    1007    1007    1007    1007
FDSize: 256
Groups: 1007 
VmPeak:    48076 kB
VmSize:    48044 kB
VmLck:         0 kB
VmHWM:      4932 kB
VmRSS:      2812 kB
VmData:     2232 kB
VmStk:        84 kB
VmExe:       832 kB
VmLib:      6468 kB
VmPTE:       108 kB
Threads:    1
SigQ:   0/8190
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000000000
SigIgn: 0000000000001010
SigCgt: 0000000188020001
CapInh: 0000000000000000
CapPrm: 0000000000000000
CapEff: 0000000000000000
Cpus_allowed:   0f
Mems_allowed:   00000000,00000001
voluntary_ctxt_switches:    69227121
nonvoluntary_ctxt_switches: 19071

However, unless memory leaks are dramatic, it's difficult to detect them looking at process statistics, because malloc and free are usually quite abstract from system calls (brk/sbrk) to which they correspond. 但是,除非内存泄漏非常严重,否则很难从进程统计信息中检测到它们,因为malloc和free通常是从它们所对应的系统调用(brk / sbrk)中抽象出来的。

You can also check into /proc/${PID}/statm. 您还可以检入/ proc / $ {PID} / statm。

you could try having a monitor script running vmstat in parallel with your process (note this is not a good idea if you're running this script multiple times as you'll get multiple vmstat copies). 您可以尝试让监视脚本与进程并行运行vmstat(请注意,如果您多次运行此脚本,这将不是一个好主意,因为您将获得多个vmstat副本)。 Then this monitor script can take the free memory plus the buffer and cache size to get the amount of memory that the OS has available and you can track that. 然后,此监视脚本可以占用可用内存,再加上缓冲区和高速缓存大小,以获取操作系统可用的内存量,您可以对其进行跟踪。 Then if that gets below some threshold you can check for the biggest processes by calling ps -e -o... (see man page for details but try vsz,pcpu,user,pid,args as a starting point). 然后,如果该阈值低于某个阈值,则可以通过调用ps -e -o ...检查最大的进程(有关详细信息,请参见手册页,但尝试以vsz,pcpu,user,pid,args为起点)。

I'd advise running this monitor as a separate process and having it kill the rogue process when it gets too large. 我建议将此监视器作为一个单独的进程运行,并在它变得太大时杀死恶意进程。 You could restrict the set of processes monitored by using the 您可以使用

-u user-name

parameter to ps. ps的参数。

This is all a hack (UK meaning) though - the right solution though is to fix the leaks, assuming you have the code. 不过,这全都是骇客(UK意思)-正确的解决方案是,假设您有代码,则修复漏洞。

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

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