[Ovmsdev] RAM

Stephen Casner casner at acm.org
Thu Nov 2 09:28:07 HKT 2017


On Mon, 30 Oct 2017, Mark Webb-Johnson wrote:

> Firstly, remember that with memory debugging turned on (menuconfig,
> Components, OVMS, Developer, Enabled extended RAM memory allocation
> statistics), there is an extra overhead for each memory allocation.
> I tested a simple allocation of a 32 byte object, and that reduced
> memory by 56 bytes. So, seems to be 24 bytes for each allocation.
> Not sure what it is without the debugging turned on (as much harder
> to see the allocations), but it should be a lot less.

In release 2.1 of esp-idf, the overhead for each block allocated with
malloc or new when the memory debugging has NOT been turned on is the
following 8-byte header:

typedef struct A_BLOCK_LINK
{
    struct A_BLOCK_LINK *pxNextFreeBlock;   /*<< The next free block in the list. */
    int xBlockSize: 24;                     /*<< The size of the free block. */
    int xTag: 7;                            /*<< Tag of this region */
    int xAllocated: 1;                      /*<< 1 if allocated */
} BlockLink_t;

When the memory debugging is turned on there is an additional 12-byte
header and 4-byte trailer added which accounts for the total of 24
bytes of overhead that Mark observed:

typedef struct {
    unsigned int dog;
    TaskHandle_t task;
    unsigned int pc;
} block_head_t;

typedef struct {
    unsigned int dog;
} block_tail_t;

The "dog" words are set to 0x1A2B3C4D and are examined to look for
bounds overruns.  The task id is what we really need to keep track of
who's allocating memory.  I don't see the pc word being used in the
heap_regions_debug.c code now, but my guess is that it was keeping
track of where the allocation was done.  Hmmm, maybe I can save 4
bytes? [does a test compile] Yes! Removing that word has the following
savings.  Here is the memory usage with WiFi, MDNS, Telnet and Web
servers started, first before removing the "pc" word:

Free 8-bit 59884/245680, 32-bit 16620/43428, blocks dumped = 0
task=Housekeeping    total=  19400  46316  26140 change= +19400 +46316 +26140
task=main            total=  34980      0      0 change= +34980     +0     +0
task=IDLE            total=     36      0      0 change=    +36     +0     +0
task=IDLE            total=     36      0      0 change=    +36     +0     +0
task=ipc1            total=     36      0      0 change=    +36     +0     +0
task=ipc0            total=   9284      0      0 change=  +9284     +0     +0
task=eventTask       total=  44348  14292    668 change= +44348 +14292   +668
task=no task         total=   8784      0      0 change=  +8784     +0     +0
task=tiT             total=    336   1440      0 change=   +336  +1440     +0
task=wifi            total=      0   5896      0 change=     +0  +5896     +0
task=NetManTask      total=      0    836      0 change=     +0   +836     +0
task=TelnetServer    total=      0    424      0 change=     +0   +424     +0

Now after removing the "pc" word, a 4264-byte savings, which implies
1066 blocks allocated:

Free 8-bit 64148/245712, 32-bit 16648/43432, blocks dumped = 0
task=Housekeeping    total=  22076  42328  26120 change= +22076 +42328 +26120
task=main            total=  34824      0      0 change= +34824     +0     +0
task=IDLE            total=     32      0      0 change=    +32     +0     +0
task=IDLE            total=     32      0      0 change=    +32     +0     +0
task=ipc1            total=     32      0      0 change=    +32     +0     +0
task=ipc0            total=   9240      0      0 change=  +9240     +0     +0
task=eventTask       total=  42052  14204    664 change= +42052 +14204   +664
task=no task         total=   8656      0      0 change=  +8656     +0     +0
task=tiT             total=    312   1392      0 change=   +312  +1392     +0
task=wifi            total=      0   5864      0 change=     +0  +5864     +0
task=NetManTask      total=      0    832      0 change=     +0   +832     +0
task=TelnetServer    total=      0    328      0 change=     +0   +328     +0

I'll commit that change now.

                                                        -- Steve


More information about the OvmsDev mailing list