Checking if malloc is allocating all memory using pmap

huangapple go评论71阅读模式
英文:

Checking if malloc is allocating all memory using pmap

问题

I'm trying to understand the basics of memory mapping.
I wrote the following small program: it allocates some number of bytes passed as an arg, then continually sets the bytes until the program is terminated:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char* argv[]) {
    printf("pid = %d\n", getpid());

    double mb = strtod(argv[1], NULL);
    size_t bytes = 1000 * mb;
    printf("allocating %f mb, or %ld bytes\n", mb, bytes);

    char* ptr = (char*)malloc(bytes);
    printf("allocated at %p\n", ptr);

    size_t i=0;
    while (1) {
        for (i=0; i<bytes; i++) {
            ptr[i] = 'a';
        }
    }
    return 0;
}

When I run ./a.out 10000 and then pmap <pid> -X for the corresponding pid, I find that the ptr starts at 0x7ff9cf753010 but the following is shown for the heap:

         Address Perm   Offset Device   Inode  Size   Rss  Pss Referenced Anonymous LazyFree ShmemPmdMapped FilePmdMapped Shared_Hugetlb Private_Hugetlb Swap SwapPss Locked THPeligible Mapping
    559eba856000 rw-p 00000000  00:00       0   132     4    4          4         4        0              0             0              0               0    0       0      0           0 [heap]
    7ff9cf753000 rw-p 00000000  00:00       0  9768  9768 9768       9768      9768        0              0             0              0               0    0       0      0           0

I'm confused because the size is 9768, not 10,000. I'm guessing the 132 bits is some kind of header for the heap? Also, the pointer value is not the same... Would appreciate any kind of clarity on this.

英文:

I'm trying to understand the basics of memory mapping.
I wrote the following small program: it allocates some number of bytes passed as an arg, then continually sets the bytes until the program is terminated:

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;unistd.h&gt;

int main(int argc, char* argv[]) {
    printf(&quot;pid = %d\n&quot;, getpid());

    double mb = strtod(argv[1], NULL);
    size_t bytes = 1000 * mb;
    printf(&quot;allocating %f mb, or %ld bytes\n&quot;, mb, bytes);

    char* ptr = (char*)malloc(bytes);
    printf(&quot;allocated at %p\n&quot;, ptr);

    size_t i=0;
    while (1) {
        for (i=0; i&lt;bytes; i++) {
            ptr[i] = &#39;a&#39;;
        }
    }
    return 0;
}

When I run ./a.out 10000 and then pmap &lt;pid&gt; -X for the corresponding pid, I find that the ptr starts at 0x7ff9cf753010 but the following is shown for the heap:

         Address Perm   Offset Device   Inode  Size   Rss  Pss Referenced Anonymous LazyFree ShmemPmdMapped FilePmdMapped Shared_Hugetlb Private_Hugetlb Swap SwapPss Locked THPeligible Mapping
    559eba856000 rw-p 00000000  00:00       0   132     4    4          4         4        0              0             0              0               0    0       0      0           0 [heap]
    7ff9cf753000 rw-p 00000000  00:00       0  9768  9768 9768       9768      9768        0              0             0              0               0    0       0      0           0

I'm confused because the size is 9768, not 10,000. I'm guessing the 132 bits is some kind of header for the heap? Also, the pointer value is not the same ... Would appreciate any kind of clarity on this.

答案1

得分: 4

The difference in size you see isn't actually a difference in size like you think, it's due to k being used to mean both 1000 and 1024 in combination with B for bytes, sometimes Ki is used to specifically mean 1024 instead but that's not always the case especially in older interfaces.

If you keep that in mind and look at the size again you'll find that 9768 is exactly the size you allocated in KiB rounded to the nearest multiple of a page size (commonly 4KiB): 10,000,000 / 1024 = 9765.625 which rounds to 9768.

The difference in pointer 7ff9cf753000 vs 7ff9cf753010 is the bookkeeping header malloc uses, which you blamed the missing bytes on.

英文:

The difference in size you see isn't actually a difference in size like you think, it's due to k being used to mean both 1000 and 1024 in combination with B for bytes, sometimes Ki is used to specifically mean 1024 instead but that's not always the case especially in older interfaces.

If you keep that in mind and look at the size again you'll find that 9768 is exactly the size you allocated in KiB rounded to the nearest multiple of a page size (commonly 4KiB): 10,000,000 / 1024 = 9765.625 which rounds to 9768.

The difference in pointer 7ff9cf753000 vs 7ff9cf753010 is the bookkeeping header malloc uses, which you blamed the missing bytes on.

huangapple
  • 本文由 发表于 2023年3月21日 03:24:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75794489-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定