Is linux kernel allocating memory consecutively, e.g from malloc
? If there's no big part available, but smaller part in pieces that works as a total, will Linux use that ?
相关问题
- What uses more memory in c++? An 2 ints or 2 funct
- Is shmid returned by shmget() unique across proces
- how to get running process information in java?
- Achieving the equivalent of a variable-length (loc
- Error building gcc 4.8.3 from source: libstdc++.so
In linux kernel, memory allocation is done using kmalloc or vmalloc. kmalloc allocates physically contiguous memories while vmalloc allocates physically non-contiguous memories. Both kmalloc and vmalloc will give you contiguous virtual memory space. This is done using page tables.
I am assuming that in the question, by "consecutive", you are referring to physical memory.
All the addresses you see when a process executes, for example when you use
gdb
oraddr2line
, are virtual addresses. The mapping to physical memory stays under the hood.malloc
is usually not a system call. It is a wrapper around a platform-specific system call (such asmmap
orbrk
). When invoked by a process, it results in the memory of the invoking process to be extended.For example,
malloc(40960)
could request 10 pages of memory, assuming 4KB page size. The Memory Management Unit (MMU) doesn't immediately allocate 10 pages in the physical memory. Instead, new entries (10 in this case) are added to the address space of the invoking process, that is, new entries are added in the page table of this process. This virtual memory will be contiguous.Now assume that later during the execution, the process tries to use this newly allocated memory, At that moment, a page fault is invoked because the process touched unallocated memory. The MMU will then allocate an actual physical page (4096 continuous bytes) in RAM, update the page table and resume execution of the process.
So the only thing we can be sure of is that 4096 bytes (one page) out of 40960 bytes are contiguous in physical memory (RAM), although the entire 40960 bytes (10 pages) appears as contiguous in the virtual memory. Any or all of the 10 pages may be mapped to pages that are scattered in RAM.
Virtual memory hides this and presents a clean, contiguous space to a process. The MMU uses the page table to provide a backend support by mapping virtual to physical memory.
I believe that the physical mapping of the allocated memory can be non-contiguous using malloc() and really is the OS' responsibility. It may or may not be contiguous.
A function comes to mind named kmalloc() which will allocate contiguous 'physical' memory, if available.