12. Application of memory space

Source: Internet
Author: User

1. Application of user space memory

The request function for user space memory is malloc, and the corresponding memory deallocation function is free. The malloc function has a bit of memory power

(1) two times the ability to manage

When the user space malloc a piece of memory, the application of the memory is immediately free, and will not immediately return the memory to the kernel, but the memory back to the C library, this memory is still part of the process, but the memory to the C library to manage. Therefore, subsequent memory requests can occupy this freed memory.

(2) The Linux kernel is always on demand

When malloc succeeds, it returns. Although it is a successful return, but the kernel does not actually give the memory to the process, this time, if read, you will find that the content is all zero, and this memory is read-only. When writing this memory, the kernel will actually give the memory to this process when the page error occurs.

2, kernel space dynamic request memory
2.1, Kmalloc

void * KMALLOC (size, flags)

Size, which allocates memory sizes. Flags, the flags of distribution, are generally more commonly used as gfp_kernel,gfp_aromic.

Kmalloc application memory, located in the DMA mapping area, most importantly, physically, is also contiguous, and they are only a fixed bargain with real physical addresses. This is critical for DMA.

Kmalloc allocates the size of the memory space, which is generally small compared to the size of 128kb-16b.

When the flags are Gfp_kernel, if the request for memory space is not met, the process will sleep and cause blocking. Therefore, if the flag is gfp_kernel, it cannot appear in the context of interrupts, spin locks, or atomic operations. Therefore, in the context of the interruption, Tasklet, the kernel of the timer, etc. can not appear in the process switch location, you can only use Gfp_atomic to apply for memory, if not at this time, do not wait for sleep, directly to return. 、

Use Kfree to release the requested memory space.

2.2, Vmalloc

Vmalloc uses similar methods, but Vmalloc is generally used for applications with large memory space, and vmalloc the requested memory space is contiguous only in virtual memory space, but in essence, the physical memory space is not necessarily contiguous. Therefore, the requested virtual address space and the actual physical address space do not have a fixed offset relationship.

Vmalloc internal implementation, is the use of the GFP_KERNEL flag Kmalloc to achieve, so, Vmalloc is also not used in the context of atoms.

In the same vein, use Vfree to release memory.

12. Application of memory space

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.