malloc does not guarantee returning physically contiguous memory

﹥>﹥吖頭↗ 提交于 2021-02-19 04:25:07

问题


I'm reading about virtual memory and my conclusions are the following:

malloc(size);
  1. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!