help with leaking memory management

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: help with leaking memory management

Post by pythoncoder » Tue Jan 26, 2016 4:23 pm

[deleted after brain revived]I was talking nonsense here, I'm afraid :oops:
Last edited by pythoncoder on Tue Jan 26, 2016 6:34 pm, edited 1 time in total.
Peter Hinch
Index to my micropython libraries.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: help with leaking memory management

Post by Roberthh » Tue Jan 26, 2016 6:15 pm

I think that's' what I have seen with a test program which constantly allocated an frees memory in a loop. In that loop, I printed the amount of free memory told by mem_free(). That went down until no more memory was available, and then went up again, and so on. The problem seems to arise, when the memory is already scattered with small allocated blocks, and then a larger one is requested. Then, gc.collect() seems not to be able to resolve this. Running gc.collect often seems to reduce the risk.

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

Re: help with leaking memory management

Post by pythoncoder » Tue Jan 26, 2016 6:36 pm

@Roberthh Yes, I've come across this a few times.
Peter Hinch
Index to my micropython libraries.

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

Re: help with leaking memory management

Post by dhylands » Tue Jan 26, 2016 6:45 pm

pythoncoder wrote:
dhylands wrote:@dhylands [...]Doing a gc collect often, in some circumstances can dramatically reduce heap fragmentation [...]
A further thought. When the VM attempts to allocate memory and the allocation fails, should it not trigger a collection and then retry? This would avoid the "out of memory" exception being thrown in such cases.

Issuing gc.collect() regularly would still be a good idea, but it strikes me that the above change would avoid needless runtime failures.
Currently that's exactly how the allocator works. If the allocation fails, it tries a gc and then retries the allocation.

But if the heap was badly fragmented then it won't help, since after the gc you wind up with a heap which has lots of tiny holes rather that one big free space.

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

Re: help with leaking memory management

Post by pythoncoder » Wed Jan 27, 2016 7:08 am

dhylands wrote:... if the heap was badly fragmented then it won't help, since after the gc you wind up with a heap which has lots of tiny holes rather that one big free space.
I realised that after posting. Call it a "senior moment" :oops:
Peter Hinch
Index to my micropython libraries.

User avatar
bmarkus
Posts: 111
Joined: Tue Oct 21, 2014 5:58 am

Re: help with leaking memory management

Post by bmarkus » Wed Jan 27, 2016 10:23 am

pythoncoder wrote:
dhylands wrote:... if the heap was badly fragmented then it won't help, since after the gc you wind up with a heap which has lots of tiny holes rather that one big free space.
I realised that after posting. Call it a "senior moment" :oops:
In such case is there a solution to merge them or the current GC implementation can't manage the situation and no way out in a running program except restart?
Tiny Core Linux (piCore) developer
HAM radio call: HA5DI (Béla)

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

Re: help with leaking memory management

Post by dhylands » Wed Jan 27, 2016 3:35 pm

The current GC can't move the blocks without also updating all of the pointers which point to the blocks.

The real problem is that in the current architecture, there is no way to distinguish a pointer containing the address of a block, and a collection of bytes that just happens to have the same value.

If one of your data structures happened to contain an integer (or just an array of bytes) which happened to coincide with the address of a block then the current GC will conservatively keep the block and prevent it from being freed.

mianos
Posts: 84
Joined: Sat Aug 22, 2015 6:42 am

Re: help with leaking memory management

Post by mianos » Wed Jan 27, 2016 7:56 pm

dhylands wrote:The current GC can't move the blocks without also updating all of the pointers which point to the blocks.

The real problem is that in the current architecture, there is no way to distinguish a pointer containing the address of a block, and a collection of bytes that just happens to have the same value.

If one of your data structures happened to contain an integer (or just an array of bytes) which happened to coincide with the address of a block then the current GC will conservatively keep the block and prevent it from being freed.
Obviously this is not the problem here. If doing a garbage collect means there is not a leak with millions of iterations there are no miss recognized pointers.
It also means there are not items being permanently allocated.
It also means the memory is not being fragmented by permanent user allocated variables as there are none. It is being fragmented by its own block pointers because they are never coalesced.

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

Re: help with leaking memory management

Post by dhylands » Wed Jan 27, 2016 8:20 pm

No -not the general problem, but this is why blocks can't be moved after they've been allocated.

mianos
Posts: 84
Joined: Sat Aug 22, 2015 6:42 am

Re: help with leaking memory management

Post by mianos » Wed Jan 27, 2016 9:06 pm

dhylands wrote:No -not the general problem, but this is why blocks can't be moved after they've been allocated.
Yep, 100% agree. Unless you have a extra level of indirection, which would mean a lot more work in C.

Post Reply