Page 1 of 1

Heap Overflow Not Caught

Posted: Wed Mar 31, 2021 4:39 pm
by Loxy618
Hello,
I am running a simple script to stress test my code.

Code: Select all

i = 0
while i<100:
  #print heap stats here
  j = [1]*i*100
  i+=1
Each iteration thru the loop I print out the heap stats which are as follows until the devices hangs
Used: 992 Free: 31520 T
Used: 1280 Free: 31232
Used: 2304 Free: 30208
Used: 4128 Free: 28384
Used: 6752 Free: 25760
Used: 10176 Free: 22336
Used: 14432 Free: 18080
Used: 19488 Free: 13024
Used: 25344 Free: 7168
Used: 32000 Free: 512
Used: 14496 Free: 18016
Used: 22784 Free: 9728
Used: 17696 Free: 14816
Used: 10464 Free: 22048
Used: 21184 Free: 11328

To me, this shows that the heap is overflowing after the 10th iteration. Is there a reason MP isn't catching this and throwing an exception?

The way my code sets up the heap is

Code: Select all

mp_stack_set_top(stack_top);
gc_init(heap, heap + sizeof(heap));
mp_init();
	

Re: Heap Overflow Not Caught

Posted: Wed Mar 31, 2021 5:15 pm
by pythoncoder
I can't comment on your C code, but the figures may be being confused by the fact that GC will occur at some points as the script progresses. I tried your script on a Pyboard running standard firmware. If I issue gc.collect() on each iteration the numbers become very deterministic.

Re: Heap Overflow Not Caught

Posted: Wed Mar 31, 2021 5:24 pm
by Loxy618
Thanks for the response. I agree with you however is there a feature built into MP to auto detect when the heap will overflow and throw an exception that can be more elegantly handled? Or, is it up to the script writer to call gc.collect throughout to ensure that the heap doesn't overflow?

Re: Heap Overflow Not Caught

Posted: Thu Apr 01, 2021 1:58 am
by dhylands
That's the way that the GC works. When the heap runs out, it triggers a garbage collection, which frees all of the unused memory.

You can use the threshold feature http://docs.micropython.org/en/latest/l ... .threshold to control when this automatic collection occurs, or you can manually call gc.collect.

Throwing an exception only occurs when not enough memory can be freed up to satisfy the allocation (in which case you'll get a MemoryError).

Re: Heap Overflow Not Caught

Posted: Thu Apr 01, 2021 1:28 pm
by cduran
Is there a compile time setting to set the threshold for the GC?
dhylands wrote:
Thu Apr 01, 2021 1:58 am
That's the way that the GC works. When the heap runs out, it triggers a garbage collection, which frees all of the unused memory.

You can use the threshold feature http://docs.micropython.org/en/latest/l ... .threshold to control when this automatic collection occurs, or you can manually call gc.collect.

Throwing an exception only occurs when not enough memory can be freed up to satisfy the allocation (in which case you'll get a MemoryError).

Re: Heap Overflow Not Caught

Posted: Thu Apr 01, 2021 2:13 pm
by dhylands
Looking at the code, it does not appear that there is a compile time setting for the gc threshold.