Deep anatomy ~ FreeRtos reading notes 5 FreeRtos memory management detailed

5 Freertos memory management

The most scarce and precious in chips is often storage resources. In order to make better use of them, developers have to become stingy in allocation and care about every byte.

FreeRtos V8.0.1 provides four schemes for dynamic memory allocation, which are placed in heap1, heap2, heap3, and heap4 files respectively.

 

5.1 Allocation method heap1

The allocation method of heap1 is the simplest, and the amount of code is relatively small. Heap1 can only be applied for and cannot be recycled. It is suitable for projects such as tasks and queues that do not need to be deleted.

The entire block of memory occupied by ucHeap is the heap space that can be applied for, and the size of the space is defined by configTOTAL_HEAP_SIZE.

Before allocating, firstly align the starting address of the heap, which may cause some initial bytes to be discarded as "placeholders", and finally get the effective heap space and use configADJUSTED_HEAP_SIZE instead.

(heap initialization)

 

When Heap1 allocates, use xNextFreeByte to always record the first address that can be allocated in the heap, and the starting value is 0.

If you need to apply for 20 bytes after initialization:

(Apply for 20 bytes)

 

The second application for 30 bytes:

(Apply for 30 bytes)

 

 

5.2 Allocation method heap2

Each memory block in Heap2 has a record linked list and block size corresponding to the structure. The linked list connects free blocks in ascending order of block size. Start and end structures are independent of the heap area as indexes, and end is used as the end identifier xBlockSize is always equal to the total size of the heap area.

After Heap2 initializes the heap area:

(after heap2 initialization)

 

5.2.1 heap2 application memory

If the program needs to apply for three blocks of memory A, B, and C, the required space size is B>A>C. The application allocation diagram using heap2 is as follows:

(Apply for A space)

 

(Apply for space B and C)

 

5.2.2 heap2 reclaims memory

When Heap2 is reclaimed, the memory blocks are inserted into the linked list in ascending order. Assuming that the remaining D space size is equal to A, the program releases the memory of B, A, and C respectively. The heap area after release is as follows:

(after releasing B memory )

 

(after releasing A memory )

 

(after freeing C memory )

The memory on the memory chain increases according to the block size, and the structure that records the block information gradually increases with the allocation and will never be released. Once a program frequently applies for small blocks of memory, it cannot apply for large memory blocks even after it is released.

 

5.3 Allocation method heap3

Heap3 uses malloc and free in the library. At this time, the heap area is out of freertos control, and configTOTAL_HEAP_SIZE becomes an invalid parameter. There is no source code,,,, let's not analyze it~

 

5.4 Allocation method heap4

Each memory block in Heap4 has a record linked list and block size of the corresponding structure. The linked list connects free blocks in ascending address order. The Start structure is independent of the heap area as an index, and the end belongs to a part of the heap area, so removing the size of a structure is the actual allocation range of the heap.

(heap area after heap4 initialization)

 

5.4.1 heap4 application memory

Whenever a free block is requested, a new structure will be generated correspondingly, and these structures will occupy a part of the heap space. The heap area map after applying for memory block A:

(After applying for A memory)

 

Apply for B and C memory blocks again:

(After applying for B memory)

 

(after applying for C memory)

 

5.4.2 heap4 reclaims memory

When heap4 reclaims memory, it will try to merge scattered memory blocks to reduce fragmentation.

Assuming that after applying for A, B, and C, the memory of B needs to be released after use. There is no adjacent free memory above and below the B block, so only inserting the free linked list can complete the release. As shown in the figure:

(after releasing B )

 

When A is released, there is an adjacent B area, the two memory blocks will be spliced, and the structure of the B block will also be merged, as shown in the figure: The green area is the reclaimed memory

(after releasing A )

 

When reclaiming block C, there are free areas up and down, and the reclamation work is divided into two steps:

(release C )

 

5.5 Reading Summary

Same point

Except for heap3, large arrays are used as the heap area for allocation, which seems wasteful. Even if the program does not apply for any space, the memory occupied by the heap area has been determined before the program is compiled.

Both Heap2 and heap4 need to consume a certain amount of heap space for recording memory block information.

The calls are the same, and pvPortMalloc and vPortFree are used uniformly to facilitate the replacement of heap files.

None of the interrupts are masked during heap operations, but the scheduler is suspended. Therefore, FreeRtos heap operations cannot be used in interrupts. If it must be implemented, each heap operation in the thread, such as creating tasks, semaphores, queues, etc., must first shield interrupts, which will reduce the real-time performance of the FreeRtos scheduler.

 

the difference

The four methods differ mainly in the distribution method:

Heap1 allocation is the easiest and fastest, and it is suitable for projects that only apply and do not release.

Heap2 will cause more fragmentation after frequent application and release, and it is suitable for projects that only apply for fixed-size memory.

Heap4 is like an upgraded version of Heap2, the allocation is almost the same, with the addition of memory block splicing during recycling to try to eliminate fragmentation.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325441619&siteId=291194637