How to determine object size? #9945
-
Does MicroPython have a method to determine object size like |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
If your goal is to understand how much RAM different representations take up, then using def make_timetable():
...
return obj
def compute_alloc_diff():
gc.collect()
before = gc.mem_alloc()
x = make_timetable()
gc.collect()
print(gc.mem_alloc() - before) Note that Some caveats though:
|
Beta Was this translation helpful? Give feedback.
sys.getsizeof
is not implemented in MicroPython. Internally, the heap does know the allocated size of a given object, but many objects have multiple layers of allocations (e.g. a dictionary object contains a pointer to its table, which contains pointers to keys and values, etc).getsizeof
works in CPython with runtime support - builtin types must implement this to report their size. This would add a lot of code size, so is unlikely to ever be included in standard MicroPython firmware.If your goal is to understand how much RAM different representations take up, then using
gc.mem_alloc()
to compute a difference might be more useful.