[Linux Memory]linux Memory learning--Frequently Asked questions

Source: Internet
Author: User

The difference between the 1,kmalloc () function and the Vmalloc () function:

The memory allocated by the Kmalloc () function is physically contiguous, whereas the memory allocated by the Vmalloc () function is only a continuous virtual address, and normal kernel programming usually uses the Kmalloc (), which is primarily in performance consideration because Vmalloc () Converting a physically discontinuous page into a contiguous page on a virtual address space must be specifically set up for page table entries, and Vmalloc () is used only when large chunks of memory are needed, typically when the module is dynamically inserted into the kernel. Many other hardware devices require a contiguous page of physical addresses, because many hardware devices exist outside of the memory Management Unit (MMU). In addition Vmalloc () function may sleep, can not be used in the middle end of the context, while Kmalloc plus gfp_atomic can be guaranteed to be used in places where sleep is not possible.

If you want your code to be very portable on different platforms, you should not allocate more than 128K of memory.

Kmalloc corresponds to Kfree, the allocated memory is between 3gb~high_memory, this kernel space corresponds to the mapping of physical memory one by one, can allocate contiguous physical memory, Vmalloc corresponds to Vfree, allocated memory in VMALLOC_ START~4GB, allocating contiguous virtual memory, but not necessarily physically contiguous, vmalloc () allocated for high-end memory ~

2. How to prevent memory fragmentation caused by frequent malloc () free () operations

Answer: Use memory pool technology.

Memory pool technology first allocates a large chunk of memory to the program, when the program needs to allocate memory from the memory pool to obtain, and do not need to release memory, when the memory pool is not in use when the entire memory can be allocated only, not released, greatly reducing the time.

How to build a memory pool

3, whether the kernel space can access the memory of the user space

A: Yes, such as Copy_to_user () and Copy_from_user () two functions,

[CPP]View Plaincopy
    1. Unsigned long copy_to_user (void __user *to,const void *from,unsigned Long Count)

The parameter to is to represent the user space pointer, the kernel space to access user space content, must use the above interface, because in Linux, the user memory and kernel memory is independent, in the respective address space implementation, the above function will do in the implementation of memory conversion and user space address check. The realization of Copy_from_user

[Linux Memory]linux Memory learning--Frequently Asked questions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.