Linux kernel memory management, does it use consecutive memory pages all the time?

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 ?


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 or addr2line, 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 as mmap or brk). 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.

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.

Need Your Help

Symfony2 Cron command path

php symfony cron

I set up a command in Symfony which is running well manually with :

Detect user mobile device on Facebook graph API


Is there a way of detecting the user mobile device or at least know if the user is also using Facebook on a mobile device?