Page 1 of 1

Heap memory consumption in webserver example

Posted: Sun Oct 21, 2018 8:09 pm
by burkulesomesh43
Hi,
I am using webserver example.
this example takes lot of heap memory. In the webserver task I am diaplaying free heap memory. as I hit the ip on browser, the web page gets opened but there is lot of heap memory consumption during task execution maybe due to my html code which is am hitting as my web page.
so How to solve this heap memory consumption in my task??

Code: Select all

static void web_server(void *pvParameters)
{
  struct netconn *conn, *newconn;
  err_t err;
  conn = netconn_new(NETCONN_TCP);
  netconn_bind(conn, NULL, 80);
  netconn_listen(conn);
  do {
     err = netconn_accept(conn, &newconn);
     if (err == ERR_OK) {
    	 vTaskDelay(10);
       http_server_netconn_serve(newconn);
       netconn_delete(newconn);
     }
     ESP_LOGI("ss", "MALLOC_CAP_DEFAULT %d", heap_caps_get_free_size(MALLOC_CAP_DEFAULT));

   } while(err == ERR_OK);
   netconn_close(conn);
   netconn_delete(conn);
   vTaskDelete(NULL);
}

Re: Heap memory consumption in webserver example

Posted: Mon Oct 22, 2018 3:53 am
by ESP_Angus
Hi,
burkulesomesh43 wrote: this example takes lot of heap memory. In the webserver task I am diaplaying free heap memory. as I hit the ip on browser, the web page gets opened but there is lot of heap memory consumption during task execution maybe due to my html code which is am hitting as my web page.
Can you give some indication of how much memory is being used? WiFi & TCP/IP stack will use some memory for buffers while connections are active.

Re: Heap memory consumption in webserver example

Posted: Mon Oct 22, 2018 5:42 am
by burkulesomesh43
ESP_Angus wrote:Hi,
burkulesomesh43 wrote: this example takes lot of heap memory. In the webserver task I am diaplaying free heap memory. as I hit the ip on browser, the web page gets opened but there is lot of heap memory consumption during task execution maybe due to my html code which is am hitting as my web page.
Can you give some indication of how much memory is being used? WiFi & TCP/IP stack will use some memory for buffers while connections are active.
[0;32mI (118045) ss: MALLOC_CAP_DEFAULT 75696[0m

[0;32mI (122705) ss: MALLOC_CAP_DEFAULT 62420[0m

[0;32mI (184475) ss: MALLOC_CAP_DEFAULT 49644[0m

[0;32mI (189135) ss: MALLOC_CAP_DEFAULT 34888[0m

Re: Heap memory consumption in webserver example

Posted: Mon Oct 22, 2018 6:42 am
by ESP_Angus
Does the free memory go back up or is it permanently going down?

Are you using the http_server example or some other code? Can you post the code you're running somewhere?

Re: Heap memory consumption in webserver example

Posted: Mon Oct 22, 2018 7:59 am
by burkulesomesh43
ESP_Angus wrote:Does the free memory go back up or is it permanently going down?

Are you using the http_server example or some other code? Can you post the code you're running somewhere?
it is permenetly going down. sorry I can't upload code.
I am using this function in http_server_netconn_serve(newconn).

Code: Select all

char *replaceWord(const char *s, const char *oldW,const char *newW)
{
    char *result;
    int i, cnt = 0;
    int newWlen = strlen(newW);
    int oldWlen = strlen(oldW);

    // Counting the number of times old word
    // occur in the string
    for (i = 0; s[i] != '\0'; i++)
    {
        if (strstr(&s[i], oldW) == &s[i])
        {
            cnt++;

            // Jumping to index after the old word.
            i += oldWlen - 1;
        }
    }

    // Making new string of enough length
    result = (char *)malloc(i + cnt * (newWlen - oldWlen) + 1);

    i = 0;
    while (*s)
    {
        // compare the substring with the result
        if (strstr(s, oldW) == s)
        {
            strcpy(&result[i], newW);
            i += newWlen;
            s += oldWlen;
        }
        else
            result[i++] = *s++;
    }

    result[i] = '\0';
    return result;
}

Re: Heap memory consumption in webserver example

Posted: Mon Oct 22, 2018 1:52 pm
by loboris
Do you free somewhere the memory allocated in replaceWord() ?
How can you excpect help if you cannot give the whole code causing the issue?

Re: Heap memory consumption in webserver example

Posted: Mon Oct 22, 2018 4:56 pm
by burkulesomesh43
loboris wrote:Do you free somewhere the memory allocated in replaceWord() ?
How can you excpect help if you cannot give the whole code causing the issue?
issue solved. the memory allocated in replaceWord() is freed.