question about GC

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
User avatar
mfwebn
Posts: 2
Joined: Mon Mar 05, 2018 2:47 am
Contact:

question about GC

Post by mfwebn » Tue Apr 30, 2019 12:32 pm

If I set gc.threshold, will the GC release the memory I am using for m_malloc in modules that are not related to MicroPython?

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

Re: question about GC

Post by dhylands » Tue Apr 30, 2019 3:18 pm

Memory allocated using m_malloc will be freed when the GC determines that nobody is using the memory.

Setting gc.threshold just changes when garbage collections occur.

If your native code allocates memory the pointers to that memory needs to be stored someplace that the garbage collector can see. If you're allocating an object and returning it to python, then it will be stored inside a python object.

If however, the memory doesn't get passed back as (or in) a python object, then you need to ensure that your pointer is stores someplace in the "root pointers". If you search the forum for "root pointers" you'll find lots of discussions about them.

WRR
Posts: 6
Joined: Wed Jan 09, 2019 8:33 pm

Re: question about GC

Post by WRR » Thu May 02, 2019 4:10 pm

The documentation has a good explanation of how 'gc.threshold' works - I could be misreading it, but it sounds like it tells the allocator to run GC once N more bytes have been allocated after the `gc.threshold(N)` call.

http://docs.micropython.org/en/v1.9.3/p ... .threshold

If you're asking about how the garbage collector decides what memory to free, there's a good explanation on the Github wiki:

https://github.com/micropython/micropyt ... ry-Manager

As I understand it, the garbage collector uses a 'mark and sweep' approach; it looks at objects that you or the VM mark as 'root pointers', and marks those areas of the heap as used. If it sees blocks of memory that look like pointers to other areas of memory inside of the Python heap, it follows those and also marks those areas as used. Finally, it frees anything that is not used.

I don't think it can free memory outside of the MicroPython heap, but if you use MicroPython's memory allocation methods then you probably should make the garbage collector aware of that.

You can call `gc_collect_root( ( void** )your_struct, struct_length );` between `gc_collect_start();` and `gc_collect_end();` to protect extra "root pointers" from being freed too early, but I have to admit that I'm not too clear on how the second 'length' parameter is determined; usually I just put 1. Maybe someone else can explain more or correct me if I'm wrong.

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

Re: question about GC

Post by dhylands » Thu May 02, 2019 4:53 pm

dhylands wrote:
Tue Apr 30, 2019 3:18 pm
You can call `gc_collect_root( ( void** )your_struct, struct_length );` between `gc_collect_start();` and `gc_collect_end();` to protect extra "root pointers" from being freed too early, but I have to admit that I'm not too clear on how the second 'length' parameter is determined; usually I just put 1. Maybe someone else can explain more or correct me if I'm wrong.
The length that you provide to gc_collect_root is the number of pointer sized objects that the GC should look at to find root pointers. For the pyboard, the size of a pointer is 4 bytes, so passing in a 1 will cause the heap to look for a single pointer, whose address is stored at the first argument. You can see this by looking at this code:
https://github.com/micropython/micropyt ... lect.c#L54 which passes in the address of the stack and the size of the stack (in pointer sized units) currently being used.

Post Reply