From 0b7920b79a18e77c8f337d285829e7b67fded388 Mon Sep 17 00:00:00 2001 From: irriden Date: Wed, 5 Jul 2023 20:26:11 +0000 Subject: [PATCH] memory: track biggest free block instead of total RAM This accounts for memory fragmentation Tested on multiple runs - restarts all good. --- sphinx-key/src/core/events.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/sphinx-key/src/core/events.rs b/sphinx-key/src/core/events.rs index c8e63d9..c41a1ab 100644 --- a/sphinx-key/src/core/events.rs +++ b/sphinx-key/src/core/events.rs @@ -225,9 +225,10 @@ pub fn make_event_loop( fn restart_esp_if_memory_low() { unsafe { let size = esp_idf_sys::heap_caps_get_free_size(4); - let threshold = 65000; - log::info!("Available DRAM: {}, Restart Threshold: {}", size, threshold); - if size < threshold { + let block = esp_idf_sys::heap_caps_get_largest_free_block(4); + let threshold = 35000; + log::info!("Available DRAM: {}, Max block: {}, Restart Threshold: {}", size, block, threshold); + if block < threshold { log::info!("Restarting esp!"); esp_idf_sys::esp_restart(); }