QUERY · ISSUE
memory malloc error on stm32f407
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
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
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.
@dpgeorge
thanks.
@dpgeorge I am also encountering the
allocation failed, allocating %u bytesissue. 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.
I split my <code.py> in two parts ( two units) . It solved