constrained ram

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
KJM
Posts: 158
Joined: Sun Nov 18, 2018 10:53 pm
Location: Sydney AU

constrained ram

Post by KJM » Mon Jan 17, 2022 11:35 pm

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.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: constrained ram

Post by pythoncoder » Tue Jan 18, 2022 11:59 am

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.
Peter Hinch
Index to my micropython libraries.

KJM
Posts: 158
Joined: Sun Nov 18, 2018 10:53 pm
Location: Sydney AU

Re: constrained ram

Post by KJM » Wed Jan 19, 2022 6:32 am

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.

Post Reply