chrisbrooks
Member
I'm seeing some weird behavior when I use malloc/free to work with dynamic memory on my Teensy 4.1. When I free memory, it doesn't always get aggregated with adjacent chunks, which prevents me from allocating a large chunk.
Is this expected behavior?
Here is a minimal example:
And here is the serial output:
Here's what I see happening:
Thanks for your help!
Details:
* Product: Teensy 4.1
* Arduino IDE 2.3.6 with Teensyduino 1.59.0
* Windows 11 Pro
Is this expected behavior?
Here is a minimal example:
C++:
#include "Arduino.h"
#include <memory>
struct BigStruct
{
uint8_t data[1024*64];
};
struct HugeStruct
{
uint8_t data[1024*492];
};
int freeram() {
// From https://forum.pjrc.com/index.php?threads/how-to-display-free-ram.33443/post-275013
extern unsigned long _heap_end;
extern char *__brkval;
return (char *)&_heap_end - __brkval;
}
bool test_malloc_free_BigStruct(void)
{
BigStruct *A = (BigStruct *) malloc(sizeof(BigStruct));
bool success = (bool) A;
free(A);
return success;
}
bool test_malloc_free_two_BigStructs(void)
{
BigStruct *B = (BigStruct *) malloc(sizeof(BigStruct));
bool success = (bool) B;
success &= test_malloc_free_BigStruct();
free(B);
return success;
}
bool test_malloc_free_HugeStruct(void)
{
HugeStruct *A = (HugeStruct *) malloc(sizeof(HugeStruct));
bool success = (bool) A;
free(A);
return success;
}
void print_freeram(void)
{
Serial.print("freeram(): ");
Serial.println(freeram());
}
void print_testresult(const char *testname, bool success)
{
Serial.print(testname);
Serial.println(success ? "successful": "failed");
}
void setup() {
while (!Serial) {}
Serial.println("teensy_malloc_test");
Serial.println("==================");
if (CrashReport)
Serial.println(CrashReport);
// malloc/free a HugeStruct
print_freeram();
print_testresult("malloc/free a HugeStruct: ", test_malloc_free_HugeStruct());
// malloc/free a BigStruct
print_freeram();
print_testresult("malloc/free a BigStruct: ", test_malloc_free_BigStruct());
// malloc/free a HugeStruct
print_freeram();
print_testresult("malloc/free a HugeStruct: ", test_malloc_free_HugeStruct());
// malloc/free two BigStructs
print_freeram();
print_testresult("malloc/free two BigStructs: ", test_malloc_free_two_BigStructs());
// malloc/free a HugeStruct
print_freeram();
print_testresult("malloc/free a HugeStruct: ", test_malloc_free_HugeStruct());
print_freeram();
}
void loop() {
// Empty loop
}
And here is the serial output:
Code:
teensy_malloc_test
==================
freeram(): 511872
malloc/free a HugeStruct: successful
freeram(): 507904
malloc/free a BigStruct: successful
freeram(): 438272
malloc/free a HugeStruct: failed
freeram(): 438272
malloc/free two BigStructs: successful
freeram(): 507904
malloc/free a HugeStruct: successful
freeram(): 507904
Here's what I see happening:
- I successfully allocate and free a HugeStruct (one 492k chunk).
- I successfully allocate and free a BigStruct (one 64k chunk). (Note that "freeram" has gone down.)
- I fail to allocate a HugeStruct (492k).
- I successfully allocate and free two BigStructs (two 64k chunks). (Note that the "freeram" lost in step 2 has been recovered.)
- I successfully allocate a HugeStruct (492k).
Thanks for your help!
Details:
* Product: Teensy 4.1
* Arduino IDE 2.3.6 with Teensyduino 1.59.0
* Windows 11 Pro