Memory allocation errors with plenty of space
Re: Memory allocation errors with plenty of space
Hmmm. Ok. On a PyBoard v1.1, running MP v1.9.4., I'm seeing the behavior described above. Thanks.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Memory allocation errors with plenty of space
@kwiley I'm not.
memoryview works as expected as far as I can see.
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
>>>
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Memory allocation errors with plenty of space
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
Try this: I get this output (in my pyboard): and under linux I get:
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')
Code: Select all
>>> import memvw
allocating a took 10032 bytes
allocating b took 10048 bytes
allocating c took 32 bytes
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
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
>>>
>> 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
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.
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
Yes I figured that was the case. But it shows the memory view clearly is functioning on another platform (ESP32)