linux – RES!= CODE DATA在顶部命令的输出信息中,为什么?

前端之家收集整理的这篇文章主要介绍了linux – RES!= CODE DATA在顶部命令的输出信息中,为什么?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
什么’人顶“说是:RES =代码数据
  1. q: RES -- Resident size (kb)
  2. The non-swapped physical memory a task has used.
  3. RES = CODE + DATA.
  4.  
  5. r: CODE -- Code size (kb)
  6. The amount of physical memory devoted to executable code,also known as the 'text resident set' size or TRS.
  7.  
  8. s: DATA -- Data+Stack size (kb)
  9. The amount of physical memory devoted to other than executable code,also known as the 'data >resident set' size or DRS.

当我运行’顶部-p 4258′,我得到以下内容

  1. PID USER PR NI VIRT RES SHR S %cpu %MEM TIME+ CODE DATA COMMAND
  2. 258 root 16 0 3160 1796 1328 S 0.0 0.3 0:00.10 476 416 bash

1796!= 476 416

为什么?

PS:
linux发行版:

  1. linux-iguu:~ # lsb_release -a
  2. 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
  3. Distributor ID: SUSE LINUX
  4. Description: SUSE Linux Enterprise Server 9 (i586)
  5. Release: 9
  6. Codename: n/a

内核版本:

  1. linux-iguu:~ # uname -a
  2. Linux linux-iguu 2.6.16.60-0.21-default #1 Tue May 6 12:41:02 UTC 2008 i686 i686 i386 GNU/Linux

解决方法

我将在一个程序分配和使用内存时发生什么的例子来解释这一点.具体来说,这个程序:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4. #include <string.h>
  5.  
  6. int main(){
  7.  
  8. int *data,size,count,i;
  9.  
  10. printf( "fyi: your ints are %d bytes large\n",sizeof(int) );
  11.  
  12. printf( "Enter number of ints to malloc: " );
  13. scanf( "%d",&size );
  14. data = malloc( sizeof(int) * size );
  15. if( !data ){
  16. perror( "Failed to malloc" );
  17. exit( EXIT_FAILURE );
  18. }
  19.  
  20. printf( "Enter number of ints to initialize: " );
  21. scanf( "%d",&count );
  22. for( i = 0; i < count; i++ ){
  23. data[i] = 1337;
  24. }
  25.  
  26. printf( "I'm going to hang out here until you hit <enter>" );
  27. while( getchar() != '\n' );
  28. while( getchar() != '\n' );
  29.  
  30. exit( EXIT_SUCCESS );
  31. }

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

  1. $./a.out
  2. fyi: your ints are 4 bytes large
  3. Enter number of ints to malloc: 1250000
  4. Enter number of ints to initialize: 500000

Top报告以下信息:

  1. PID USER PR NI VIRT RES SHR S %cpu %MEM TIME+ SWAP CODE DATA COMMAND
  2. <program start>
  3. 11129 xxxxxxx 16 0 3628 408 336 S 0 0.0 0:00.00 3220 4 124 a.out
  4. <allocate 1250000 ints>
  5. 11129 xxxxxxx 16 0 8512 476 392 S 0 0.0 0:00.00 8036 4 5008 a.out
  6. <initialize 500000 ints>
  7. 11129 xxxxxxx 15 0 8512 2432 396 S 0 0.0 0:00.00 6080 4 5008 a.out

相关资料为:

  1. DATA CODE RES VIRT
  2. before allocation: 124 4 408 3628
  3. after 5MB allocation: 5008 4 476 8512
  4. after 2MB initialization: 5008 4 2432 8512

在我malloc’d 5MB的数据后,VIRT和DATA都增加了约5MB,但RES没有.在我触摸了我分配的整数的2MB后,RES的确增加了,但DATA和VIRT保持不变.

VIRT是进程使用的虚拟内存的总量,包括共享和what is over-committed.DATA是不共享的虚拟内存量,而不是代码文本.即,它是进程的虚拟堆栈和堆. RES不是虚拟的:它是对该进程在该特定时间实际使用多少内存的测量.

所以在你的情况下,大的不等式CODE DATA< RES可能是进程包含的共享库.在我的例子(和你的)中,SHR代码数据更接近于RES. 希望这可以帮助.
与top和ps有很多的挥手和巫术.在线有很多文章(咆哮?),关于这些不符之处.例如thisthis.

猜你在找的Linux相关文章