MemoryError: memory allocation failed, allocating 1269848 bytes

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
strombrg
Posts: 3
Joined: Sat Oct 14, 2017 5:27 pm

MemoryError: memory allocation failed, allocating 1269848 bytes

Post by strombrg » Sat Oct 14, 2017 5:44 pm

Hi folks.

For fun, I decided to do a prime number sieve using micropython on a Linux laptop.

It seems to work fine until I try to do too many primes. But "too many primes" for micropython is quite a bit smaller than "too many primes" for pypy or cpython on the same hardware.

The laptop I'm running it on has 8 Gig of physical RAM, and 8 Gig of swap space.

Sadly, micropython tracebacks when I try to allocate a small fraction of that, even if I gc.collect() frequently.

What am I doing wrong? Does micropython have issues with allocating large amounts of memory? I suppose that wouldn't be surprising, given that it mostly (?) targets resource-constrained environments.

The code is at:
http://stromberg.dnsalias.org/svn/sieve/trunk/sieve-mp
...and:
http://stromberg.dnsalias.org/svn/sieve ... _mod_mp.py

The traceback looks like:
dstromberg@zareason-strata7440 ~/src/home-svn/sieve/trunk $ ./sieve-mp 10,000,000
Creating bit array
Traceback (most recent call last):
File "./sieve-mp", line 55, in <module>
File "./sieve-mp", line 51, in main
File "./sieve-mp", line 19, in sieve
File "/home/dstromberg/src/home-svn/sieve/trunk/bits_mod_mp.py", line 41, in __init__
MemoryError: memory allocation failed, allocating 1269848 bytes

The version of micropython I'm using is from git on 2017-06-16.

The micropython executable is 64 bit, and my OS is a 64 bit Linux Mint 18.2.

Thanks!

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: MemoryError: memory allocation failed, allocating 1269848 bytes

Post by deshipu » Sun Oct 15, 2017 9:39 am

You are not doing anything wrong. The way MicroPython is designed, it allocates a static chunk of RAM at start, and only ever uses that. So it doesn't matter how much ram your computer has, MicroPython will only use as much as it was compiled for. This is a design decision and makes a lot of sense on embedded systems for which MicroPython was designed.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: MemoryError: memory allocation failed, allocating 1269848 bytes

Post by Roberthh » Sun Oct 15, 2017 10:41 am

You can set the heapsize on starting micropython on linux.

micropython -X heapsize=<n>[w][K|M]

So starting

micropython -X heapsize=2000M

will give you 2 Gbytes of heap size, and the OS has to take care for it, if your physical memory is smaller.

Post Reply