Optimizing the Memory Manager Settings

Last modified by Microchip on 2024/09/26 09:48

Introduction

The Microchip Graphics Suite (MGS) Harmony Library allocates memory from preconfigured memory pools. These memory pools are used for dynamic memory allocations to create screens and widgets, as well as by the render to keep track of dirty sections of the frame.

This guide provides information on how to configure these memory pools for optimal application memory footprint and performance.

Memory Manager

The MGS Memory Manager manages the memory allocations made by the library and by the application that uses the MGS library and widget APIs.

This memory management system is used by the MGS Harmony Library only. It is not related to the general allocator and all of its memory is statically allocated. The general heap is still managed from within the MPLAB® Code Configurator (MCC) or in the MPLAB X IDE project.

These memory pools are split into two types:

  • Fixed Heap
  • Variable Heap

Fixed Heap

The Fixed Pool heap offers fast, fragment-free dynamic allocation. One of its drawbacks is that it can sometimes lack efficiency and can consume more memory than is necessary for a given allocation. The total size of the Fixed Pool is shown in the Memory section of the Project Settings window of Legato™ Composer. The defaults shown were derived based on testing of various Legato demo graphics applications.

Variable Heap

The Variable Heap is a single, often larger, memory pool. The benefit of the variable heap is that it can allocate chunks of any size. However, it can become fragmented with use.

The Variable Heap can be configured to allocate using a Best Fit or First Fit mode. The Best Fit mode may take longer to allocate a new chunk but works to utilize the heap as efficiently as possible, reducing fragmentation. First Fit mode takes the opposite approach and allocates the first available block it finds, perhaps resulting in a block split.

Legato uses the Fixed Heap for smaller allocations to reduce overall memory fragmentation and reserves the Variable Heap for larger allocations.

Memory Manager Settings

Memory Settings in the composer project settings provide users with the options to configure the size of the memory pools used by the Memory Manager for these allocations.

Memory manager Settings

These settings are generated into legato_config.h and are used in legato_memory.c.

Optimizing Heap Memory Settings

It is important that the sizes of the heap memories are properly configured. Otherwise, the library may be reserving more memory than it needs, or it doesn't have enough memory to render the screens properly.

To do this, it is good to know what's the maximum amount of allocations (high-watermark) the library uses for each heap memory. This max usage information is saved by the library in the fixed and variable heap data structures. 

fixedHeaps[idx].maxUsage //usage watermark value (count)

variableHeap.maxUsage //the highest amount of usage the heap has seen (bytes)

A summary of the memory usage information can be printed out of the serial console by calling the leMemoryPrintReport() function.

The output would be similar to the the accompanying image, where information about the Max Usage for each heap memory is shown.

Memory usage print output

To make sure that the heap memory size is optimized for the application, first run your application through all the screens and UI use cases to gather the max usage for the entire life cycle of the application. Then, call the function leMemoryPrintReport() to determine the Max Usage for each heap memory.

Set the size of the Variable Heap and Fixed Heap memory pools in the Memory Manager in MGS Harmony Composer using the Max Usage values, plus some reserve capacity.

Back to Top