如何解释进程的top命令内存使用情况?

问题描述 投票:0回答:1

我想了解作为进程的 top 命令一部分显示的 VIRT、RES 和 SHR 值的重要性。

我编写了一个简单的程序,它本身不分配任何私有内存。它只是使用系统功能。

#include<unistd.h>
#include<stdlib.h>
#include<iostream>
using namespace std;

int main(int argc,char *argv[]){

        while(1) {
                cout<<endl<<"This is a dummy program"<<endl;
                sleep(1);
        }

        exit(EXIT_SUCCESS);
}

我已使用以下命令生成了执行。

g++ -o Dummy1.exe -g -Wall Dummy.cpp

以下是运行上述执行时的顶部输出。

 PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND 
 4461 soumajit  20   0   13988   1772   1628 S   0.0  0.0   0:00.00 Dummy1.exe

对 SHR 值有贡献的最小共享内存段是什么?
当我明确未声明任何变量时,为什么 RES 和 SHR 之间存在微小差异?
VIRT的这个值是如何分配的?

我已经捕获了 strace 输出。它是否包含对 SHR 值有贡献的共享内存段的大小?

brk(NULL)                               = 0x561940984000
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=130951, ...}) = 0
mmap(NULL, 130951, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f54a6877000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/x86_64-linux-gnu/libstdc++.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\220\304\10\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=1594864, ...}) = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f54a6875000
mmap(NULL, 3702848, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f54a62e7000
mprotect(0x7f54a6460000, 2097152, PROT_NONE) = 0
mmap(0x7f54a6660000, 49152, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x179000) = 0x7f54a6660000
mmap(0x7f54a666c000, 12352, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f54a666c000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\260\34\2\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=2030544, ...}) = 0
mmap(NULL, 4131552, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f54a5ef6000
mprotect(0x7f54a60dd000, 2097152, PROT_NONE) = 0
mmap(0x7f54a62dd000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1e7000) = 0x7f54a62dd000
mmap(0x7f54a62e3000, 15072, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f54a62e3000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libm.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\200\272\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=1700792, ...}) = 0
mmap(NULL, 3789144, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f54a5b58000
mprotect(0x7f54a5cf5000, 2093056, PROT_NONE) = 0
mmap(0x7f54a5ef4000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x19c000) = 0x7f54a5ef4000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libgcc_s.so.1", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\300*\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=96616, ...}) = 0
mmap(NULL, 2192432, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f54a5940000
mprotect(0x7f54a5957000, 2093056, PROT_NONE) = 0
mmap(0x7f54a5b56000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x16000) = 0x7f54a5b56000
close(3)                                = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f54a6873000
arch_prctl(ARCH_SET_FS, 0x7f54a6873d00) = 0
mprotect(0x7f54a62dd000, 16384, PROT_READ) = 0
mprotect(0x7f54a5b56000, 4096, PROT_READ) = 0
mprotect(0x7f54a5ef4000, 4096, PROT_READ) = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f54a6871000
mprotect(0x7f54a6660000, 40960, PROT_READ) = 0
mprotect(0x56193f2a2000, 4096, PROT_READ) = 0
mprotect(0x7f54a6897000, 4096, PROT_READ) = 0
munmap(0x7f54a6877000, 130951)          = 0
brk(NULL)                               = 0x561940984000
brk(0x5619409a5000)                     = 0x5619409a5000
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 3), ...}) = 0
write(1, "\n", 1)                       = 1
write(1, "This is dummy program\n", 22) = 22
nanosleep({tv_sec=1, tv_nsec=0}, {tv_sec=0, tv_nsec=203523438}) = ? ERESTART_RESTARTBLOCK (Interrupted by signal)
--- SIGINT {si_signo=SIGINT, si_code=SI_KERNEL} ---
+++ killed by SIGINT +++

以下是测试环境

Linux soumajit-HP-Pavilion-Desktop-590-p0xxx 5.4.0-71-generic #79~18.04.1-Ubuntu SMP Thu Mar 25 05:45:39 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
linux memory-management process shared-memory strace
1个回答
0
投票

RES 是进程当前使用的常驻物理内存,其值可能包含其访问的 SHR 内存。

你可以看到这个:https://www.linuxquestions.org/questions/linux-server-73/does-res-in-top-reflect-any-shared-memory-803032/

© www.soinside.com 2019 - 2024. All rights reserved.