英文:
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 <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.
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论