Skip to content

Commit 560734e

Browse files
committed
py/gc: Change heuristic for "greedy limit" to a number of bytes.
Will stop adding large chunks of heap once the free system heap is lower than this threshold. With the change the Python heap growth on original ESP32 is closer to the v1.20 Python heap size after the first time it expands. Signed-off-by: Angus Gratton <angus@redyak.com.au>
1 parent e7ede95 commit 560734e

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

ports/esp32/mpconfigport.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,15 @@ void *esp_native_code_commit(void *, size_t, void *);
182182
#define MP_PLAT_COMMIT_EXEC(buf, len, reloc) esp_native_code_commit(buf, len, reloc)
183183
#define MP_SSIZE_MAX (0x7fffffff)
184184

185+
#ifndef MP_PLAT_TRY_RESERVE_SYSTEM_HEAP
186+
// Try to keep some heap free for ESP-IDF system, unless Python is completely out of memory
187+
//
188+
// The default here is particularly high, because it's enough memory to initialise Wi-Fi and
189+
// create a TLS connection. If the program is not using these things, or if the heap doesn't
190+
// grow until after those things are created, then this will fragment memory more than needed.
191+
#define MP_PLAT_TRY_RESERVE_SYSTEM_HEAP (80 * 1024)
192+
#endif
193+
185194
// Note: these "critical nested" macros do not ensure cross-CPU exclusion,
186195
// the only disable interrupts on the current CPU. To full manage exclusion
187196
// one should use portENTER_CRITICAL/portEXIT_CRITICAL instead.

py/gc.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -286,15 +286,19 @@ STATIC bool gc_try_add_heap(size_t failed_alloc) {
286286

287287
size_t new_heap_size = total_heap;
288288

289-
// If this size is >75% of the total free RAM (across all free blocks of
290-
// system heap), reduce it to 75% of the total free RAM. This is to avoid
291-
// greedily claiming all of the system RAM for Python heap and running
292-
// into allocation failures elsewhere in the system.
289+
// If this "greedy" size will cut free system heap below
290+
// MP_PLAT_TRY_RESERVE_SYSTEM_HEAP then reduce it to conserve that limit.
291+
//
292+
// (MP_PLAT_TRY_RESERVE_SYSTEM_HEAP still isn't a hard limit, if the only
293+
// options are returning a MemoryError to Python or using up the reserved
294+
// system heap space then MicroPython will use up the reserved system heap
295+
// space.)
293296

294297
size_t total_free = gc_get_total_free();
295-
size_t grow_limit = total_free * 75 / 100;
296-
if (new_heap_size > grow_limit) {
297-
new_heap_size = grow_limit;
298+
if (total_free < MP_PLAT_TRY_RESERVE_SYSTEM_HEAP) {
299+
new_heap_size = needed;
300+
} else if (total_free - new_heap_size < MP_PLAT_TRY_RESERVE_SYSTEM_HEAP) {
301+
new_heap_size = total_free - MP_PLAT_TRY_RESERVE_SYSTEM_HEAP;
298302
}
299303

300304
// If this size is smaller than the size 'needed' to avoid an immediate
@@ -314,8 +318,8 @@ STATIC bool gc_try_add_heap(size_t failed_alloc) {
314318
}
315319

316320
DEBUG_printf("total_heap " UINT_FMT " total_free "
317-
UINT_FMT "grow_limit " UINT_FMT " bytes\n",
318-
total_heap, total_free, grow_limit);
321+
UINT_FMT " TRY_RESERVE_SYSTEM_HEAP " UINT_FMT " bytes\n",
322+
total_heap, total_free, MP_PLAT_TRY_RESERVE_SYSTEM_HEAP);
319323

320324
mp_state_mem_area_t *new_heap = MP_PLAT_ALLOC_HEAP(new_heap_size);
321325

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy