Linux memory: The principle of operation
10:22, 01.12.2023
On computer specifications, particularly for servers, N gigabytes of RAM are typically included to indicate the available physical memory. The allocation of these resources is managed by operating systems, so each app has its own set of resources, a virtual address spaces, to make each application function as if it were independent.
This way programs do not have to be aware of each other's memory usage or the total available physical memory.
Linux uses a hierarchical set of data structures and specialized hardware mechanisms to track the correspondence between physical and virtual memory. They are together referred to as MMU.
For the sake of efficiency, memory is managed in large blocks or pages, each having usually 4 kilobytes of weight.
In Linux, unlike other operating systems, memory segmentation at the hardware level is not used.
The concept of virtual memory in Linux encompasses both RAM and all SWAP partitions.
Memory process, in turn, can be either resident (actual physical memory used) or virtual (total potential memory available). Usually, rss is smaller than vsz.
SWAP partition is the partition on the drive where rarely used data from resident memory and sometimes all data (in case there is not enough physical memory) are placed. Linux can work with both swap partitions and swap files, allowing data from physical memory to be transferred to a special file on the hard drive. In this file and the swap partition, the same format as RAM is used.
Detailed information on a process’s memory usage is stored in the proc/<pid>/ file.
Virtual memory is composed of pages. These are sets of memory cells in virtual space, that correspond to actual disk memory. Most pages come in the standard size of 4 KB, with some cases where bigger pages (huge pages) are used (2 megabytes or 1 gigabyte). Huge pages are used to handle large data (databases, etc).
Methods of Memory Subsystem Management
Memory management is crucial for system performance in Linux,
Linux's memory allocation methods include bootmem (initial basic allocator), buddy (allocating contiguous page frames), vmalloc (handling non-contiguous physical memory areas), and kmemcache (allocating small objects within page frames). These allocators serve various purposes, from initial booting to handling different memory allocation needs.
NUMA (Non-Uniform Memory Access) in multiprocessor systems is another aspect Linux efficiently manages. It is responsible for optimizing memory access based on the physical proximity of memory to processors, enhancing performance.
User memory management
alloc_pages() and kmalloc() requests lead to immediate memory allocation since the kernel is the most trusted component of the system.
Process address space
Process address space is linear addresses that are accessed by processes.
A process accesses new memory areas through invocations: malloc(), calloc(), mmap(), brk(), shmget() + shmat(), posix_memalign(), mmap() based on void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset).
Memory descriptor
mm_struct, memory descriptor contains all the information on the address space.
Memory area
Memory is divided into two fields – vm_start and vm_end. They stand for the address of the start and of the first bit after the end of the area allocated.
All areas are connected into a two-direction list, where they are ordered in ascending order of addresses.
Linear address interval allocation
Linear addresses allocated are either connected to the file (FILE) or not (ANON), with the process requesting the memory, can share them with other processes or have private access to them.
(MAP_SHARED or MAP_PRIVATE).
Deferred allocation
In some cases, user process requests can be deferred until memory is actually needed.