← index #4968Issue #294
Off-topic · high · value 0.069
QUERY · ISSUE

memory malloc error on stm32f407

openby atouyuopened 2019-08-02updated 2019-08-07
port-stm32

print error log:

#Malloc: 0x20013560 16
#Malloc: 0x20013570 12
#Malloc: 0x200135A0 16
#Malloc: 0x200135B0 24
**_#Malloc bytes:4096 failed_**
#Testing 1kB...
Addr:0x20013B10
#Testing 1.25kB...
Addr:0x20013B10
#Testing 1.5kB...
Addr:0x20013B10
#Testing 2kB...
**_Addr:0x2001B5B0_**
#Testing 3kB...
###Malloc bytes:3072 failed

source code:

void *m_malloc(size_t num_bytes) {
    void *ptr = malloc(num_bytes);
    if (ptr == NULL && num_bytes != 0) {
        //PrintInfo("num_bytes : %d ", num_bytes);
        SEGGER_RTT_printf(0, "#Malloc bytes:%d failed\r\n", num_bytes);
        SEGGER_RTT_printf(0, "#Testing 1kB...\r\n");
        ptr = malloc(1024);
        if (!ptr) {
            SEGGER_RTT_printf(0, "###Malloc bytes:%d failed\r\n", 1024);
        } else {
            SEGGER_RTT_printf(0, "Addr:0x%08x\r\n", ptr);
            free(ptr);
        }
        SEGGER_RTT_printf(0, "#Testing 1.25kB...\r\n");
        ptr = malloc(1250);
        if (!ptr) {
            SEGGER_RTT_printf(0, "###Malloc bytes:%d failed\r\n", 1250);
        } else {
            free(ptr);
            SEGGER_RTT_printf(0, "Addr:0x%08x\r\n", ptr);
        }

        SEGGER_RTT_printf(0, "#Testing 1.5kB...\r\n");
        ptr = malloc(1536);
        if (!ptr) {
            SEGGER_RTT_printf(0, "###Malloc bytes:%d failed\r\n", 1536);
        } else {
            free(ptr);
            SEGGER_RTT_printf(0, "Addr:0x%08x\r\n", ptr);
        }
        SEGGER_RTT_printf(0, "#Testing 2kB...\r\n");
        ptr = malloc(1900);
        if (!ptr) {
            SEGGER_RTT_printf(0, "###Malloc bytes:%d failed\r\n", 1900);
        } else {
            free(ptr);
            SEGGER_RTT_printf(0, "Addr:0x%08x\r\n", ptr);
        }
        SEGGER_RTT_printf(0, "#Testing 3kB...\r\n");
        ptr = malloc(3072);
        if (!ptr) {
            SEGGER_RTT_printf(0, "###Malloc bytes:%d failed\r\n", 3072);
        } else {
            free(ptr);
            SEGGER_RTT_printf(0, "Addr:0x%08x\r\n", ptr);
        }

        m_malloc_fail(num_bytes);
    }
#if MICROPY_MEM_STATS
    MP_STATE_MEM(total_bytes_allocated) += num_bytes;
    MP_STATE_MEM(current_bytes_allocated) += num_bytes;
    UPDATE_PEAK();
#endif
    // PrintInfo("malloc %d : %p", num_bytes, ptr);
    SEGGER_RTT_printf(0, "#Malloc: 0x%08x %d\r\n", ptr, num_bytes);
    return ptr;
}

heap & stack address:

.heap           0x20012f94     0x4000 load address 0x080b2a44
                0x20012f94                . = ALIGN (0x4)
                0x20016f94                . = (. + _minimum_heap_size)
 *fill*         0x20012f94     0x4000 
                0x20016f94                . = ALIGN (0x4)

.stack          0x20016f94      0x800 load address 0x080b2a44
                0x20016f94                . = ALIGN (0x4)
                0x20017794                . = (. + _minimum_stack_size)
 *fill*         0x20016f94      0x800 
                0x20017794                . = ALIGN (0x4)
CANDIDATE · ISSUE

MemoryError: memory allocation failed, allocating %u bytes

closedby fernandomorseopened 2018-07-14updated 2019-06-04

When you try to run file (test.py) with size of 18447 bytes, the error is generated:

MemoryError: memory allocation failed, allocating %u bytes

gc.mem_free()
25520

When I split the same, it runs correctly. Is there any limitation of file size (py), or command that can bypass this situation

5 comments
dpgeorge · 2018-07-15

This doesn't sound like it is about any particular library and is a question more suited to the forum: http://forum.micropython.org

In short: if you pre-compile the .py to a .mpy using mpy-cross, or use frozen bytecode, it will help.

fernandomorse · 2018-07-15

@dpgeorge

thanks.

ARF1 · 2018-11-10

@dpgeorge I am also encountering the allocation failed, allocating %u bytes issue. My problem is not that the error itself is raised but the %u. Is this intended behaviour or should I open a separate issue for it?

dpgeorge · 2018-11-15

My problem is not that the error itself is raised but the %u. Is this intended behaviour or should I open a separate issue for it?

That is intended behaviour: if there is not enough memory to even allocate space to format the error message then it just gives you the unformatted string with the "%u" still in it.

amarildolacerda · 2019-06-04

I split my <code.py> in two parts ( two units) . It solved

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied