Need Help regarding Netconn Memory Leak Issue

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Need Help regarding Netconn Memory Leak Issue

Postby WiFive » Tue Nov 28, 2017 7:29 am

pratik.yadav.455 wrote:
When I send request second time my free heap size is become 222940,

when I send request 3rd time my free heap size is become 222992.
Looks like free heap went UP. Does it really go all the way to zero over time?

If you are writing server from scratch why are you using netconn instead of socket api?

Ritesh
Posts: 1383
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: Need Help regarding Netconn Memory Leak Issue

Postby Ritesh » Tue Nov 28, 2017 8:29 am

WiFive wrote:
pratik.yadav.455 wrote:
When I send request second time my free heap size is become 222940,

when I send request 3rd time my free heap size is become 222992.
Looks like free heap went UP. Does it really go all the way to zero over time?

If you are writing server from scratch why are you using netconn instead of socket api?
Hi,

I think Pratik has not provided full logs in which we have seen memory leak on each request like first, second and third request. So, let me check and will provide you updated logs in which you can see that Heap Memory is continuously going to decrease.

Right Now, We have checked it using netconn library as we have started from base example provided into examples folder or on GitHub.

Let me check with socket API and will get back to you with updated result with that.

But I just want to confirm that in which ESP32 IDF release that netconn issue will be resolved like we are facing right now.
Regards,
Ritesh Prajapati

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Need Help regarding Netconn Memory Leak Issue

Postby WiFive » Tue Nov 28, 2017 10:33 am

I don't think any idf examples use netconn api

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

Re: Need Help regarding Netconn Memory Leak Issue

Postby ESP_Angus » Wed Nov 29, 2017 12:50 am

Ritesh wrote: I think Pratik has provided code and few debug informations regarding this issue when he created issue. So, Please let me know if need more informations regarding that so that I can provide you to debug that issue.
The code in the first post doesn't call netconn_free(). So it will leak memory, as per the discussion & linked issue above.

If you have updated code that still leaks memory, please post it and we can take a look.

As suggested, rewriting to use the BSD sockets API may also be a good option.

pratik.yadav.455
Posts: 23
Joined: Thu Oct 26, 2017 7:34 am

Re: Need Help regarding Netconn Memory Leak Issue

Postby pratik.yadav.455 » Wed Nov 29, 2017 6:00 am

As suggested above i called netconn_free() after netcon_delete() , but still facing same issue.

My Code is as following:

Code: Select all

 
 static void server(void *pvParameters)
{
	
  printf("\n system_get_free_heap_size in starting of server task =%d\n\n\n",system_get_free_heap_size());
  struct netconn *conn , *newconn;
 
  err_t err;
  conn = netconn_new(NETCONN_TCP);
 
  err= netconn_bind(conn, NULL, 80);
   if (err == ERR_OK) {printf("\nsocket bound\n");}
   
  netconn_listen(conn);
	
  do{
		
		ESP_LOGI(TAG,"\nsystem_get_free_heap_size before accept in request number %d =%d\n\n\n",++counter,system_get_free_heap_size());
		err = netconn_accept(conn, &newconn);
		
		if (err == ERR_OK)
		{
			ESP_LOGI(TAG,"connection accept\n");
			
			ESP_LOGI(TAG,"\nsystem_get_free_heap_size after accept  =%d\n\n\n",system_get_free_heap_size());			
      
			
		}				
		
		
		ESP_LOGI(TAG,"\nsystem_get_free_heap_size befor close  newconn =%d\n\n\n",system_get_free_heap_size());
		
		err=netconn_close(newconn);
		if(err!=ERR_OK) printf("\n new con not closed\n");		
		
		
		ESP_LOGI(TAG,"\nsystem_get_free_heap_size after close  and before delete newconn  =%d\n\n\n",system_get_free_heap_size());
		
		err_t err_newconn_d=netconn_delete(newconn); 
		if (err_newconn_d != ERR_OK) printf("newconn not delete succesfully\n");
		
		ESP_LOGI(TAG,"\nsystem_get_free_heap_size after delete and befor free =%d\n\n\n",system_get_free_heap_size());
		
		netconn_free(newconn);
		ESP_LOGI(TAG,"\nsystem_get_free_heap_size after calling netconn_free =%d\n\n\n",system_get_free_heap_size());
		
		
		
    } while(err == ERR_OK);
    
    
   netconn_close(conn);
   printf("conn is close\n");
   netconn_delete(conn); 
     

}

 

This is only task, which is running.And there is random memory leak.




my log is as following:

Code: Select all


I (1322) main: system_get_free_heap_size before accept in request number 1 =234892


I (21702) main: system_get_free_heap_size after accept  =230264


I (21702) main:system_get_free_heap_size befor close  newconn =230264


I (21712) main: system_get_free_heap_size after close  and before delete newconn  =232884


I (21722) main: system_get_free_heap_size after delete and befor free =233240


I (21732) main: system_get_free_heap_size after calling netconn_free =233300


I (21742) main:system_get_free_heap_size before accept in request number 2 =233300


I (22712) main: connection accept
I (22712) main: system_get_free_heap_size after accept  =232820


I (22712) main: system_get_free_heap_size befor close  newconn =231056


I (22722) main: system_get_free_heap_size after close  and before delete newconn  =232668


I (22732) main: system_get_free_heap_size after delete and befor free =233032


I (22742) main: system_get_free_heap_size after calling netconn_free =233092


I (22752) main: system_get_free_heap_size before accept in request number 3 =233092


I (23662) main: connection accept
I (23662) main: system_get_free_heap_size after accept  =230864


I (23662) main: system_get_free_heap_size befor close  newconn =230864


I (23672) main: system_get_free_heap_size after close  and before delete newconn  =232460


I (23682) main: system_get_free_heap_size after delete and befor free =232824


I (23692) main: system_get_free_heap_size after calling netconn_free =232884


I (23702) main: system_get_free_heap_size before accept in request number 4 =232884


I (24452) main: connection accept
I (24452) main: system_get_free_heap_size after accept  =230648


I (24452) main: system_get_free_heap_size befor close  newconn =230648


I (24462) main: system_get_free_heap_size after close  and before delete newconn  =232252


I (24462) main: system_get_free_heap_size after delete and befor free =232616


I (24472) main: system_get_free_heap_size after calling netconn_free =232676


I (24482) main: system_get_free_heap_size before accept in request number 5 =232676


I (25592) main: connection accept
I (25592) main: system_get_free_heap_size after accept  =230440


I (25592) main: system_get_free_heap_size befor close  newconn =230440


I (25602) main: system_get_free_heap_size after close  and before delete newconn  =232044


I (25612) main: system_get_free_heap_size after delete and befor free =232408


I (25622) main: system_get_free_heap_size after calling netconn_free =232468


I (25632) main: system_get_free_heap_size before accept in request number 6 =232468
I (879372) main: connection accept
I (879372) main: system_get_free_heap_size after accept  =229896


I (879372) main: system_get_free_heap_size befor close  newconn =229896


I (879382) main:system_get_free_heap_size after close  and before delete newconn  =231720


I (879392) main: system_get_free_heap_size after delete and befor free =231856


I (879402) main: system_get_free_heap_size after calling netconn_free =231916


I (879412) main: system_get_free_heap_size before accept in request number 7 =231916


I (892582) main: connection accept
I (892582) main: system_get_free_heap_size after accept  =229352


I (892582) main: system_get_free_heap_size befor close  newconn =229436


I (892592) main: system_get_free_heap_size after close  and before delete newconn  =231292


I (892602) main: system_get_free_heap_size after delete and befor free =231648


I (892612) main: system_get_free_heap_size after calling netconn_free =231708


I (892622) main: system_get_free_heap_size before accept in request number 8 =231708


I (925662) main: connection accept
I (925662) main: system_get_free_heap_size after accept  =230524


I (925662) main: system_get_free_heap_size befor close  newconn =230524


I (925672) main: system_get_free_heap_size after close  and before delete newconn  =230368

I (925672) main: system_get_free_heap_size after delete and befor free =230600


I (925682) main: system_get_free_heap_size after calling netconn_free =230660


I (925692) main: system_get_free_heap_size before accept in request number 9 =230660


I (925802) main: connection accept
I (925802) main: system_get_free_heap_size after accept  =230872


I (925802) main: system_get_free_heap_size befor close  newconn =230872


I (925812) main: system_get_free_heap_size after close  and before delete newconn  =230716


I (925822) main: system_get_free_heap_size after delete and befor free =230948


I (925832) main: system_get_free_heap_size after calling netconn_free =231008


I (925842) main: system_get_free_heap_size before accept in request number 10 =229268

Also I saw memory is released after calling netcon_freee(), but its only 60 bytes.where dose other bytes consumed?

Using reference of https://github.com/cmmakerclub/esp32-webserver we made application, but we saw memory leakage in it , after debugging we found this issue.

So, I would like to know that, is there any proper solution for lwip netcon library ? Or we have to use socket API?

Am i Missing anything?

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Need Help regarding Netconn Memory Leak Issue

Postby WiFive » Wed Nov 29, 2017 7:21 am

There is no cJSON_Delete(root);

pratik.yadav.455
Posts: 23
Joined: Thu Oct 26, 2017 7:34 am

Re: Need Help regarding Netconn Memory Leak Issue

Postby pratik.yadav.455 » Wed Nov 29, 2017 8:50 am

WiFive wrote:There is no cJSON_Delete(root);
It is in example but i am only creating server task.Right now I only create server task as shown above.Only this task is created in my app_main().
my app_main is as following :

Code: Select all

static esp_err_t event_handler(void *ctx, system_event_t *event)
{
    switch(event->event_id) {
    case SYSTEM_EVENT_STA_START:
        esp_wifi_connect();
        break;
    case SYSTEM_EVENT_STA_GOT_IP:
        xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
        printf("got ip\n");
        printf("ip: " IPSTR "\n", IP2STR(&event->event_info.got_ip.ip_info.ip));
        printf("netmask: " IPSTR "\n", IP2STR(&event->event_info.got_ip.ip_info.netmask));
        printf("gw: " IPSTR "\n", IP2STR(&event->event_info.got_ip.ip_info.gw));
        printf("\n");
        fflush(stdout);
        break;
    case SYSTEM_EVENT_STA_DISCONNECTED:
        /* This is a workaround as ESP32 WiFi libs don't currently
           auto-reassociate. */
        esp_wifi_connect();
        xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
        break;
    default:
        break;
    }
    return ESP_OK;
}

static void initialise_wifi(void)
{
    tcpip_adapter_init();
    wifi_event_group = xEventGroupCreate();
    ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
    ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
    ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_AP) );
    wifi_config_t ap_config = {
        .ap = {
            .ssid = "PRATIK",
            .password = "0123456789",
            .authmode=WIFI_AUTH_WPA_WPA2_PSK,
            .max_connection=5,
            
        }
    };

    ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_AP, &ap_config) );
    ESP_ERROR_CHECK( esp_wifi_start() );
}
int app_main(void)
{
	
    nvs_flash_init();
    system_init();
    initialise_wifi();
    
       
    
    xTaskCreate(&server, "server", 4048, NULL, 5, NULL);
    
   
  
    return 0;
}


Ritesh
Posts: 1383
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: Need Help regarding Netconn Memory Leak Issue

Postby Ritesh » Wed Nov 29, 2017 9:16 am

WiFive wrote:I don't think any idf examples use netconn api
Hi,

We have taken reference from below link for that example.

https://github.com/cmmakerclub/esp32-webserver
Regards,
Ritesh Prajapati

Ritesh
Posts: 1383
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: Need Help regarding Netconn Memory Leak Issue

Postby Ritesh » Tue Dec 05, 2017 6:30 pm

Hi

I think Espressif Systems Developer had applied patch to fix this error into Master development branch yesterday.

So, They have updated LWIP portion with netconn fixed and released into master idf.

So, we will check that fix and will post if any issue after that.
Regards,
Ritesh Prajapati

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

Re: Need Help regarding Netconn Memory Leak Issue

Postby ESP_Angus » Tue Dec 05, 2017 11:00 pm

Hi Pratik,

Thanks for posting the code and the log. It's not clear to me from the log that you have a memory leak. If I look at the same log line in your code each time around the loop, there's no long-term trend of less free RAM:

Code: Select all

I (23662) main: system_get_free_heap_size after accept  =230864
 
I (24452) main: system_get_free_heap_size after accept  =230648
 
I (25592) main: system_get_free_heap_size after accept  =230440
 
I (879372) main: system_get_free_heap_size after accept  =229896
 
I (892582) main: system_get_free_heap_size after accept  =229352
 
I (925662) main: system_get_free_heap_size after accept  =230524
 
I (925802) main: system_get_free_heap_size after accept  =230872
In fact, the final log line shows more free heap (230872) than the initial line (230864).

This kind of minor variation over time is due to the TCP/IP & WiFi library activity. Some of it may be explained by TCP sockets in the TIME_WAIT state, as I mentioned above, but the WiFi library may also allocate temporary buffers for various uses.

If you see a long term trend of free memory reducing, that is a memory leak.

Who is online

Users browsing this forum: Google [Bot] and 91 guests