问题
I'm reading about virtual memory and my conclusions are the following:
malloc(size);
- malloc does not guarantee to return physically contiguous memory. It guarantees to return virtually contiguous memory. Especially it is true when size > 4KB because 4KB is the size of page. (On Linux systems).
Am I right or am I wrong? Please explain.
回答1:
malloc does not guarantee returning physically contiguous memory
yes
It guarantees returning virtually contiguous memory
yes
Especially it is true when size > 4KB because 4KB is a size of page. ( On Linux systems).
Being contiguous memory does not imply that it will also be page aligned. The allcated memory can start from any address in heap. So whatever OS uses the page size it does not affect the allocation nature of malloc.
回答2:
malloc just allocate memory from heap , in a virtual memory system complete allocate memory to a process different section (text , data , bss ,heap , stack) is virtually contiguous. Every section of process allocated a virtual memory region by using mmap.
in case of extension of heap it is basically allocated a virtual memory region allocated by brk() system call. it is not affected if size is greater the 4kb. though linux has concept of HUGETLB page to avoid frequent TLB misses, but that is internal to Linux.
来源:https://stackoverflow.com/questions/32832669/malloc-does-not-guarantee-returning-physically-contiguous-memory