9.4. Compact Memory Pools

9.4.1. Overview

PikaPython has a built-in compact memory pool for small resource chips, which is not enabled by default.

Compact memory pooling can reduce memory fragmentation from the usual 20-30% to less than 5%.

Note] Compact memory pooling can slow down the operation speed.

9.4.2. Enabling method

Note that the kernel version must be at least v1.9.0.

9.4.2.1. Enable user configuration

Refer to the configuration document

9.4.2.2. Add configuration items

/* pika_config.h */
#define PIKA_POOL_ENABLE 1
#define PIKA_POOL_SIZE 0x1900

Where PIKA_POOL_ENABLE means open compact memory pool, PIKA_POOL_SIZE means the size of the memory pool, the memory pool pre-apply memory from heap, please make sure the heap can apply to that size.

Refer to bsp/stm32g030c8/Booter/pika_config.h

9.4.2.3. Memory pool initialization

Initialize the memory pool before pikaScriptInit() or newRootObj().

mem_pool_init();

Reference: bsp/stm32g030c8/Booter/main.c

9.4.3. Freeing the memory pool

If the memory pool needs to be freed, call

mem_pool_deinit();

9.5. Interrupting a running script

Calling pks_vm_exit() forces the interruption of a running script (also in a dead loop), which can be placed in the interrupt function.

[Note]

Requires kernel version not lower than v1.11.0.

After interrupting a running script, only the VM is exited, the root object can still be used and will not be freed, if you need to free memory, you should execute obj_deinit() on the root object.