Monitoring the memory usage of the process at any time is very important for the stability and peace of the service. For example, the size of the process memory for each php-fpm in the nginx+php-fpm technology stack will affect the number of processes open. .
So how do you see the amount of memory used by running processes? Linux provides the top command, which can view the memory usage of each process in real time. There are two main parameters to note, RES and DATA parameters.
Top -p pid Then use f and then click the DATA corresponding identifier to see the DATA column.
RES is actually used by the process. VIRT is the sum of virtual and physical. For example, the php-fpm process has a memory limit. This can only limit the RES.
You can use the following practical example to explain:
#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 );
}Copy code
This example shows how much memory is allocated, how much memory is initialized, 1250,000 bytes are allocated at runtime, and 500000 bytes are initialized:
$ ./a.out
fyi: your ints are 4 bytes large
Enter number of ints to malloc: 1250000
Enter number of ints to initialize: 500000
TOp view results displayCopy code
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.outCopy code
After malloc had 1250,000, VIRT and DATA increased, but RES did not change. VIRT and DATA did not change after initialization, but RES increased.
VIRT is all the virtual memory of the process, including shared, into the dynamic link library, etc. DATA is the size of the virtual stack and heap. RES is not virtual, it is the memory that is actually used in memory at a specified time.