Memory Fragmentation and companion algorithm _c tips

Source: Internet
Author: User
Today I learned about the Linux memory allocation problem, some do not understand, what is the memory fragmentation problem. And why a function such as maloc () frees resources with free () each time it allocates memory, and why it can cause fragmentation problems. How the memory fragmentation problem arises and how to solve it.
The following is your own learning experience today:
Memory Fragmentation Concept:
The memory fragmentation problem is divided into internal and external fragments.
1. Internal fragmentation is due to the use of a fixed size of the memory partition, when a process can not fully use the fixed area of memory allocated to it, the remainder of the process after the allocation of the part known as internal fragmentation. Usually internal fragments are unavoidable;
2. External fragmentation is due to the fact that some unallocated contiguous areas are too small to be sufficient for the memory block size of the memory resource allocated by any process, which is said to be an external fragment of the memory block size


Functions such as malloc () are released each time the memory is completed, but each time the memory is freed and the release time is different, memory fragmentation occurs. For example, a total of 100 1 bytes of space was applied between the starting address of memory unit 100 and the memory unit 200. When free (), a memory unit (such as 101,103,105 ...) with an odd memory address is freed. While even units are not freed, 50 1 bytes of space are freed, although the total space is 50 bytes, but because these 50 1 bytes of space are not contiguous. The next time you want to request a 2-byte memory unit, you will not be able to request space in the 100 to 200 memory address unit, resulting in a memory fragmentation problem.


Why memory fragmentation is generated.
The allocation methods for memory are: Continuous address assignment, paging mechanism and segmentation mechanism, as well as segment page (there are many articles about memory address assignment on the Internet, it is very good to understand)
Continuous address assignment: Fixed partition allocation produces internal fragmentation problems, and dynamic partitioning creates external fragmentation problems

Paging mechanism: The internal fragmentation problem has been significantly reduced compared to the fixed partitioning partition

Fragmentation mechanism: eliminates the internal fragmentation problem, but creates an external fragmentation problem



The companion algorithm can solve the problem of outer fragmentation, and its algorithm thought is as follows:

Whether an allocated partition or an idle partition has a K power of 2, K is an integer, 1<=k<=m, where 2^1 represents the minimum partition size allocated, and 2^m represents the maximum partition size allocated. When the system starts running, the entire memory area is an idle partition of size 2^m, with the system running, the continuous division of the free area will form a number of discontinuous free partitions, these free partitions according to the size of the partition, for each class with the same size of all free partitions, set up a separate free partition two-way list. As a result, free partitions of different sizes form a K (0<=k<=m) free partition list.

When you need to allocate a storage space of length n for a process, first compute the value of I, so that 2^ (i-1) < n < 2^ I are found in the Free zone list of the free partition size I. If found, the free partition is assigned to the process. Otherwise, an idle partition with a length of 2^ I is depleted, and the free partitioned list in length 2^ (i+1) is found. If there is an idle partition with a size of 2^ (i+1), the partition is divided into blocks that are all 2^ I, one block is allocated to the process, and one block is mounted in the free partition list with length size 2^ i. If an idle partition of size 2^ (i+1) is exhausted, to find an idle partition of size 2^ (i+2), if found, divide the partition two times, for the first time, the partition size 2^ (i+2) is divided into two free partitions of 2^ (I+1), one mounted at 2^ (i+ 1 on the free partition, a partition that is split again into two size 2^ I, one is mounted on an idle partition of size 2^ I, one for allocation to the process, or for an idle partition with a size of 2^ (i+2), which is found on the free partition of 2^ (i+3), one repeat of the steps above, Guide to finding the free zone


In Linux, this memory is divided into 10 free partition list, 0-9, size range is: 2^0-2^9


The inverse process of the above process is the release process of the partner algorithm, the release process needs to meet two conditions: 1. Two blocks have the same size 2. Their physical address is continuous.

It is based on the above two conditions to call this algorithm as a partner algorithm



Data structures involved in the Linux partner algorithm:

free_area_t Free_area[max_order];

We again give a more detailed description of free_area_t.

#difine Max_order 10

Type struct Free_area_struct {

struct List_head free_list

unsigned int *map

} free_area_t


The List_head domain is a universal bidirectional linked list structure, and the type of the elements in the linked list will be mem_map_t (that is, the struct page structure). The map field points to a bitmap, depending on the number of pages that are available. Free_area each bit of the K-item bitmap describes the state of the two-partner blocks of page size 2k. If a bitmap has a bit of 0 that means either a sibling block or two are free, or two are allocated, if 1, there must be a piece assigned. When the brothers are idle, the kernel treats them as a single 2k+1 size


Turn from: https://blog.csdn.net/man_lady_horse/article/details/47169137

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.