micropython – MicroPython internals

Access and control MicroPython internals.

const(value) Any

Declares the value as a constant, which makes your code more efficient.

To reduce memory usage further, prefix its name with an underscore (_ORANGES). This constant can only be used within the same file.

If you want to import the value from another module, use a name without an underscore (APPLES). This uses a bit more memory.

Parameters

value (int or float or str or tuple) – The literal to be made constant.

Returns

The constant value.

heap_lock()

Locks the heap. When locked, no memory allocation can occur. A MemoryError will be raised if any heap allocation is attempted.

heap_unlock() int

Unlocks the heap. Memory allocation is now allowed again.

If heap_lock() was called multiple times, heap_unlock() must be called the same number of times to make the heap available again.

Returns

The lock depth after unlocking. It is 0 once it is unlocked.

kbd_intr(chr)

Sets the character that triggers a KeyboardInterrupt exception when you type it in the input window. By default it is set to 3, which corresponds to pressing Ctrl C.

Parameters

chr (int) – Character that should raise the KeyboardInterrupt. Choose -1 to disable this feature.

mem_info()
mem_info(verbose) None

Prints information about stack and heap memory usage.

Parameters

verbose – If any value is given, it also prints out the entire heap. This indicates which blocks are used and which are free.

opt_level() int
opt_level(level: int) None

Sets the optimization level for code compiled on the hub:

  1. Assertion statements are enabled. The built-in __debug__ variable is True. Script line numbers are saved, so they can be reported when an Exception occurs.

  2. Assertions are ignored and __debug__ is False. Script line numbers are saved.

  3. Assertions are ignored and __debug__ is False. Script line numbers are saved.

  4. Assertions are ignored and __debug__ is False. Script line numbers are not saved.

This applies only to code that you run in the REPL, because regular scripts are already compiled before they are sent to the hub.

Parameters

level (int) – The level to be set.

Returns

If no argument is given, this returns the current optimization level.

qstr_info()
qstr_info(verbose) None

Prints how many strings are interned and how much RAM they use.

MicroPython uses string interning to save both RAM and ROM. This avoids having to store duplicate copies of the same string.

Parameters

verbose – If any value is given, it also prints out the names of all RAM-interned strings.

stack_use() int

Checks the amount of stack that is being used. This can be used to compute differences in stack usage at different points in a script.

Returns

The amount of stack in use.

Examples

Using constants for efficiency

from micropython import const

# This value can be used here. Other files can import it too.
APPLES = const(123)

# These values can only be used within this file.
_ORANGES = const(1 << 8)
_BANANAS = const(789 + _ORANGES)

# You can read the constants as normal values. The compiler
# will just insert the numeric values for you.
fruit = APPLES + _ORANGES + _BANANAS
print(fruit)

Checking free RAM

from micropython import mem_info

# Print memory usage.
mem_info()

This prints information in the format shown below. In this example for the SPIKE Prime Hub, there are 257696 bytes (251 KB) worth of memory remaining for the variables in your code.

stack: 372 out of 40184
GC: total: 258048, used: 352, free: 257696
No. of 1-blocks: 4, 2-blocks: 2, max blk sz: 8, max free sz: 16103

Getting more memory statistics

from micropython import const, opt_level, mem_info, qstr_info, stack_use

# Get stack at start.
stack_start = stack_use()

# Print REPL compiler optimization level.
print("level", opt_level())

# Print memory usage.
mem_info()

# Print memory usage and a memory map.
mem_info(True)

# Print interned string information.
qstr_info()

# Print interned string information and their names.
APPLES = const(123)
_ORANGES = const(456)
qstr_info(True)


def test_stack():
    return stack_use()


# Check the stack.
print("Stack diff: ", test_stack() - stack_start)