The physical memory map for Linux is completely independent from the virtual map and is designed to maximize contiguous space. Given that the kernel image will always be at the start of DRAM, the Linux kernel maximizes contiguous space by allocating runtime memory from the end of physical DRAM moving downward.
The kernel starts by breaking available memory out into large, contiguous blocks (typically 4MB or more).It then maintains memory using the buddy system, where physical memory is always allocated in combinations of blocks of 2^n pages (where n is the order, that is, 4K is a 0 order block, 8K is a 1st order block, 16K is a 2nd order block, etc).
Linux物理内存的映射完全独立于虚拟内存的映射,而且尽可能映射到连续的空间。假定内核映像总是位于DRAM的开始处,Linux内核尽可能让空间连续,这是通过从物理DRAM的末端向下移动来分配运行时内存而达到的。 内核一开始把可用内存分割为大而连续的块(通常为4MB 或更多)。此后,内核利用伙伴算法来管理内存,这里,物理内存的分配总是2^n个页所形成块的组合(这里,n是幂,4k就是幂为0的块,8K是幂为1块,16K为幂为2的块,如此等等)。 When physical memory is allocated, that is, on process start, or when malloc’ed memory is written to (copy-on-write), the kernel scans for the smallest order block that will fill fit starting from the top of DRAM. As the number of running processes increases, or new allocations are spawned from drivers or processes, the physical memory used grows downward.
When a process exits or a large enough memory block is freed, its DRAM space is unmapped and becomes a gap in memory. This process is called memory fragmentation and becomes more and more prevalent the longer a device is used and the more frequently the use case changes (such as checking email, playing an mp3, watching a video, etc). As new processes and allocations are created, the gaps are filled whenever possible, but fragmentation is still an inevitable part of any OS memory map.