mirror of
https://github.com/pybricks/pybricks-api.git
synced 2026-07-27 19:57:02 +00:00
micropython: Add examples for memory stats.
This commit is contained in:
@@ -25,3 +25,24 @@ Using constants for efficiency
|
||||
|
||||
.. literalinclude::
|
||||
../../../examples/micropython/const.py
|
||||
|
||||
Checking free RAM
|
||||
******************************
|
||||
|
||||
.. literalinclude::
|
||||
../../../examples/micropython/memuse.py
|
||||
|
||||
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
|
||||
******************************
|
||||
|
||||
.. literalinclude::
|
||||
../../../examples/micropython/memstat.py
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
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)
|
||||
@@ -0,0 +1,4 @@
|
||||
from micropython import mem_info
|
||||
|
||||
# Print memory usage.
|
||||
mem_info()
|
||||
Reference in New Issue
Block a user