Memory fragmentation causes and ultimate Solutions _s algorithm

Source: Internet
Author: User
Memory fragmentation is usually divided into internal fragments and external fragments:
1. Internal fragmentation is due to the use of a fixed sized memory partition, which produces internal fragmentation when a process does not fully use the fixed memory area that is assigned to it, which is usually difficult to avoid entirely;
2. External fragmentation is due to the fact that some unallocated contiguous areas of memory are too small to meet the memory allocation requests of any process and thus cannot be exploited by processes.

The most commonly used segment-page memory allocation is to divide the memory area of the process into segments, and then make each section composed of several fixed-size pages. The page table mechanism makes it possible for pages within a segment to not be contiguous in the same memory area, thereby reducing external fragmentation, but there may still be a small amount of internal fragmentation on the same page, but only one page of memory space is smaller, making possible internal fragmentation less.


Workaround: First, using the most original tag allocation method, the system needs to maintain a simple memory information table:

When the program requests a 3-length memory space:

When the program requests a 2-length, and 4-length memory space:

At this point, only 1 free space remains. If the program then to apply for longer than 1 of the space, it can not apply, that is, not enough memory. Now, release the id=2 space:

We found that the available memory space is now 3, but these 3 free spaces are not contiguous. Therefore, if the program now applies to 3 of the memory space, the same will not apply, there will be insufficient memory. This is what the industry calls "memory fragmentation."

Obviously the remaining 3 space, but not 3 memory space application, this TMD ripped eggs.

Engineers, then, invented a page-based memory management approach:

First, the physical memory, according to a certain size, is divided evenly. For example, I now use 2 memory units to split the memory, that is, every two consecutive memory space, composed of a page of memory:
Next, the system also needs to maintain a memory information table:

The program now requests a 3-length memory space, but since the smallest unit currently requested is a page, and a page is 2, you now need to apply for 2 pages, which is 4 memory space. You see, this is a waste of 1 memory spaces.

The program then applies for a 1-length, 2-Length space:


Release the memory space information for the id=2, memory page ID 3:

Now, there is a previous scenario: There are currently 4 memory spaces, but not continuous. However, because it is now a paging management mechanism, you can still continue to apply for a 4-length memory space:


This is a much better plan. Without debris, you can use up as much space as possible. But think about it, and it's a huge price to pay for this advantage.

The previous memory allocation method, although vulnerable to fragmentation, and low utilization of memory space, but with high performance, the program can get the memory address directly from the Memory information table, then you can use the memory space directly according to the address.

But the following way of paging, the program needs to record the memory page ID, each time you use, you need to translate from the memory page ID to the actual memory address, one more conversion. And this mode, will waste some memory, such as the above application for 3 memory space, the actual allocation of 2 pages a total of 4 memory space, wasted 1 memory space.

The above is the basic principle, the actual system will do a lot of optimization. The current mainstream operating systems are paging, so you don't need to be too concerned about fragmentation. This topic extends further, is a local memory pool within the program. But this is another question, if you like, you can delve into it.

Source: Know Links: https://www.zhihu.com/question/51836333/answer/145693402

Turn from: https://blog.csdn.net/tong5956/article/details/74937178

Related Article

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.