Suspicious Memory leak when using esp-mqtt

zilizii
Posts: 15
Joined: Sun Oct 29, 2017 2:19 pm

Suspicious Memory leak when using esp-mqtt

Postby zilizii » Tue Jan 22, 2019 9:51 pm

Hello,

I tries to make a small Home Automation project with a Nextion display. I use Eclipse and C++ with the ESP-IDF together.

I made the first single test with the following code:

Code: Select all

void mqtt_app_start(void * parameters)
{
    esp_mqtt_client_config_t mqtt_cfg = {
    		.event_handle = mqtt_event_handler,
    		.host = "192.168.1.140",
			.uri = "mqtt://192.168.1.140:1883",
			.port = 1883,
			.client_id = "NextionMQTTBridge",
			.username = mqtt_user,
			.password = mqtt_password,
    };

    ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size());

    esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
    xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false,true, 5000 / portTICK_PERIOD_MS);
    esp_mqtt_client_start(client);

    while(1){
    	vTaskDelay(5000 / portTICK_PERIOD_MS);
    	xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false,true, 5000 / portTICK_PERIOD_MS);
    	esp_mqtt_client_publish(client,"test/NextionMQTTBridge","Hello World",10,0,0);
    	ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size());
    }
    vTaskDelete(NULL);
}
This is called via following way:

Code: Select all

void app_main(void) {
       nvs_flash_init();
       wifi_init();
       xTaskCreate(&mqtt_app_start, "MQTT", 2000,NULL,5,NULL );
       while(1) {
       		vTaskDelay(1000 / portTICK_PERIOD_MS);
       	}
}
I checked the log file and for me seems a continusly decreasing trend....
Started:
I (12367) NextionMQTTBridge: [APP] Free memory: 213752 bytes
I (2477367) NextionMQTTBridge: [APP] Free memory: 213248 bytes

Where I made the mistake? Thank you very much in advance!
Trial - Error scenario for learning ESP32 :D

zilizii
Posts: 15
Joined: Sun Oct 29, 2017 2:19 pm

Re: Suspicious Memory leak when using esp-mqtt

Postby zilizii » Wed Jan 23, 2019 8:38 pm

After a day running seems oscillating

currently the low point the low (82292367) NextionMQTTBridge: [APP] Free memory: 213064 bytes
which is far way from the top:
I (84432367) NextionMQTTBridge: [APP] Free memory: 214876 bytes

And the same "static" code running. I do not except this difference
Trial - Error scenario for learning ESP32 :D

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Suspicious Memory leak when using esp-mqtt

Postby ESP_Angus » Thu Jan 24, 2019 1:44 am

Because both the TCP/IP stack and the WiFi stack allocate from heap, you can expect some variance in memory usage during network activity. Something in the order of ~1800 bytes between min and max is likely to be expected. If there's no constant decreasing trend then there is no memory leak as such.

Some information about memory leak false positives can be found here:
https://docs.espressif.com/projects/esp ... mory-leaks

To stabilise heap usage from the Wi-Fi stack, there are some configuration items you can choose: set a high number of Static RX buffers, set the number of Dynamic RX buffers to 1, and set TX buffers to static. This will decrease the amount of variability in heap usage, however it will also increase the overall (constant) heap usage. We don't really recommend this unless you plan to push right to the limit of minimum available heap, and can't risk some small allocations.

Unfortunately no similar option exists for TCP/IP.

zilizii
Posts: 15
Joined: Sun Oct 29, 2017 2:19 pm

Re: Suspicious Memory leak when using esp-mqtt

Postby zilizii » Thu Jan 24, 2019 6:04 pm

Thank you for your explanation, good to know. I will read those documents to increase my knowledge.
Trial - Error scenario for learning ESP32 :D

Who is online

Users browsing this forum: No registered users and 8 guests