2

I am using Free RTOS on ESP32 in my program. However at some point of time when I execute xTaskCreate() it returns -1 , the error being could_not_allocate_required_memory. In my program, each time I pring getFreeHeap() it is reduced by 2,000 approximately and is never cleared or reset. When it is not sufficient or below 2,000 xTaskCreate returns -1.

After reading a lot of forums I could not find a way to clear the memory to create a task. Can anyone guide me with this?

code snippet

void createTsk(){
  SerialDebug.print("available Heap size: ");
  SerialDebug.println(ESP.getFreeHeap());

  BaseType_t xBy = xTaskCreate(taskPriorityOne, "TaskOne", 20240, NULL, 1, &thOne);
  SerialDebug.println("Return Val : ");
  SerialDebug.println(xBy);
}

void taskPriorityOne(void *paramter)
{
   SerialDebug.println("Prio 1 task Initiated");
   threadTask(priorityOneQue, "task1", thOne);
}

void threadTask(QueueHandle_t priorityQue, String taskName, TaskHandle_t th)
{
  xQueueReceive(priorityQue, &taskdataholder, portMAX_DELAY);
  ongoingProcess = taskdataholder;
  furtherExecutionHttp(taskdataholder, fnm, taskName, th);
}

void furtherExecutionHttp(DataHolder *taskdataholder, String fnm, String 
taskName, TaskHandle_t th)
{
   String response = httpRequest(taskdataholder->protocol, taskdataholder- 
   >host, taskdataholder->port, taskdataholder->url, taskdataholder->body, 
   fnm);

  delay(10);

  free(taskdataholder);
  SerialDebug.println("Releasing memory..\n");

  vTaskDelete(th);
}
9
  • I don't know much about FreeRTOS, but memory on the heap can be cleared by deleting the dynamically created objects, that you don't need. Have you searched for a function in FreeRTOS to do this? In plain C/C++ it is just the delete operator.
    – chrisl
    Commented Jan 14, 2021 at 7:23
  • @chrisl hi. thanks for the reply. I am using arduino ide, hence i am not be able to do few custom modification. is there any more resolved way can you suggest ?
    – Androing
    Commented Jan 14, 2021 at 7:41
  • I'm not asking for "custom modifications". Code inside the Arduino IDE is just plain C/C++ with the Arduino framework (and the FreeRTOS framework in your case). You have some code (please show it in your question by editing it) and in this code you create objects dynamically. These objects must be deleted again. Someone must keep track over the objects, that are created, and free the memory, when they are not needed anymore. That can be you with your own code, or maybe the FreeRTOS framework (which I don't know enough to say, if it is capable of doing that).
    – chrisl
    Commented Jan 14, 2021 at 7:47
  • 1
    @chrisl code added. please suggest.
    – Androing
    Commented Jan 14, 2021 at 8:03
  • 1
    @Androing: Hmm.. True, that looks right. However, since thOne is a global variable, this only works if only one task is running at a time. If you start a new task while the old has not exited, the next call to xTaskDelete will delete the task that was most recently started, not the current one. Try xTaskDelete(null) to delete the current task.
    – PMF
    Commented Jan 14, 2021 at 9:14

2 Answers 2

2

The heap memory in ESP32 cannot be cleared explicitly. The heap memory is used to store dynamically allocated objects, and when an object is no longer needed, it is automatically deallocated by the garbage collector.

You can free up some heap memory by deleting unused objects. In your code, you create a new object called taskdataholder in the furtherExecutionHttp() function. This object is allocated on the heap and will only be deallocated once the function returns. If you want to free up this memory, you can call the free() function before the function returns.

0

You probably don't want to "clear" the heap, assuming you mean either deallocating or zeroing all memory that you've allocated from the heap.

If you simply return all memory to the heap when you run out, your code may have pointers to that memory remaining when that memory gets allocated to another use, meaning some data will likely get over-written. Or, if you clear the heap (by zeroing it) you will be over-writing data already stored there, and possibly still be unable to allocate more memory if the allocator doesn't "know" that you meant to return it.

Instead, your code should free each memory allocation as soon as it is no longer useful, and make sure its pointer to that memory won't be used again. One way to protect re-use of dead pointers is 1) zero them when you deallocate the memory they point to; and 2) always test for a valid pointer (non-zero) before you try to use it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.