FreeRtos cpu starvation issue

Ambimat
Posts: 2
Joined: Mon Dec 08, 2025 12:56 pm

FreeRtos cpu starvation issue

Postby Ambimat » Mon Dec 08, 2025 1:34 pm

Hello ,

In below main.c file i am running task ,task1 from to ...... task21 and in this task wifi_rssi_monitor_task try to download 29kb data using call api and http ,
case 1:
if total number of task is 15 used than i can download proper data every time
case 2:
if i increase number of task 16 to 21 or more i an unable to download proper data
i change in menuconfig file which is mention in below screenshoot

my code output :

================= SYSTEM MONITOR =================
Free heap: 139224 bytes
Minimum ever free heap: 132288 bytes
Total heap (8-bit capable): 331940 bytes
Free internal heap: 139224 bytes
Free PSRAM: 0 bytes

----- Task List (stack free bytes) -----
----- Runtime Stats -----
==================================================

I (20384) wifi:<ba-add>idx:1 (ifx:0, 40:86:cb:de:ce:1f), tid:5, ssn:0, winSize:64
I (20404) WiFi_Scanner: ���� Guest RSSI: -57 dBm
I (20404) WiFi_Scanner: ���� Still connected to Guest (RSSI: -57 dBm)

================= SYSTEM MONITOR =================
Free heap: 89920 bytes
Minimum ever free heap: 85532 bytes
Total heap (8-bit capable): 331940 bytes
Free internal heap: 89920 bytes
Free PSRAM: 0 bytes

----- Task List (stack free bytes) -----
----- Runtime Stats -----
==================================================

I (23394) WiFi_Scanner: ���� Guest RSSI: -52 dBm
I (23394) WiFi_Scanner: ���� Still connected to Guest (RSSI: -52 dBm)
I (26314) WiFi_Scanner: ���� Guest RSSI: -51 dBm
I (26314) WiFi_Scanner: ���� Still connected to Guest (RSSI: -51 dBm)

================= SYSTEM MONITOR =================
Free heap: 93992 bytes
Minimum ever free heap: 85532 bytes
Total heap (8-bit capable): 331940 bytes
Free internal heap: 94172 bytes
Free PSRAM: 0 bytes

----- Task List (stack free bytes) -----
----- Runtime Stats -----
==================================================

I (29234) WiFi_Scanner: ���� Guest RSSI: -56 dBm
I (29234) WiFi_Scanner: ���� Still connected to Guest (RSSI: -56 dBm)
I (29694) mqtts_example: MQTT_EVENT_CONNECTED
I (29694) mqtts_example: Subscribed to topic 'test/client', msg_id=35146
main_field=*****************1I (30104) mqtts_example: Published JSON to 'ble-MAC': {
"type": "aaaaaa",
"Tag": "1"
} | msg_id=25057
I (32154) WiFi_Scanner: ���� Guest RSSI: -50 dBm
I (32154) WiFi_Scanner: ���� Still connected to Guest (RSSI: -50 dBm)
I (32554) mqtts_example: Other event id:3

================= SYSTEM MONITOR =================
Free heap: 94572 bytes
Minimum ever free heap: 85532 bytes
Total heap (8-bit capable): 331940 bytes
Free internal heap: 94572 bytes
Free PSRAM: 0 bytes

----- Task List (stack free bytes) -----
----- Runtime Stats -----
==================================================

I (35074) WiFi_Scanner: ���� Guest RSSI: -62 dBm
I (35074) WiFi_Scanner: ���� Still connected to Guest (RSSI: -62 dBm)
I (35104) mqtts_example: Other event id:5
I (37994) WiFi_Scanner: ���� Guest RSSI: -62 dBm
I (37994) WiFi_Scanner: ���� Still connected to Guest (RSSI: -62 dBm)

================= SYSTEM MONITOR =================
Free heap: 96876 bytes
Minimum ever free heap: 85532 bytes
Total heap (8-bit capable): 331940 bytes
Free internal heap: 96876 bytes
Free PSRAM: 0 bytes

----- Task List (stack free bytes) -----
----- Runtime Stats -----
==================================================

I (40914) WiFi_Scanner: ���� Guest RSSI: -59 dBm
I (40914) WiFi_Scanner: ���� Still connected to Guest (RSSI: -59 dBm)
I (43834) WiFi_Scanner: ���� Guest RSSI: -55 dBm
I (43834) WiFi_Scanner: ���� Still connected to Guest (RSSI: -55 dBm)

================= SYSTEM MONITOR =================
Free heap: 97276 bytes
Minimum ever free heap: 85532 bytes
Total heap (8-bit capable): 331940 bytes
Free internal heap: 97276 bytes
Free PSRAM: 0 bytes

----- Task List (stack free bytes) -----
----- Runtime Stats -----
==================================================

I (46754) WiFi_Scanner: ���� Guest RSSI: -54 dBm
I (46754) WiFi_Scanner: ���� Still connected to Guest (RSSI: -54 dBm)

================= SYSTEM MONITOR =================
Free heap: 96376 bytes
Minimum ever free heap: 85532 bytes
Total heap (8-bit capable): 331940 bytes
Free internal heap: 96376 bytes
Free PSRAM: 0 bytes

----- Task List (stack free bytes) -----
----- Runtime Stats -----
==================================================

I (49674) WiFi_Scanner: ���� Guest RSSI: -56 dBm
I (49674) WiFi_Scanner: ���� Still connected to Guest (RSSI: -56 dBm)
I (52604) WiFi_Scanner: ���� Guest RSSI: -57 dBm
I (52604) WiFi_Scanner: ���� Still connected to Guest (RSSI: -57 dBm)

================= SYSTEM MONITOR =================
Free heap: 96776 bytes
Minimum ever free heap: 85532 bytes
Total heap (8-bit capable): 331940 bytes
Free internal heap: 96776 bytes
Free PSRAM: 0 bytes

----- Task List (stack free bytes) -----
----- Runtime Stats -----
==================================================

I (55524) WiFi_Scanner: ���� Guest RSSI: -59 dBm
I (55524) WiFi_Scanner: ���� Still connected to Guest (RSSI: -59 dBm)
I (55524) mqtts_example: MQTT_EVENT_DATA
TOPIC=DT:10:20:BA:08:12:7E
DATA="hello"
TOPIC=DT:10:20:BA:08:12:7E
DATA="hello"
I (58444) WiFi_Scanner: ���� Guest RSSI: -57 dBm
I (58444) WiFi_Scanner: ���� Still connected to Guest (RSSI: -57 dBm)
I (59084) ASSISTNOW: [] Attempt 1/1
I (59084) ASSISTNOW: POST ""
W (59084) mqtts_example: Failed to parse JSON data: "hello"

================= SYSTEM MONITOR =================
Free heap: 82748 bytes
Minimum ever free heap: 82552 bytes
Total heap (8-bit capable): 331940 bytes
Free internal heap: 82748 bytes
Free PSRAM: 0 bytes

----- Task List (stack free bytes) -----
----- Runtime Stats -----
==================================================

I (61364) WiFi_Scanner: ���� Guest RSSI: -56 dBm
I (61364) WiFi_Scanner: ���� Still connected to Guest (RSSI: -56 dBm)
I (61454) esp-x509-crt-bundle: Certificate validated
I (64294) WiFi_Scanner: ���� Guest RSSI: -54 dBm
I (64294) WiFi_Scanner: ���� Still connected to Guest (RSSI: -54 dBm)

================= SYSTEM MONITOR =================
Free heap: 44904 bytes
Minimum ever free heap: 39944 bytes
Total heap (8-bit capable): 331940 bytes
Free internal heap: 44904 bytes
Free PSRAM: 0 bytes

----- Task List (stack free bytes) -----
----- Runtime Stats -----
==================================================

I (67214) WiFi_Scanner: ���� Guest RSSI: -52 dBm
I (67214) WiFi_Scanner: ���� Still connected to Guest (RSSI: -52 dBm)
I (70134) WiFi_Scanner: ���� Guest RSSI: -51 dBm
I (70134) WiFi_Scanner: ���� Still connected to Guest (RSSI: -51 dBm)

================= SYSTEM MONITOR =================
Free heap: 19112 bytes
Minimum ever free heap: 8748 bytes
Total heap (8-bit capable): 331940 bytes
Free internal heap: 19112 bytes
Free PSRAM: 0 bytes

----- Task List (stack free bytes) -----
----- Runtime Stats -----
==================================================

I (73054) WiFi_Scanner: ���� Guest RSSI: -56 dBm
I (73054) WiFi_Scanner: ���� Still connected to Guest (RSSI: -56 dBm)
I (73334) ASSISTNOW: HTTP 200, content-length: 29440, captured=29440, finished=yes
I (73334) POST API: POST completed
I (74454) ASSISTNOW: [] Done. Deleting task.

================= SYSTEM MONITOR =================
Free heap: 60968 bytes
Minimum ever free heap: 3636 bytes
Total heap (8-bit capable): 331940 bytes
Free internal heap: 60968 bytes
Free PSRAM: 0 bytes

----- Task List (stack free bytes) -----
----- Runtime Stats -----
==================================================

I (75954) mqtts_example: Other event id:5
I (75974) WiFi_Scanner: ���� Guest RSSI: -53 dBm
I (75974) WiFi_Scanner: ���� Still connected to Guest (RSSI: -53 dBm)

case 1 output:

I (41064) WiFi_Scanner: ���� RSSI: -51 dBm
I (41064) WiFi_Scanner: ���� Still connected to Guest (RSSI: -51 dBm)
I (43984) WiFi_Scanner: ���� RSSI: -52 dBm
I (43984) WiFi_Scanner: ���� Still connected to Guest (RSSI: -52 dBm)

================= SYSTEM MONITOR =================
Free heap: 52592 bytes
Minimum ever free heap: 39720 bytes
Total heap (8-bit capable): 332436 bytes
Free internal heap: 52592 bytes
Free PSRAM: 0 bytes

----- Task List (stack free bytes) -----
----- Runtime Stats -----
==================================================

I (46914) WiFi_Scanner: ���� RSSI: -52 dBm
I (46914) WiFi_Scanner: ���� Still connected to Guest (RSSI: -52 dBm)
I (49834) WiFi_Scanner: ���� RSSI: -52 dBm
I (49834) WiFi_Scanner: ���� Still connected to Guest (RSSI: -52 dBm)
I (49864) ASSISTNOW: HTTP 200, content-length: 29440, captured=29440, finished=yes

case 2 output :

I (116994) WiFi_Scanner: ���� Still connected to Guest (RSSI: -51 dBm)

================= SYSTEM MONITOR =================
Free heap: 26784 bytes
Minimum ever free heap: 18328 bytes
Total heap (8-bit capable): 332436 bytes
Free internal heap: 26784 bytes
Free PSRAM: 0 bytes

----- Task List (stack free bytes) -----
----- Runtime Stats -----
==================================================

I (119904) ASSISTNOW: HTTP 200, content-length: 29440, captured=16128, finished=yes
W (119904) ASSISTNOW: Short read: expected=29440, got=16128
I (119924) WiFi_Scanner: ���� RSSI: -48 dBm
I (119944) POST API: POST completed
I (120024) WiFi_Scanner: ���� Still connected to Guest (RSSI: -48 dBm)
I (120094) mqtts_example: Other event id:5
I (121164) ASSISTNOW: [k] Done. Deleting task.
I (123064) WiFi_Scanner: ���� RSSI: -50 dBm
I (123064) WiFi_Scanner: ���� Still connected to Guest (RSSI: -50 dBm)

================= SYSTEM MONITOR =================
Free heap: 75448 bytes
Minimum ever free heap: 14920 bytes
Total heap (8-bit capable): 332436 bytes
Free internal heap: 75448 bytes
Free PSRAM: 0 bytes


can you guide me how to solve this issue so i can download data properly , i hope we will connect as soon as possible

thank you
Attachments
Screenshot 2025-12-08 185216.png
Screenshot 2025-12-08 185216.png (54.69 KiB) Viewed 2587 times
Screenshot 2025-12-08 185018.png
Screenshot 2025-12-08 185018.png (57.07 KiB) Viewed 2587 times
main.c
(23.33 KiB) Downloaded 19 times

lichurbagan
Posts: 59
Joined: Thu Nov 13, 2025 3:20 pm

Re: FreeRtos cpu starvation issue

Postby lichurbagan » Tue Dec 09, 2025 4:35 pm

Your issue is low free heap during HTTP downloads. More tasks reduce memory and fragment the heap. TLS and HTTP buffers then fail to allocate. Reduce each task’s stack size. Shrink HTTP client buffers. Lower TLS buffer settings. Avoid creating and deleting many tasks. Use one shared HTTP worker task. Keep free heap above 50 KB during downloads. These changes stabilize memory and stop partial reads.

Ambimat
Posts: 2
Joined: Mon Dec 08, 2025 12:56 pm

Re: FreeRtos cpu starvation issue

Postby Ambimat » Wed Dec 10, 2025 5:54 am

I can not reduce task size because I need more than 20 tasks than how to reduce heap size or free ,I will share my http logic please review it also

my http code:
/* -------- HTTP response capture -------- */
typedef struct {
char *buf;
size_t len;
size_t cap;
int expected_len; // -1 if not known
bool finished;
} http_buf_t;

static void http_buf_reset(http_buf_t *hb, bool free_mem)
{
if (free_mem && hb->buf) {
free(hb->buf);
}
hb->buf = NULL;
hb->len = 0;
hb->cap = 0;
hb->expected_len = -1;
hb->finished = false;
}

size_t downloaded_len =0;
static esp_err_t http_event_handler(esp_http_client_event_t *evt)
{
http_buf_t *hb = (http_buf_t *)evt->user_data;
switch (evt->event_id) {
case HTTP_EVENT_ON_HEADER:
if (evt->header_key && evt->header_value &&
strcasecmp(evt->header_key, "Content-Length") == 0) {
hb->expected_len = atoi(evt->header_value);
if (hb->expected_len > 0 && hb->cap < (size_t)hb->expected_len + 1) {
char *nb = 0;
nb = (char *)realloc(hb->buf, (size_t)hb->expected_len + 1);
if (!nb) return ESP_FAIL;
hb->buf = nb;
hb->cap = (size_t)hb->expected_len + 1;
}
}
break;

case HTTP_EVENT_ON_DATA:
if (evt->data_len > 0) {
// Ensure capacity (handles chunked / unknown length)
size_t need = hb->len + evt->data_len + 1;
if (need > hb->cap) {
size_t newcap = hb->cap ? hb->cap : 8096;
while (newcap < need) newcap *= 2;
char *nb =0;
nb = (char *)realloc(hb->buf, newcap);
if (!nb) return ESP_FAIL;
hb->buf = nb;
hb->cap = newcap;
}
memcpy(hb->buf + hb->len, evt->data, evt->data_len);
hb->len += evt->data_len;
hb->buf[hb->len] = '\0';
}
break;

case HTTP_EVENT_ON_FINISH:
hb->finished = true;
break;

case HTTP_EVENT_DISCONNECTED:
// leave hb->finished as-is; caller can inspect hb->len / finished
break;

default:
break;
}
return ESP_OK;
}

//http_buf_t hb = { .expected_len = -1 };
http_buf_t hb = { .buf = NULL, .len = 0, .cap = 0, .expected_len = -1, .finished = false };

static esp_err_t post_api(const char *url,
const char *token,
const char *uniqid_hex,
const char *monver_hex)
{
// ... build json as you do ... // Build JSON body (same as your curl)
// Use snprintf to avoid extra deps; payload is a JSON with string values.
char *json = NULL;
size_t need = strlen(token) + strlen(uniqid_hex) + strlen(monver_hex) + 256;
json = malloc(need);
if (!json) return ESP_ERR_NO_MEM;

snprintf(json, need,
"{\n"
" \"token\": \"%s\",\n"
" \"UBX-SEC-UNIQID\": \"%s\",\n"
" \"UBX-MON-VER\": \"%s\"\n"
"}",
token, uniqid_hex, monver_hex);




http_buf_reset(&hb, /*free_mem=*/true);
esp_http_client_config_t cfg = {
.url = url,
.method = HTTP_METHOD_POST,
.event_handler = http_event_handler,
.user_data = &hb,
.timeout_ms = 45000,
.crt_bundle_attach = esp_crt_bundle_attach,
.disable_auto_redirect = false,
};

esp_http_client_handle_t client = esp_http_client_init(&cfg);
if (!client) { free(json); return ESP_FAIL; }

esp_http_client_set_header(client, "Content-Type", "application/json");
esp_http_client_set_header(client, "Accept-Encoding", "identity"); // avoid gzip
ESP_ERROR_CHECK( esp_http_client_set_post_field(client, json, strlen(json)) );

ESP_LOGI(TAG_A, "POST %s", url);
esp_err_t err = esp_http_client_perform(client);

if (err == ESP_OK) {
int status = esp_http_client_get_status_code(client);
int cl = esp_http_client_get_content_length(client);
if (cl >= 0 && hb.expected_len < 0) hb.expected_len = cl;

ESP_LOGI(TAG_A, "HTTP %d, content-length: %d, captured=%u, finished=%s",
status, cl, (unsigned)hb.len, hb.finished ? "yes" : "no");
downloaded_len = hb.len;

if (hb.expected_len >= 0 && hb.len != (size_t)hb.expected_len) {
ESP_LOGW(TAG_A, "Short read: expected=%d, got=%u", hb.expected_len, (unsigned)hb.len);
}

if (hb.buf && hb.len) {
if (HEX_STREAM) { free(HEX_STREAM); HEX_STREAM = NULL; HEX_STREAM_LEN = 0; }
HEX_STREAM = hb.buf;
HEX_STREAM_LEN = hb.len;
hb.buf = NULL; // keep it
//log_chunked_1k(HEX_STREAM, HEX_STREAM_LEN); // optional
}
} else {
ESP_LOGE(TAG_A, "HTTP request failed: %s", esp_err_to_name(err));
}

printf("Heap before cleanup: %d\n", (unsigned)esp_get_free_heap_size());
esp_http_client_close(client);
esp_http_client_cleanup(client);
printf("Heap after cleanup: %d\n", (unsigned)esp_get_free_heap_size());
free(hb.buf); // only frees if ownership not taken
hb.buf=NULL;
hb.len=0;
hb.expected_len=0;

free(json);
return err;
}

Sprite
Espressif staff
Espressif staff
Posts: 10596
Joined: Thu Nov 26, 2015 4:08 am

Re: FreeRtos cpu starvation issue

Postby Sprite » Thu Dec 11, 2025 1:45 am

I can not reduce task size because I need more than 20 task
Can I ask why? That honestly sounds like a problem with your architecture.

MicroController
Posts: 2661
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: FreeRtos cpu starvation issue

Postby MicroController » Thu Dec 11, 2025 8:17 am

I need more than 20 tasks
At about ~5kB per task that's not a problem per se. However, alternative, more RAM-efficient, approaches include using timers to execute multiple short activities at certain intervals, or creating a single "worker" task which can do different things at different times.

Who is online

Users browsing this forum: Baidu [Spider] and 13 guests