Page 1 of 1

Suspicious Memory leak when using esp-mqtt

Posted: Tue Jan 22, 2019 9:51 pm
by zilizii
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!

Re: Suspicious Memory leak when using esp-mqtt

Posted: Wed Jan 23, 2019 8:38 pm
by zilizii
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

Re: Suspicious Memory leak when using esp-mqtt

Posted: Thu Jan 24, 2019 1:44 am
by ESP_Angus
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.

Re: Suspicious Memory leak when using esp-mqtt

Posted: Thu Jan 24, 2019 6:04 pm
by zilizii
Thank you for your explanation, good to know. I will read those documents to increase my knowledge.