doubt with memory when using esp_http_client

jakarman12
Posts: 8
Joined: Fri Oct 30, 2020 6:46 pm

doubt with memory when using esp_http_client

Postby jakarman12 » Wed Jan 27, 2021 8:49 pm

Hello guys,

I have a doubt with the handling of the heap when using esp_http_client.

Every time I make a new request, the heap decreases (I attach the image) after several requests the heap remains at a static value (red box in the image), this last value is what I would expect since I am making the same request with a very similar answer.

What is the reason that when making a new request the heap decreases?

I already checked and I don't have any malloc without free() ... is there a reason for this to happen?

I am relying on the example of esp_http_client... I attach my code

esp-idf: v4.3-dev-2586-g526f68239
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include "freertos/FreeRTOS.h"
  4. #include "freertos/task.h"
  5. #include "esp_log.h"
  6. #include "esp_system.h"
  7. #include "nvs_flash.h"
  8. #include "esp_event.h"
  9. #include "esp_netif.h"
  10. #include "protocol_examples_common.h"
  11. #include "esp_tls.h"
  12. #include "esp_http_client.h"
  13.  
  14. #define MAX_HTTP_RECV_BUFFER 512
  15. #define MAX_HTTP_OUTPUT_BUFFER 2048
  16. static const char *TAG = "HTTP_CLIENT";
  17.  
  18. esp_err_t _http_event_handler(esp_http_client_event_t *evt)
  19. {
  20.     static char *output_buffer;  // Buffer to store response of http request from event handler
  21.     static int output_len;       // Stores number of bytes read
  22.     switch(evt->event_id) {
  23.         case HTTP_EVENT_ERROR:
  24.             ESP_LOGD(TAG, "HTTP_EVENT_ERROR");
  25.             break;
  26.         case HTTP_EVENT_ON_CONNECTED:
  27.             ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED");
  28.             break;
  29.         case HTTP_EVENT_HEADER_SENT:
  30.             ESP_LOGD(TAG, "HTTP_EVENT_HEADER_SENT");
  31.             break;
  32.         case HTTP_EVENT_ON_HEADER:
  33.             ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value);
  34.             break;
  35.         case HTTP_EVENT_ON_DATA:
  36.             ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
  37.             /*
  38.              *  Check for chunked encoding is added as the URL for chunked encoding used in this example returns binary data.
  39.              *  However, event handler can also be used in case chunked encoding is used.
  40.              */
  41.             if (!esp_http_client_is_chunked_response(evt->client)) {
  42.                 // If user_data buffer is configured, copy the response into the buffer
  43.                 if (evt->user_data) {
  44.                     memcpy(evt->user_data + output_len, evt->data, evt->data_len);
  45.                 } else {
  46.                     if (output_buffer == NULL) {
  47.                         output_buffer = (char *) malloc(esp_http_client_get_content_length(evt->client));
  48.                         output_len = 0;
  49.                         if (output_buffer == NULL) {
  50.                             ESP_LOGE(TAG, "Failed to allocate memory for output buffer");
  51.                             return ESP_FAIL;
  52.                         }
  53.                     }
  54.                     memcpy(output_buffer + output_len, evt->data, evt->data_len);
  55.                 }
  56.                 output_len += evt->data_len;
  57.             }
  58.  
  59.             break;
  60.         case HTTP_EVENT_ON_FINISH:
  61.             ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH");
  62.             if (output_buffer != NULL) {
  63.                 // Response is accumulated in output_buffer. Uncomment the below line to print the accumulated response
  64.                 // ESP_LOG_BUFFER_HEX(TAG, output_buffer, output_len);
  65.                 free(output_buffer);
  66.                 output_buffer = NULL;
  67.             }
  68.             output_len = 0;
  69.             break;
  70.         case HTTP_EVENT_DISCONNECTED:
  71.             ESP_LOGI(TAG, "HTTP_EVENT_DISCONNECTED");
  72.             int mbedtls_err = 0;
  73.             esp_err_t err = esp_tls_get_and_clear_last_error(evt->data, &mbedtls_err, NULL);
  74.             if (err != 0) {
  75.                 if (output_buffer != NULL) {
  76.                     free(output_buffer);
  77.                     output_buffer = NULL;
  78.                 }
  79.                 output_len = 0;
  80.                 ESP_LOGI(TAG, "Last esp error code: 0x%x", err);
  81.                 ESP_LOGI(TAG, "Last mbedtls failure: 0x%x", mbedtls_err);
  82.             }
  83.             break;
  84.     }
  85.     return ESP_OK;
  86. }
  87.  
  88. static void http_rest_with_url(void)
  89. {
  90.     char local_response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
  91.  
  92.     esp_http_client_config_t config = {
  93.         .host = "httpbin.org",
  94.         .path = "/get",
  95.         .query = "esp",
  96.         .event_handler = _http_event_handler,
  97.         .user_data = local_response_buffer,        // Pass address of local buffer to get response
  98.         .disable_auto_redirect = true,
  99.     };
  100.     esp_http_client_handle_t client = esp_http_client_init(&config);
  101.  
  102.     // GET
  103.     esp_err_t err = esp_http_client_perform(client);
  104.     if (err == ESP_OK) {
  105.         ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %d",
  106.                 esp_http_client_get_status_code(client),
  107.                 esp_http_client_get_content_length(client));
  108.     } else {
  109.         ESP_LOGE(TAG, "HTTP GET request failed: %s", esp_err_to_name(err));
  110.     }
  111.  
  112.     esp_http_client_cleanup(client);
  113. }
  114.  
  115. static void http_test_task(void *pvParameters)
  116. {
  117.     while (1)
  118.     {
  119.         vTaskDelay(5000 / portTICK_PERIOD_MS);
  120.         http_rest_with_url();
  121.         printf("Heap %d \r\n", esp_get_free_heap_size());
  122.     }
  123.  
  124.     vTaskDelete(NULL);
  125. }
  126.  
  127. void app_main(void)
  128. {
  129.     esp_err_t ret = nvs_flash_init();
  130.     if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
  131.     {
  132.         ESP_ERROR_CHECK(nvs_flash_erase());
  133.         ret = nvs_flash_init();
  134.     }
  135.     ESP_ERROR_CHECK(ret);
  136.     ESP_ERROR_CHECK(esp_netif_init());
  137.     ESP_ERROR_CHECK(esp_event_loop_create_default());
  138.  
  139.     ESP_ERROR_CHECK(example_connect());
  140.     ESP_LOGI(TAG, "Connected to AP, begin http example");
  141.  
  142.     xTaskCreate(&http_test_task, "http_test_task", 8192, NULL, 5, NULL);
  143. }
  144.  
Attachments
memHTTP.png
memHTTP.png (183.74 KiB) Viewed 2986 times

boarchuz
Posts: 606
Joined: Tue Aug 21, 2018 5:28 am

Re: doubt with memory when using esp_http_client

Postby boarchuz » Thu Jan 28, 2021 12:52 am

Don't dwell on it, there's no leak if there's not a consistent pattern of reduction.

See this post: viewtopic.php?t=14056#p55646

And also consider that there are other tasks doing work behind the scenes - especially WiFi/lwip in this case, which will be dynamically allocating/deallocating buffers as required.

Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot] and 102 guests