Memory allocation errors with plenty of space

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
kwiley
Posts: 140
Joined: Wed May 16, 2018 5:53 pm
Contact:

Re: Memory allocation errors with plenty of space

Post by kwiley » Tue Jun 19, 2018 7:25 pm

Hmmm. Ok. On a PyBoard v1.1, running MP v1.9.4., I'm seeing the behavior described above. Thanks.

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

Re: Memory allocation errors with plenty of space

Post by pythoncoder » Wed Jun 20, 2018 5:14 am

@kwiley I'm not.

Code: Select all

MicroPython v1.9.4-162-gb78ca32-dirty on 2018-06-15; PYBv1.1 with STM32F405RG
Type "help()" for more information.
>>> import micropython, gc
>>> a = bytearray(10000)
>>> gc.collect()
>>> micropython.mem_info()
stack: 468 out of 15360
GC: total: 102400, used: 19584, free: 82816
 No. of 1-blocks: 29, 2-blocks: 13, max blk sz: 625, max free sz: 5131
>>> c = memoryview(a)
>>> gc.collect()
>>> micropython.mem_info()
stack: 468 out of 15360
GC: total: 102400, used: 19680, free: 82720
 No. of 1-blocks: 31, 2-blocks: 15, max blk sz: 625, max free sz: 5131
>>> 
memoryview works as expected as far as I can see.
Peter Hinch
Index to my micropython libraries.

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Memory allocation errors with plenty of space

Post by stijn » Wed Jun 20, 2018 10:58 am

Yeah tried on linux and windows only (even without the intermediate gc.collect() calls) and looked at the source code and I don't really see how memoryview instantiation could cause a huge memory hit as it doesn't copy memory but just allocates some pointers and bookeeping variables.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Memory allocation errors with plenty of space

Post by dhylands » Wed Jun 20, 2018 4:17 pm

Try this:

Code: Select all

import gc

gc.collect()
mbase = gc.mem_alloc()
a = bytearray(10000)
ma = gc.mem_alloc()

b = a[:]
mb = gc.mem_alloc()

c = memoryview(a)
mc = gc.mem_alloc()

print('allocating a took', ma - mbase, 'bytes')
print('allocating b took', mb - ma, 'bytes')
print('allocating c took', mc - mb, 'bytes')
I get this output (in my pyboard):

Code: Select all

>>> import memvw
allocating a took 10032 bytes
allocating b took 10048 bytes
allocating c took 32 bytes
and under linux I get:

Code: Select all

allocating a took 10080 bytes
allocating b took 10112 bytes
allocating c took 64 bytes

timh
Posts: 2
Joined: Fri Jun 22, 2018 3:00 am

Re: Memory allocation errors with plenty of space

Post by timh » Fri Jun 22, 2018 3:02 am

Just for the hell of it. On a PyCOM FipY (ESP32)

>> import gc
>>>
>>> gc.collect()
>>> mbase = gc.mem_alloc()
>>> a = bytearray(10000)
>>> ma = gc.mem_alloc()
>>>
>>> b = a[:]
>>> mb = gc.mem_alloc()
>>>
>>> c = memoryview(a)
>>> mc = gc.mem_alloc()
>>>
>>> print('allocating a took', ma - mbase, 'bytes')
allocating a took 10224 bytes
>>> print('allocating b took', mb - ma, 'bytes')
allocating b took 10256 bytes
>>> print('allocating c took', mc - mb, 'bytes')
allocating c took 208 bytes
>>>

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Memory allocation errors with plenty of space

Post by dhylands » Fri Jun 22, 2018 4:22 am

You'll find that you get different results when you execute the code from a .py file rather than from the REPL.

This is because as you're typing text in the REPL, these lines are being stored in the history buffer which is allocated on the heap. So the numbers you're seeing are skewed because they include the text for the history lines.

timh
Posts: 2
Joined: Fri Jun 22, 2018 3:00 am

Re: Memory allocation errors with plenty of space

Post by timh » Fri Jun 22, 2018 7:37 am

Yes I figured that was the case. But it shows the memory view clearly is functioning on another platform (ESP32)

Post Reply