Memory use when accessing array containing > 30-bit values #10475
-
I spent quite a long time pinning down why the Neopixel driver I was using was occasionally garbage collecting mid-update, causing LED glitches. This shows the underlying cause, which is that accessing array members with values of 2**31 or more results in a heap allocation: import gc
import array
def test(arr):
before = gc.mem_free()
print(arr[0])
print(gc.mem_free()-before)
arr = array.array('I', [0])
arr[0] = 0x3FFFFFFF
test(arr)
arr[0] = 0x40000000
test(arr) Produces (on the rp2 port, at least):
I've now found a better way around the original problem (see #10465), but I'm curious to know what's going on here, and if there are better ways to work with full 32-bit numbers. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 8 replies
-
On 32-bit ports that use object representation A (most ports), integers between -230 and 230-1 are "small integers" and are stored directly in the object handle, ie they don't need any heap memory. Integers outside this range are "big integers" and require heap memory (and more memory the larger the value). Ways around this:
|
Beta Was this translation helpful? Give feedback.
On 32-bit ports that use object representation A (most ports), integers between -230 and 230-1 are "small integers" and are stored directly in the object handle, ie they don't need any heap memory. Integers outside this range are "big integers" and require heap memory (and more memory the larger the value).
Ways around this:
array('H')
and 16-bit numbers only.@micropython.viper
functions.