Embedded operating system kernel principle and development (Memory Allocation Algorithm)

Source: Internet
Author: User

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

Memory Allocation is an essential part of the operating system. Unless the system itself does not require memory scheduling, all services can be done through global data and stacks. Memory allocation is not difficult, but the extended memory is complicated. There was no MMU earlier, and there was no priority between the system space and user space. Therefore, the memory of different programs is shared, and mutual influence is inevitable. Therefore, in general, in addition to memory allocation, we also need some log information and detection information to help us debug and analyze. Of course, these are not our concerns. We are concerned about the general memory allocation algorithms.

 

(1) fixed Memory Allocation

The fixed memory allocation algorithm is the simplest and best understood algorithm. For example, if we have 16 MB of memory and assume that the basic memory allocated is 4 kb, there are 16 Mb/4 K = 4 K units in total. Therefore, if you want to apply for memory, it can be up to 4 K times. If you want more memory, the system will allocate the adjacent memory to you.

 

(2) linked list Memory Allocation

Although the fixed memory allocation is good, there is another drawback, that is, there are a lot of waste opportunities. Imagine that if a user only needs dozens of bytes, 4 K bytes will be allocated to the user, and the waste of space exceeds 99%. Based on this, we propose the linked list memory algorithm. The linked list algorithm stores idle nodes. When the memory is released, the idle nodes are found in the memory. The nodes are merged and released. Of course, if you want to apply for memory, there are more ways to apply, the worst application, the best application, and the best application.

 

(3) partner Algorithms

Compared with the fixed memory algorithm, the linked list algorithm can save a lot of memory. However, the linked list algorithm has a characteristic that memory fragments are easily formed. Therefore, we can combine the features of the fixed allocation and linked list algorithms to allocate memory to several linked lists of sizes 8, 16, 32, 64, 128, 256, and 512. The size inside the linked list is the same, and the relationship between linked lists is a multiple. When allocating memory, we first look for the most suitable linked list and then allocate memory. If the memory space is not enough, we can apply for a high-level memory linked list, in this way, the disassembled memory can be allocated to low-level linked lists. When releasing the memory, we should also pay attention to the memory merging and combination.

 

(4) Memory Pool-based partner Algorithm

The partner algorithm is good, but if a memory application is very frequent, repeated splitting and merging are required in the partner algorithm. On the one hand, this will affect the memory allocation efficiency, and on the other hand, it will easily cause memory allocation fragmentation. Therefore, we can build a memory pool based on the partner algorithm. When the memory is released, it only indicates that the current memory is no longer used, but it is not actually released, when all the memory in the memory pool is no longer used, it will be released, which will improve the memory allocation efficiency to a certain extent. Especially after the system runs for a period of time, this effect is particularly obvious.

 

 (5) Working Set Algorithm

A working set algorithm is essentially not an algorithm, but a basic idea. We know that after the system is stable, the size of memory allocation and the ratio of configuration are relatively fixed, and the change is not very large. If we can record the data and pre-allocate the memory when the system starts, will it increase the system startup speed? Of course, the parameter setting in the working set is more of an experience value. It requires us to analyze various factors and compare them repeatedly to get better results.

 

These five algorithms only provide the basic idea. They can be learned only by practice and practice.

 

 

 

 

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.