Heap Overflow Not Caught

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Loxy618
Posts: 21
Joined: Wed Apr 24, 2019 2:01 am

Heap Overflow Not Caught

Post by Loxy618 » Wed Mar 31, 2021 4:39 pm

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();
	

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Heap Overflow Not Caught

Post by pythoncoder » Wed Mar 31, 2021 5:15 pm

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.
Peter Hinch
Index to my micropython libraries.

Loxy618
Posts: 21
Joined: Wed Apr 24, 2019 2:01 am

Re: Heap Overflow Not Caught

Post by Loxy618 » Wed Mar 31, 2021 5:24 pm

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?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Heap Overflow Not Caught

Post by dhylands » 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).

cduran
Posts: 80
Joined: Thu Mar 17, 2016 4:52 pm

Re: Heap Overflow Not Caught

Post by cduran » Thu Apr 01, 2021 1:28 pm

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).

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Heap Overflow Not Caught

Post by dhylands » Thu Apr 01, 2021 2:13 pm

Looking at the code, it does not appear that there is a compile time setting for the gc threshold.

Post Reply