Is there a way to handle heap memory fragmentation in AVR/Arduino microcontrollers? -
i've been searching few days without luck.
heap memory fragmentation result of using malloc() , free() heavily in microcontrollers/arduino.
if using them unavoidable, how can defragment heap every , make sure next malloc() call find contiguous memory allocate?
create pool of fixed-size memory chunks. when need amount of memory, allocate 1 of chunks pool , use portion need. when you're done it, return chunk pool. pool never fragment because chunks same size. , if chunks available they're right size. you'll need couple bytes , second you'll think you're being wasteful tying entire chunck. you'll remember you're avoiding memory fragmentation , you'll feel better it.
if size of allocations vary 1 fixed-size memory pool might wasteful. can make 2 or 3 fixed-size memory pools small, medium, , large chunks. make sure return chunks same pool obtained them from.
use queue or linked list organize chunks in pool. chunks removed queue/list when they're allocated. , they're returned queue/list when they're freed.
Comments
Post a Comment