How to choose kmalloc and vmalloc

insert image description here
Both kmalloc and vmalloc are functions used in the Linux kernel for memory allocation, but they are suitable for different memory allocation scenarios.

The kmalloc function is used to allocate small blocks (usually less than a page size) of contiguous memory areas in kernel space, which can be used to store kernel data structures and buffers, etc. kmalloc memory is allocated by the buddy algorithm, so it has low memory allocation and deallocation overhead, but the memory size is limited.

Instead, the vmalloc function is used to allocate large blocks of virtual memory regions in kernel space, typically used to store large buffers and memory-mapped files, etc. Since the memory allocated by vmalloc is not physically contiguous, its performance overhead is relatively high. However, vmalloc can allocate very large memory regions, and kmalloc cannot meet this requirement.

Therefore, in kernel programming, it is necessary to select a suitable function according to specific memory allocation requirements. If you need to allocate a small block of contiguous memory, you can use the kmalloc function; if you need to allocate a large block of non-contiguous virtual memory, you need to use the vmalloc function.

[The last bug] There are updates and releases on multiple platforms. You can connect three times with one click, follow + star, and don't miss exciting content~insert image description here

Guess you like

Origin blog.csdn.net/qq_33471732/article/details/131670564