@@ -537,3 +537,61 @@ the corresponding functions, or you can use the command-line client
537
537
538
538
See the MicroPython forum for other community-supported alternatives
539
539
to transfer files to an ESP32 board.
540
+
541
+ Controlling the Python heap size
542
+ --------------------------------
543
+
544
+ _This feature is only supported with esp-idf v4 and above._
545
+
546
+ By default MicroPython allocates the largest contiguous chunk of memory to the python heap.
547
+ On a "simple" esp32 this comes out to around 100KB and on an esp32 with external SPIRAM this
548
+ ends up being the full SPIRAM, typically 4MB. This default allocation may not be desirable
549
+ and can be reduced for two use-cases by setting one of two variables in the "micropython" NVS
550
+ namespace, see :ref: `esp32.NVS <esp32.NVS >` for details about accessing NVS (Non-Volatile
551
+ Storage).
552
+
553
+ Because MicroPython allocates the heap as one of the very first actions it is not possible to run
554
+ python code to set the heap size. This is the reason that NVS variables are used and it also means
555
+ that a hard reset is necessary after setting the variables before they take effcet.
556
+ A typical use is for `main.py ` to check heap sizes and, if they're not appropriate,
557
+ to set an NVS variable and perform a hard reset.
558
+
559
+ The first use-case for this feature is to guarantee that ESP-IDF has some minimum amount of memory
560
+ to work with. For example, by default without SPIRAM there is around 90KB left for ESP-IDF
561
+ right at boot time. This is not enough for two TLS connections and not enough for one
562
+ TLS connection and BLE either. (While 90KB might seem like a lot, it disappears quickly once
563
+ Wifi is started and sockets are connected.)
564
+
565
+ To give esp-idf a bit more memory, use something like:
566
+
567
+ import machine
568
+ from esp32 import NVS, idf_heap_info
569
+
570
+ idf_free = sum([h[2] for h in idf_heap_info(HEAP_DATA)])
571
+ print("IDF heap free:", idf_free)
572
+
573
+ nvs = NVS("micropython")
574
+ nvs.set_i32("min_idf_heap", 120000)
575
+ nvs.commit()
576
+ machine.reset()
577
+
578
+ Setting the "min_idf_heap" NVS variable to 120000 tells MicroPython to reduce its heap allocation
579
+ from the default such that at least 120000 bytes are left for esp-idf.
580
+
581
+ A second use case is to reduce GC times when using SPIRAM. A GC collection has to read sequentially
582
+ though all of RAM during its sweep phase. When using a SPIRAM with the default 4MB allocation this
583
+ takes about 90ms (assuming 240Mhz cpu and 80Mhz QIO SPIRAM), which is very impactful in a not so
584
+ good way. Often applications only need a few hundred KB and this can be accomplished by setting the
585
+ "max_mp_heap" NVS variable to the desired size in bytes.
586
+
587
+ A similar use-case is with an SPIRAM where it is desired to leave memory to a native module, for
588
+ example to allocate a camera framebuffer. The size of the MP heap can be limited to at most 300KB
589
+ using something like:
590
+
591
+ import machine
592
+ from esp32 import NVS
593
+
594
+ nvs = NVS("micropython")
595
+ nvs.set_i32("max_mp_heap", 300*1024)
596
+ nvs.commit()
597
+ machine.reset()
0 commit comments