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!
MemoryError: memory allocation failed, allocating 1269848 bytes
Re: MemoryError: memory allocation failed, allocating 1269848 bytes
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.
Re: MemoryError: memory allocation failed, allocating 1269848 bytes
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.
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.