Why is pmap [heap] readings different from the actual program's heap memory usage?

69 Views Asked by At

I was learning how pmap analyzes the memory usage of a program. Running this very basic program that initializes an int array with 1,000,000 elements should theoretically use 4,000,000 bytes (I tried adding or removing stdlib.h, but I got the same memory readings):

#include <stdlib.h>
int main() {
    int h[1000000];
    for(int i = 0; i < 1000000; i++) {
        h[i] = i;
    }
    while(1) {

    }
}

This is the pmap -x result (the program is called fixedmemory):

10356: ./fixedmemory
Address            Kbytes     PSS   Dirty    Swap  Mode  Mapping
0000000000400000     504     148       0       0 r-x--  fixedmemory
000000000048d000      16      12       8       0 r----  fixedmemory
0000000000491000      12      12      12       0 rw---  fixedmemory
0000000000494000      20       8       8       0 rw---    [anon]
000000002dd7d000     136       8       8       0 rw---    [heap]
000000739e568000       4       0       0       0 r----    [vvar]
000000739e569000       4       0       0       0 r-x--    [vdso]
0000007fc14ef000    3912    3912    3912       0 rw---    [stack]
----------------  ------  ------  ------  ------
total               4608    4100    3948       0

Looking at the PSS results because they are supposed to be more accurate[1], I noticed that the stack memory usage was 3912. Supposing that the metric uses Kibibytes (KiB), the results are accurate and off by around 0.1472%, which is marginal.

If I then try to allocate memory using malloc using this program:

#include <stdlib.h>  
int main() {
    int* s = malloc(1000000*4); 
    for(int i = 0; i < 1000000; i++) {
        s[i] = i;
    }
    while(1) {

    }
    free(s);
}

These are the results that I get:

18296: ./fixedmemory
Address            Kbytes     PSS   Dirty    Swap  Mode  Mapping
0000000000400000     504       4       0       0 r-x--  fixedmemory
000000000048d000      16       8       8       0 r----  fixedmemory
0000000000491000      12      12      12       0 rw---  fixedmemory
0000000000494000      20       8       8       0 rw---    [anon]
000000000f2e8000     136       8       8       0 rw---    [heap]
000000708fd6a000    3908    3908    3908       0 rw---    [anon]
000000709013b000       4       0       0       0 r----    [vvar]
000000709013c000       4       0       0       0 r-x--    [vdso]
0000007fdacbd000     132      12      12       0 rw---    [stack]
----------------  ------  ------  ------  ------
total               4736    3960    3956       0

Why is the heap reporting 8 KiB even though the heap memory usage should, theoretically, be equal to 4,000,000 bytes or 3906.25 KiB? I noticed that there is a memory mapping called [anon] that reports an almost exact theoretical value of 3908 KiB. Is this the actual memory used by malloc, or am I missing something? If it is, then why is it called [anon]? And If I were to benchmark a C program's memory usage, would [anon]'s reading be always equal to the heap's memory usage of the program?

0

There are 0 best solutions below