RES!= CODE + DATA在top命令的输出信息中,为什么?

red*_*x10 14 linux memory top-command

'man top'所说的是:RES = CODE + DATA

q: RES -- Resident size (kb)
The non-swapped physical memory a task has used.
RES = CODE + DATA.

r: CODE -- Code size (kb)
The amount of physical memory devoted to executable code, also known as the 'text        resident set' size or TRS.

s: DATA -- Data+Stack size (kb)
The amount of physical memory devoted to other than executable code, also known as the   'data >resident set' size or DRS.
Run Code Online (Sandbox Code Playgroud)

当我运行'top -p 4258'时,我得到以下内容:

PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  CODE DATA COMMAND
258 root      16   0  3160 1796 1328 S  0.0  0.3   0:00.10  476  416 bash
Run Code Online (Sandbox Code Playgroud)

1796!= 476 + 416

为什么?

ps:linux发行版:

linux-iguu:~ # lsb_release -a
LSB Version:    core-2.0-noarch:core-3.0-noarch:core-2.0-ia32:core-3.0-ia32:desktop-3.1-ia32:desktop-3.1-noarch:graphics-2.0-ia32:graphics-2.0-noarch:graphics-3.1-ia32:graphics-3.1-noarch
Distributor ID: SUSE LINUX
Description:    SUSE Linux Enterprise Server 9 (i586)
Release:        9
Codename:       n/a
Run Code Online (Sandbox Code Playgroud)

内核版本:

linux-iguu:~ # uname -a
Linux linux-iguu 2.6.16.60-0.21-default #1 Tue May 6 12:41:02 UTC 2008 i686 i686 i386 GNU/Linux
Run Code Online (Sandbox Code Playgroud)

Chr*_*lan 26

我将借助于程序分配和使用内存时会发生什么的示例来解释这一点.具体来说,这个程序:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

int main(){

        int *data, size, count, i;

        printf( "fyi: your ints are %d bytes large\n", sizeof(int) );

        printf( "Enter number of ints to malloc: " );
        scanf( "%d", &size );
        data = malloc( sizeof(int) * size );
        if( !data ){
                perror( "failed to malloc" );
                exit( EXIT_FAILURE );
        }

        printf( "Enter number of ints to initialize: " );
        scanf( "%d", &count );
        for( i = 0; i < count; i++ ){
                data[i] = 1337;
        }

        printf( "I'm going to hang out here until you hit <enter>" );
        while( getchar() != '\n' );
        while( getchar() != '\n' );

        exit( EXIT_SUCCESS );
}
Run Code Online (Sandbox Code Playgroud)

这是一个简单的程序,它会询问您要分配多少个整数,分配它们,询问有多少个整数要初始化,然后初始化它们.对于我分配1250000个整数并初始化500000个整数的运行:

$ ./a.out
fyi: your ints are 4 bytes large
Enter number of ints to malloc: 1250000
Enter number of ints to initialize: 500000
Run Code Online (Sandbox Code Playgroud)

Top报告以下信息:

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  SWAP CODE DATA COMMAND
<program start>
11129 xxxxxxx   16   0  3628  408  336 S    0  0.0   0:00.00 3220    4  124 a.out
<allocate 1250000 ints>
11129 xxxxxxx   16   0  8512  476  392 S    0  0.0   0:00.00 8036    4 5008 a.out
<initialize 500000 ints>
11129 xxxxxxx   15   0  8512 2432  396 S    0  0.0   0:00.00 6080    4 5008 a.out
Run Code Online (Sandbox Code Playgroud)

相关信息是:

                          DATA CODE  RES VIRT
before allocation:         124    4  408 3628
after 5MB allocation:     5008    4  476 8512
after 2MB initialization: 5008    4 2432 8512
Run Code Online (Sandbox Code Playgroud)

在我对5MB数据进行malloc后,VIRT和DATA都增加了大约5MB,但RES却没有.在触及我分配的2MB整数后,RES确实增加了,但DATA和VIRT保持不变.

VIRT是进程使用的虚拟内存总量,包括共享内容和过度提交内容.DATA是未共享且不是代码文本的虚拟内存量.即,它是进程的虚拟堆栈和堆.RES不是虚拟的:它是对该进程在该特定时间实际使用的内存量的测量.

因此,在您的情况下,大的不等式CODE + DATA <RES可能是该过程包含的共享库.在我的例子中(和你的),SHR + CODE + DATA更接近于RES.

希望这可以帮助.顶部和ps有很多挥手和伏都教.有很多文章(咆哮?)在网上关于这些差异.例如,这个.