Need Help regarding Netconn Memory Leak Issue

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

Need Help regarding Netconn Memory Leak Issue

Postby pratik.yadav.455 » Thu Nov 23, 2017 1:33 pm

Hello,

I implemented simple server using lwip netcon My server 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{
		
		printf("\nsystem_get_free_heap_size before accept  =%d\n\n\n",system_get_free_heap_size());
		err = netconn_accept(conn, &newconn);
		printf("\nsystem_get_free_heap_size after accept  =%d\n\n\n",system_get_free_heap_size());
		if (err == ERR_OK)
		{
			printf("connection accept\n");		
			     
			
		}		
		
		
		
		printf("\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");
		
		
		
		
		printf("\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");
		printf("\nsystem_get_free_heap_size in end of http_server task =%d\n\n\n",system_get_free_heap_size());
		
		
		
    } while(err == ERR_OK);
    
    
   netconn_close(conn);
   printf("conn is close\n");
   netconn_delete(conn); 
     
   printf("http exit\n");
}
When I send request first time free heap size is 223520,

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.

continuously my heap size is reducing as I send request continuously.

My questions are:

Why memory is consume at every request? Is it Library issue or am I doing anything wrong?

How to manage this memory consumption?

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

Re: Need Help regarding Netconn Memory Leak Issue

Postby Ritesh » Thu Nov 23, 2017 2:12 pm

Hi Espressif Systems Developer or Loboris,

I and Pratik are working together in this stuff and we need your quick help for same.

So, Would you please check that issue at your end and provide us any update like we are doing something wrong or issue is in netconn library?

It will be good if anyone has any idea regarding 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 » Thu Nov 23, 2017 10:55 pm

There is a bug, tracked here, for which the workaround is to call netconn_free() after calling netconn_delete():
https://github.com/espressif/esp-idf/issues/784

In addition, when you close a TCP connection, the TCP/IP stack needs to keep it in the TIME_WAIT state for a period of time. During this time the memory will appear to "leak", until the timeout expires (I believe it's 60 seconds but you may want to allow 120 seconds). This can be noticeable in any application which opens a lot of TCP connections over a short period of time.

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

Re: Need Help regarding Netconn Memory Leak Issue

Postby Ritesh » Sat Nov 25, 2017 4:18 am

Hi,

So, Issue is still in the open state. correct?

As they have provided one workaround solution to fix it but will it proper solution or just workaround solution?
Regards,
Ritesh Prajapati

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

Re: Need Help regarding Netconn Memory Leak Issue

Postby Ritesh » Sat Nov 25, 2017 4:51 am

ESP_Angus wrote:There is a bug, tracked here, for which the workaround is to call netconn_free() after calling netconn_delete():
https://github.com/espressif/esp-idf/issues/784

In addition, when you close a TCP connection, the TCP/IP stack needs to keep it in the TIME_WAIT state for a period of time. During this time the memory will appear to "leak", until the timeout expires (I believe it's 60 seconds but you may want to allow 120 seconds). This can be noticeable in any application which opens a lot of TCP connections over a short period of time.
Hi,

Thanks for quick support for that.

Actually, we have also tried it with netconn_free after netconn_delete API but issue remains same. but one more thing that we are closing connection and after 5 to 10 seconds one more HTTP request is coming and processing that request.

So, Will it create any problem as you have mentioned like TIME_WAIT state into TCP connection?
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 » Sun Nov 26, 2017 11:22 pm

Ritesh wrote:we are closing connection and after 5 to 10 seconds one more HTTP request is coming and processing that request.

So, Will it create any problem as you have mentioned like TIME_WAIT state into TCP connection?
In this case you will definitely have some old TCP connections temporarily in TIME_WAIT, and they will use some memory for this time. This is not generally a problem, if memory is low then LWIP will preemptively clean up these connections. But it can look like a memory leak, which is why I mentioned it.

Ritesh
Posts: 1365
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 4:35 am

ESP_Angus wrote:
Ritesh wrote:we are closing connection and after 5 to 10 seconds one more HTTP request is coming and processing that request.

So, Will it create any problem as you have mentioned like TIME_WAIT state into TCP connection?
In this case you will definitely have some old TCP connections temporarily in TIME_WAIT, and they will use some memory for this time. This is not generally a problem, if memory is low then LWIP will preemptively clean up these connections. But it can look like a memory leak, which is why I mentioned it.
Hi,

Thanks for Reply.

But, We have checked that Memory is not going to be free after executing few HTTP based request back to back.
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 Nov 28, 2017 4:38 am

Ritesh wrote: But, We have checked that Memory is not going to be free after executing few HTTP based request back to back.
I can help you debug this but I will need a lot more information - timing, code in use, free memory over time, other tasks running, etc.

Ritesh
Posts: 1365
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 4:48 am

ESP_Angus wrote:
Ritesh wrote: But, We have checked that Memory is not going to be free after executing few HTTP based request back to back.
I can help you debug this but I will need a lot more information - timing, code in use, free memory over time, other tasks running, etc.
Hi,

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.
Regards,
Ritesh Prajapati

Who is online

Users browsing this forum: awegel, hetal-mpc and 149 guests