free memory 100KB

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
mschu
Posts: 2
Joined: Wed Sep 12, 2018 2:31 pm

free memory 100KB

Post by mschu » Wed Sep 12, 2018 2:39 pm

ESP32 has 512KB S-RAM.
The running micropython, gc.mem_free() is about 100KB.
Is the remaining 100KB a little few?

As a compilation, is there a way to suppress memory usage by making some library unused?

BR,
mschu

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: free memory 100KB

Post by jickster » Wed Sep 12, 2018 4:36 pm

mschu wrote:
Wed Sep 12, 2018 2:39 pm
ESP32 has 512KB S-RAM.
The running micropython, gc.mem_free() is about 100KB.
Is the remaining 100KB a little few?

As a compilation, is there a way to suppress memory usage by making some library unused?

BR,
mschu
What is the heap size you're passing to gc_init()?

mschu
Posts: 2
Joined: Wed Sep 12, 2018 2:31 pm

Re: free memory 100KB

Post by mschu » Sat Sep 29, 2018 1:46 am

I got heap size with gc_init ().

heap size : heap_caps_get_largest_free_block(MALLOC_CAP_8BIT)

That is, It's specify the remaining memory size of micropython.
In other words, 100 KB is known as the heap maximum size.

By the way, if we forcibly designate a heap size of 150 KB, restart was repeated due to a fatal error at startup.

Do you know how to reduce the micopython system for getting big size of heap memory ?

----------------------------
ports/esp32/main.c

line 75~77
// Allocate the uPy heap using malloc and get the largest available region
size_t mp_task_heap_size = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT);
void *mp_task_heap = malloc(mp_task_heap_size); # change to malloc(150*1024) -> a fatal error at startup.
----------------------------

Thank you.

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

free memory 100KB

Post by jickster » Sun Sep 30, 2018 12:09 am

I know nothing about ESP32 but I can help with micropython.

Micropython does use some RAM when you start it up.

However, unless you “import” a module, micropython does not use RAM.

Please run this Python code at the very beginning and reply back with results:

import sys
import gc
dir()
sys.modules

This will tell you which modules are loaded and taking up RAM.



Sent from my iPhone using Tapatalk Pro

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: free memory 100KB

Post by jickster » Sun Sep 30, 2018 8:09 pm

mschu wrote:
Sat Sep 29, 2018 1:46 am
I got heap size with gc_init ().

heap size : heap_caps_get_largest_free_block(MALLOC_CAP_8BIT)

That is, It's specify the remaining memory size of micropython.
In other words, 100 KB is known as the heap maximum size.

By the way, if we forcibly designate a heap size of 150 KB, restart was repeated due to a fatal error at startup.

Do you know how to reduce the micopython system for getting big size of heap memory ?

----------------------------
ports/esp32/main.c

line 75~77
// Allocate the uPy heap using malloc and get the largest available region
size_t mp_task_heap_size = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT);
void *mp_task_heap = malloc(mp_task_heap_size); # change to malloc(150*1024) -> a fatal error at startup.
----------------------------

Thank you.
There's two things you have to keep separate: ESP32 and micropython.

The biggest block of RAM available on ESP32 is, according to the ESP32 documentation, obtained by heap_caps_get_largest_free_block().
Therefore, if heap_caps_get_largest_free_block() returns 100KB, it makes no sense to call malloc(150*1024). heap_caps_get_largest_free_block() == 100000 means "you can only call malloc with an argument no larger than 100000"
If you call malloc with a bigger number than heap_caps_get_largest_free_block(), you're gonna get NULL and if you pass NULL into gc_init(), of course there's gonna be a fatal error.

heap_caps_get_largest_free_block() has nothing to do with micropython. It returns how much memory the ESP32 has left.

User avatar
mattyt
Posts: 410
Joined: Mon Jan 23, 2017 6:39 am

Re: free memory 100KB

Post by mattyt » Sun Sep 30, 2018 11:39 pm

Hi @mschu,

There is 'only' a little over 100KB of available memory by the time MicroPython starts on a standard ESP32.

Although the device has 520KB RAM only 328KB is DRAM, available for use by the MicroPython heap; the remaining 192KB is IRAM (instruction use only). Of the 328KB, various 'chunks' go to FreeRTOS/ESP-IDF, TCP/IP stack, Wifi and Bluetooth before MicroPython can take its cut. It's possible we could recover more memory for modules that are not in use but I haven't investigated deeply enough to know what's available.

If you require more memory the easiest way forward today is to purchase an ESP32 with additional PSRAM. This can extend the RAM by 4MB at the cost of slightly slower runtime performance.

I'll try to find time to look into where we could potentially save some memory.

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: free memory 100KB

Post by jickster » Sun Sep 30, 2018 11:41 pm

mattyt wrote:
Sun Sep 30, 2018 11:39 pm

I'll try to find time to look into where we could potentially save some memory.

Code: Select all

import sys
dir()
sys.modules
That will show you which modules are loaded and taking up RAM

Post Reply