Memory Management algorithm--buddy partner algorithm

Source: Internet
Author: User

Advantages and disadvantages of the buddy algorithm:

1) Although the partner memory algorithm has done quite well on the memory fragmentation issue, but in this algorithm, a very small block will often hinder a large chunk of the merger, a system, the allocation of memory blocks, the size is random, a piece of memory only a small block of memory is not released, next to the two large can not be merged.

2) There is a certain waste in the algorithm, the partner algorithm is the power of 2 to allocate memory block, of course, this is a reason, that is, in order to avoid the large block of memory is too broken, more important is to make the allocation and release process quickly. But he also brought a disadvantage, if the required memory size is not 2 power, there will be some of the page wasted. Sometimes it's serious. For example, the original is 1024 blocks, applied for 16 blocks, and then apply for 600 blocks on the application is not, because has been divided.

3) Additional splits and merges involve more linked lists and bitmap operations, and the overhead is relatively large.

Buddy (definition of partner):

This gives the concept of a partner, which meets the following three criteria called a partner:
1) Two blocks of the same size;
2) two block address continuous;
3) Two blocks must be separated from the same chunk;

The allocation principle of the buddy algorithm:

If the system needs 4 (2*2) page-size memory blocks, the algorithm will be found in the free_area[2], if there are free blocks in the list, it is directly removed from and allocated. If not, the algorithm will follow the array up to find free_area[3], if there is free block in free_area[3], then it is removed from the linked list, divided into two parts of equal size, the first four pages as a block inserted free_area[2], the last 4 pages allocated, Free_ AREA[3] also did not, and then look up, if free_area[4] there, will be 16 (2*2*2*2) pages, etc. into two parts, the first half of the linked list such as Free_area[3], the second half of the 8 pages divided into two equal, the first half hung free_area[ 2]
In the linked list, the latter half is allocated. If FREE_AREA[4] is not there, repeat the process above, knowing that the Free_area array is reached at the end, and if not, discard the assignment.



The release principle of the Buddy algorithm:

The release of memory is the inverse of the allocation process and can also be seen as the merging process of the partner. When releasing a block, first in its corresponding linked list to check whether a partner exists, if there is no partner block, the block to be released directly into the linked list, if any, then take off from the list of partners, combined into a large chunk, and then continue to investigate the merged block in the larger list of whether there are partners exist, Until you cannot merge or have merged to the largest block (2*2*2*2*2*2*2*2*2 pages).


Throughout the process, the bitmap plays an important role, as shown in 2, one of the bitmaps corresponds to two mutual partners of the block, 1 means that one of the pieces has been allocated, for 0 means both are idle. The partner either allocates or releases is only relative to the bitmap for the XOR operation. When allocating memory to a bitmap
is to release the process service, the release process depends on the bitmap to determine whether the partner exists, if the corresponding bit XOR 1, then no partner can be merged, if the XOR or operation is 0, merge, and continue to merge the partners in this way until it cannot be merged.

Reference documents:

http://blog.csdn.net/orange_os/article/details/7392986

http://blog.csdn.net/zhongnanjun_3/article/details/21799209

http://www.oschina.net/code/snippet_180916_7598


Memory Management algorithm--buddy partner algorithm

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.