Application Layer memory management for memory-constrained devices (I)

Source: Internet
Author: User

Application Layer memory management for memory-constrained devices (I)

Raveendran vadakkoot and neeraj S. Sharma

Translated by Guo shilong

 

1. Introduction

Dynamic memory allocation has always played an important role in software development. Fact system applications frequently allocate and recycle memory, which seriously affects the generation of performance and fragmentation. If applications in the real-time system can manage their own memory, they can achieve better performance and memory management. This article discusses the dynamic memory allocation technology for a faster and better real-time system.

 

2. Application-level Memory Management Technology

Before in-depth discussion on the development of the C ++ framework for memory management, we will first introduce the background knowledge of the two common memory management technologies, which are very popular in the development community.

Clear list (explicit lists)

Segregated lists)

The C ++ framework discussed here uses the separation List mechanism.

 

2a. Clear list (explicit lists)

The clear list generally has the following link area:

Single-chain table or double-chain table

The linked list can also be a ring

Figure 1 shows how continuous heap areas are connected to a single-chain table. The allocated unit usually has a head to store the starting position of the unit, followed by the actual dynamic data. The memory management system maintains a pointer to the header, A freelist ). Once you get a malloc () request with an object size of X (in the case of C/C ++), the system will traverse the free linked list (freelist) find the memory block with the size of Y, assuming y> = x, this object is suitable for storage. The problem is that Y is much larger than X. A typical memory management system takes the most appropriate policy to minimize the value of the Y-X to reduce memory fragmentation. Users may also prefer the "First fit" strategy.

Figure 1 clear chain structure

 

Advantages

1. Easy bookkeeping

2. Bookkeeping consumes less memory

 

Disadvantages

1. Slow allocation and recovery, unpredictable time required

2. Difficult Unit Connection

3. Chain links may occur due to improper management.

4. difficult overall Algorithm

 

2b separated linked list

Separated linked lists are another mechanism in which memory blocks of special sizes have different idle linked lists.

In this case, there are:

Memory blocks of different sizes have different idle linked lists

A typical two-way linked list

The link must start and end

The link sequence does not need to be the same as the memory block.

 

 

Figure 2 separated linked list Structure

 

Advantages

1. Fewer fragments

2. Fast allocation and recovery

3. Easy Unit Connection

4. Easy to locate idle Blocks

 

Disadvantages

1. Difficult bookkeeping

2. Difficult allocation

 

The C ++ framework discussed here uses the separated linked list mechanism.

 

3. Separate linked lists

Any type of memory management starts with a large block of continuous memory and an idle memory card. This idle continuous memory block must be initialized first.

 

 Initialization

In the initial stage, the idle block is called the idle linked list as shown in the following figure.

 

Figure 3 Idle memory block (Memory Pool)

 

This bulk is divided into small blocks. All blocks are of the same size, for example, m units or pages.

 

Figure 4 Memory split page

 

Objects smaller than or equal to M/2 are placed in the same unit/page. That is to say, a page with the size of M can hold 2 objects with the maximum size of M/2 and 4 objects with the maximum size of M/4. To identify idle blocks/units in a unit, a bitmap is required. The unit used is marked as 1, and the unused unit is marked as 0. This is easy to see from Figure 5 below.

 

Figure 5 bitmap indicating idle or allocated memory blocks

 

In Figure 5, the shadow represents the used unit, and the white represents the unused unit. The size of each block in a unit is M/4. Inner units are not strictly limited to objects with a size of M/4. The unit with a size of M/4 can store objects with a size of X. Here, M/8 <x <= m/4, this makes it a complete binary sequence.

 

Speed and response time are important aspects of any fact system. This also means that memory management should be fast in terms of timeliness. The power algorithm of 2 is most suitable for quick efficiency. The Application Layer memory management discussed here also uses the power 2 technology to achieve better efficiency.

 

3A. Separation list and binary algorithm (Buddy System)

In the algorithm discussed here, all memory allocations are based on objects, and the object size falls within the power of 2. For example, if the size of the allocated object is 35 bytes, the object will be in a category greater than 32 (2 ^ 5) and smaller than or equal to 64 (2 ^ 6.

Each page contains an object of a category, which groups objects into different categories. That is to say, an object with the size of 'M' is allocated in page p, So that I <m <= J (j = I + 1 ), here, I and j are the power of 2, and the size of all objects on the page is P. Here P = 2 ^ J.

For example, if the allocation request is a 17-Byte object X, for this specific allocation request, the above equation is changed to 16 <17 <= 32, here I and j are 4 and 5 power of 2, and m is 17. all objects in this category have 32 bytes, whether or not 32 bytes are required. All objects with a size of 16 <x <= 32 bytes are stored in pages where X is stored. All object allocation requests whose sizes do not fall into this range will go to different pages.

Objects falling into the same category are linked together with separated linked lists, as shown in figure 6.

 

Figure 6: separated chain Data Structure

 

There is a head pointer pointing to the first page of the category linked list, and a tail "tail" pointer pointing to the last page of the category linked list.

(To be continued ...)

Application Layer memory management for memory-constrained devices (2)

Application Layer memory management for memory-constrained devices (III)

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.