Page 2 of 2

Re: Memory allocation errors with plenty of space

Posted: Tue Jun 19, 2018 7:25 pm
by kwiley
Hmmm. Ok. On a PyBoard v1.1, running MP v1.9.4., I'm seeing the behavior described above. Thanks.

Re: Memory allocation errors with plenty of space

Posted: Wed Jun 20, 2018 5:14 am
by pythoncoder
@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.

Re: Memory allocation errors with plenty of space

Posted: Wed Jun 20, 2018 10:58 am
by stijn
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.

Re: Memory allocation errors with plenty of space

Posted: Wed Jun 20, 2018 4:17 pm
by dhylands
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

Re: Memory allocation errors with plenty of space

Posted: Fri Jun 22, 2018 3:02 am
by timh
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
>>>

Re: Memory allocation errors with plenty of space

Posted: Fri Jun 22, 2018 4:22 am
by dhylands
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.

Re: Memory allocation errors with plenty of space

Posted: Fri Jun 22, 2018 7:37 am
by timh
Yes I figured that was the case. But it shows the memory view clearly is functioning on another platform (ESP32)