简体   繁体   中英

How to understand Linux top command result for a process?

I am trying to get a general idea of memory fields in linux top command while running my program. Its a simple c program where I print top results for my program using its process id in batch mode before dynamically allocating memory, after allocating memory and after freeing memory. Here is the top command i am using in shell:

top -p my_pid -b -n 1

I am using following function to create a 2d array dynamically

int** Create2DArray(unsigned int size, int** addr_of_ptr_to_ints_array){

    int** int_ptrs_array = (int**)malloc(size * sizeof(int*));
    *addr_of_ptr_to_ints_array = (int*)malloc(size * size * sizeof(int));
    for (int i = 0; i < size; i++) {
        int_ptrs_array[i] = *addr_of_ptr_to_ints_array + (i * size);
    }

    return int_ptrs_array;
}

as

 int* int_array1;  
 int** int_ptr_array1 = Create2DArray(n, &int_array1); 

where n = 64 so array size is (4096*size(int) = 16kb) + 64*size(int*) = 16.25kb and using following code to free the memory.

free(int_array1);free(int_ptr_array1);

The output I am getting from top command before, after allocation and after freeing memory is following:

EDIT:
Pagesize: 4096

--- Before Array Allocation.
top - 17:45:01 up 17:39,  3 users,  load average: 0.36, 0.26, 0.34
Tasks:   1 total,   0 running,   1 sleeping,   0 stopped,   0 zombie
%Cpu(s): 15.4 us,  2.5 sy,  0.0 ni, 81.7 id,  0.3 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem:   2065072 total,  1675324 used,   389748 free,    48344 buffers
KiB Swap:  1046524 total,    25168 used,  1021356 free,   485028 cached

  PID  VIRT  RES  SHR  %CPU %MEM SWAP CODE DATA COMMAND
14606 10756  616  524   0.0  0.0    0    8 8508 mx

--- Array Allocated.
top - 17:45:03 up 17:39,  3 users,  load average: 0.36, 0.26, 0.34
Tasks:   1 total,   0 running,   1 sleeping,   0 stopped,   0 zombie
%Cpu(s): 15.4 us,  2.5 sy,  0.0 ni, 81.7 id,  0.3 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem:   2065072 total,  1675364 used,   389708 free,    48344 buffers
KiB Swap:  1046524 total,    25168 used,  1021356 free,   485028 cached

  PID  VIRT  RES  SHR  %CPU %MEM SWAP CODE DATA COMMAND
14606 11780  616  524   0.0  0.0    0    8 9532 mx

--- Array Updated.
top - 17:45:06 up 17:39,  3 users,  load average: 0.49, 0.29, 0.35
Tasks:   1 total,   0 running,   1 sleeping,   0 stopped,   0 zombie
%Cpu(s): 15.4 us,  2.5 sy,  0.0 ni, 81.7 id,  0.3 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem:   2065072 total,  1675348 used,   389724 free,    48344 buffers
KiB Swap:  1046524 total,    25168 used,  1021356 free,   485028 cached

  PID  VIRT  RES  SHR  %CPU %MEM SWAP CODE DATA COMMAND
14606 11784  616  524   0.0  0.0    0    8 9536 mx

Questions:

Q1. Why RES is constant before and after array allocation?

Q2. Why VIRT increased after array was deallocated?

Q3. I was expecting the anonymous memory to be 16.5kb after allcation but (RES - SHR)*(pagesize) = (616 - 524)*4096 = 376.832Kb although anonymous memory should represent the memory created by malloc commands according to this article .

Any help is appreciated. Thanks

You don't say if you have touched any of the allocated memory or not. Linux (by default) has a slightly unusual memory allocation strategy: when you call malloc the call always succeeds. "But what if there is no memory?" you ask. Well malloc doesn't get you a page of physical memory, just a page in your virtual address space (which is basically limitless). You will get a page fault when you try to access any of these pages and at this point the kernel will find you a physical page for your memory.

So that leaves us with the following:

Q1) RES is unchanged because you didn't ever use any more memory by allocating

Q2) VIRT (virtual memory allocated to task) has increased because you allocated virtual memory. This remains even after you free it, but since it isn't backed by physical memory it really doesn't matter.

I don't think I have enough information to answer Q3.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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