How to calculate allocated memory in lvgl

Hi @kisvegabor , @embeddedt


I try to know how many heap memories are used for a screen.
Could you teach me how to get this information?

What LVGL version are you using?

7.4.1 and 7.10.0

Code to reproduce

For ex, I want to know sum of memory use for this screen

//Write codes screen
ui->screen = lv_obj_create(NULL, NULL);

//Write codes screen_label_1
ui->screen_label_1 = lv_label_create(ui->screen, NULL);
lv_label_set_text(ui->screen_label_1, "12");
lv_label_set_long_mode(ui->screen_label_1, LV_LABEL_LONG_BREAK);
lv_label_set_align(ui->screen_label_1, LV_LABEL_ALIGN_LEFT);

//Write style LV_LABEL_PART_MAIN for screen_label_1
static lv_style_t style_screen_label_1_main;

//Write style state: LV_STATE_DEFAULT for style_screen_label_1_main
lv_style_set_radius(&style_screen_label_1_main, LV_STATE_DEFAULT, 0);
lv_style_set_bg_color(&style_screen_label_1_main, LV_STATE_DEFAULT, lv_color_make(0x31, 0xb4, 0x3a));
lv_style_set_bg_grad_color(&style_screen_label_1_main, LV_STATE_DEFAULT, lv_color_make(0x56, 0xb8, 0x32));
lv_style_set_bg_grad_dir(&style_screen_label_1_main, LV_STATE_DEFAULT, LV_GRAD_DIR_VER);
lv_style_set_bg_opa(&style_screen_label_1_main, LV_STATE_DEFAULT, 255);
lv_style_set_text_color(&style_screen_label_1_main, LV_STATE_DEFAULT, lv_color_make(0x00, 0x00, 0x00));
lv_style_set_text_font(&style_screen_label_1_main, LV_STATE_DEFAULT, &lv_font_simsun_48);
lv_style_set_text_letter_space(&style_screen_label_1_main, LV_STATE_DEFAULT, 2);
lv_style_set_pad_left(&style_screen_label_1_main, LV_STATE_DEFAULT, 0);
lv_style_set_pad_right(&style_screen_label_1_main, LV_STATE_DEFAULT, 0);
lv_style_set_pad_top(&style_screen_label_1_main, LV_STATE_DEFAULT, 0);
lv_style_set_pad_bottom(&style_screen_label_1_main, LV_STATE_DEFAULT, 0);
lv_obj_add_style(ui->screen_label_1, LV_LABEL_PART_MAIN, &style_screen_label_1_main);
lv_obj_set_pos(ui->screen_label_1, 83, 50);
lv_obj_set_size(ui->screen_label_1, 60, 0);

The simplest solution is to measure the memory usage immediately before and after you create the screen, and then take the difference.

    lv_mem_monitor_t mon1, mon2;
    uint32_t mem_used = mon1.free_size - mon2.free_size;

Note that the lv_mem_monitor API is only available if you are using the built-in heap implementation. If you are using a custom heap, you will need to find its equivalent API and use that instead. It’s the same approach in both cases, though.

This is not an exact measurement of the total memory that will be needed (since some will be allocated for events, rendering, etc.), however it should give you a rough estimate.

1 Like

Hi @embeddedt

Thanks your reply soon.
If LV_MEM_CUSTOM == 0, It’s mean I am using using the built-in heap implementation, is that right?

That’s correct.

Thank you very very much !!

is the mem monitoring in the simualtor the same as for a µC project?

Unfortunately, the raw numbers usually won’t match.

  • The simulator is 64-bit, but the target device is usually 32-bit. Because LVGL stores a lot of pointers on the heap, this will almost double the memory usage on the simulator (an approximation, it probably won’t be quite double).
  • If the simulator’s screen size is not identical to the target device, the simulator will need more memory for buffers.

However, relative comparisons (e.g. how much more memory does a list with 100 items use v.s. a list with 50) should give very similar results on both.