Page 1 of 1

constrained ram

Posted: Mon Jan 17, 2022 11:35 pm
by KJM
I've got a 29kb update text file in ram on a Lolin D32 board with 100k of ram. When I try to access the first 30 characters with update[:30] I get a memory error

Code: Select all

line 102 MemoryError: memory allocation failed, allocating 19694 bytes
Well that's mostly what happens, sometimes the repl just prints the first line of the program. But lack of ram seems to be the problem.

Is there a way to access the first 30 characters without running out of ram? I tried writing the file to flash (to preserve it) & then recycling the update variable with update=update[:30] but still got the memory error.

Re: constrained ram

Posted: Tue Jan 18, 2022 11:59 am
by pythoncoder
I think you need to provide more detail on how you are accessing the data.

In general storing data as a file in flash and accessing parts of it using Python file commands reduces RAM usage. For example you can allocate a buffer of (say) 2KiB and read the file into the buffer 2KiB at a time. That way, RAM use is constrained to 2KiB and allocation occurs once only.

Re: constrained ram

Posted: Wed Jan 19, 2022 6:32 am
by KJM
I wrote the update to flash Peter & then used readline() to access the first line for the info I needed. Why that uses less ram than the update[:30] or update.split slicing techniques I was trying to use before to select the front of the file is beyond me but it works, thnx.

This having to think about how much ram is free while upython coding is a pain, the sooner I can get my hands on some ESP32s with more ram the better.