Code: Select all
>>> import gc
>>> gc.enable()
>>>
>>>
>>>
>>>
>>>
>>>
>>> gc.mem_free()
FATAL: uncaught exception 804b09c
FATAL ERROR:
Thank you.
Code: Select all
>>> import gc
>>> gc.enable()
>>>
>>>
>>>
>>>
>>>
>>>
>>> gc.mem_free()
FATAL: uncaught exception 804b09c
FATAL ERROR:
Code: Select all
Micro Python v1.3.1-190-gb6ca397 on 2014-10-06; PYBv1.0 with STM32F405RG
Type "help()" for more information.
>>> import gc
>>> gc.enable()
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> gc.mem_free()
FATAL: uncaught exception 804ec64
FATAL ERROR:
Code: Select all
>>> gc.disable()
>>> x=1
FATAL: uncaught exception 200002d0
FATAL ERROR:
Code: Select all
import pyb
pyb.info()
Code: Select all
import gc
# low memory here, e.g. 28000 bytes free
pyb.info()
gc.collect()
pyb.info()
# freed memory, e.g. 98000 bytes free
In normal usage the garbage collector is always enabled, and it collects memory automatically whenever it tries to allocate memory and finds that there isn't enough free. This is true even if you don't explicitly import gc or call gc.enable(). (The garbage collector is also responsible for allocating memory, which is why you see these failures when it is disabled and a function tries to allocate memory.)mosi wrote:Does it mean the garbage collector should be active from the start?
Code: Select all
def consume_memory():
x = 0
for i in range(10000):
x = x + 1.0
print(x)
Code: Select all
>>> gc.mem_free()
42560
>>> consume_memory()
10000.0
>>> gc.mem_free()
25504
>>> consume_memory()
10000.0
>>> gc.mem_free()
8448
>>> consume_memory()
10000.0
>>> gc.mem_free()
92448
>>>