resizing a bytearray memory penalty #14090
Replies: 1 comment 1 reply
-
@kjm1102 I actually get MicroPython's GC is "imprecise" which means that it can have temporary false positives (but not false negatives) in it's "is this allocation still referenced" check. i.e. it can occasionally think an allocation is in-use for some additional period of time. This can be due to e.g. a pointer still being held in a CPU register. Compare to e.g. a reference counted GC, or one that uses a lot of additional memory and overhead to perfectly track references. As a result can make it difficult to make inferences from tests like this. But I promise you that resizing this way does work, and the blocks will eventually be reclaimed. FWIW, following on from https://github.com/orgs/micropython/discussions/14088 the "correct" way to resize a bytearray without copying is with slice assignment:
However while this does technically resize the bytearray it does not free the unused blocks back. There's a TODO in the code for this: https://github.com/micropython/micropython/blob/master/py/objarray.c#L517 I think it would be relatively easy to fix (perhaps this code was written before we implemented support for this in the GC). |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
If I shrink a byte array I incur a ram loss equal to the new size
this happens even if I move the resizing into a function so that it is 'out of scope' on return. I'd expect this result if I'd used R=Q[:18000] but it seems that, even with Q=Q[:18000], I'm gonna loose 18k rather than gain 2k. Since I'm a bit short of ram I'm wondering if there is a way around this?
Beta Was this translation helpful? Give feedback.
All reactions