1.4 gc – Memory Management API Manual#

This module implements a subset of the CPython memory management module’s functionality. For more details, please refer to the original CPython documentation: gc.

On the K230, the following interfaces have been added to obtain memory information for the RT-Smart system:

  • sys_total: System total memory size.

  • sys_heap: Used for kernel application memory management.

  • sys_page: Used for user application memory management.

  • sys_mmz: Used for multimedia driver memory management, suitable for modules like Sensor, Display, etc.

1. Functions#

1.1 enable#

gc.enable()

Enables the automatic garbage collection mechanism.

1.2 disable#

gc.disable()

Disables the automatic garbage collection mechanism. While disabled, heap memory allocation is still possible, and garbage collection can be performed manually by calling gc.collect().

1.3 collect#

gc.collect()

Manually runs the garbage collection process to reclaim unused memory.

1.4 mem_alloc#

gc.mem_alloc()

Returns the number of bytes of currently allocated heap memory.

Difference from CPython

This feature is an extension of MicroPython and is not included in CPython.

1.5 mem_free#

gc.mem_free()

Returns the number of bytes of currently available heap memory. If the remaining heap memory cannot be determined, it returns -1.

Difference from CPython

This feature is an extension of MicroPython and is not included in CPython.

1.6 threshold#

gc.threshold([amount])

Sets or queries the allocation threshold for garbage collection. Garbage collection is usually triggered when heap memory is low. If a threshold is set, garbage collection will also be triggered after allocating more than the set amount of bytes. The amount parameter is usually less than the total heap size to trigger collection before the heap is exhausted, reducing memory fragmentation. The effectiveness of this value varies by application, and the optimal value needs to be adjusted according to the application scenario.

Without parameters, this function returns the current threshold setting. A return value of -1 indicates that the allocation threshold is disabled.

Difference from CPython

This function is an extension of MicroPython. CPython has a similar set_threshold() function, but due to differences in the garbage collection mechanism, its signature and semantics are different.

1.7 sys_total#

gc.sys_total()

Queries the system total memory size, in bytes.

1.8 sys_heap#

gc.sys_heap()

Queries the system heap memory usage, returning a tuple containing 3 elements representing total (total memory), free (available memory), and used (used memory), in bytes.

1.9 sys_page#

gc.sys_page()

Queries the system page memory usage, returning a tuple containing 3 elements representing total (total memory), free (available memory), and used (used memory), in bytes.

1.10 sys_mmz#

gc.sys_mmz()

Queries the system mmz memory usage, returning a tuple containing 3 elements representing total (total memory), free (available memory), and used (used memory), in bytes.

Comments list

Comments list

Comments
Log in