Heap memory consumption in webserver example

burkulesomesh43
Posts: 132
Joined: Tue Aug 14, 2018 6:21 am
Location: India

Heap memory consumption in webserver example

Postby burkulesomesh43 » Sun Oct 21, 2018 8:09 pm

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);
}
--
Somesh Burkule

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

Re: Heap memory consumption in webserver example

Postby ESP_Angus » Mon Oct 22, 2018 3:53 am

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.

burkulesomesh43
Posts: 132
Joined: Tue Aug 14, 2018 6:21 am
Location: India

Re: Heap memory consumption in webserver example

Postby burkulesomesh43 » Mon Oct 22, 2018 5:42 am

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
--
Somesh Burkule

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

Re: Heap memory consumption in webserver example

Postby ESP_Angus » Mon Oct 22, 2018 6:42 am

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?

burkulesomesh43
Posts: 132
Joined: Tue Aug 14, 2018 6:21 am
Location: India

Re: Heap memory consumption in webserver example

Postby burkulesomesh43 » Mon Oct 22, 2018 7:59 am

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;
}
--
Somesh Burkule

User avatar
loboris
Posts: 514
Joined: Wed Dec 21, 2016 7:40 pm

Re: Heap memory consumption in webserver example

Postby loboris » Mon Oct 22, 2018 1:52 pm

Do you free somewhere the memory allocated in replaceWord() ?
How can you excpect help if you cannot give the whole code causing the issue?

burkulesomesh43
Posts: 132
Joined: Tue Aug 14, 2018 6:21 am
Location: India

Re: Heap memory consumption in webserver example

Postby burkulesomesh43 » Mon Oct 22, 2018 4:56 pm

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.
--
Somesh Burkule

Who is online

Users browsing this forum: BivvyCoder and 90 guests