Why each instance of my class consume 20byte of memory at least?
Posted: Wed Aug 15, 2018 9:17 am
Hi,
I'm a newbie of Arduino-esp32, I used to use Arduino.
I succeed to run the blink example on my ESP32-WROOM-32 dev module.
And I began to test other functions.
I make a simple RAM test to figure out the difference in RAM usage between ESP32 and Arduino.
I found creating a new instance of a class will consume 20byte of RAM at least.
-----
An empty class takes 20byte.
A class with a 1byte variable takes 20byte.
A class with a uint16_t variable takes 20byte.
A class with a uint32_t variable takes 20byte.
A class with 3 uint32_t variables takes 28byte.
-----
It seems that a Class will at least consume 20byte no matter what it contains.
Here is my simple testing sketch:
Here is the log of serial monitor
Hope someone could explain why an instance of an empty class will take 20byte.
And is there a way to make it smaller?
I'm a newbie of Arduino-esp32, I used to use Arduino.
I succeed to run the blink example on my ESP32-WROOM-32 dev module.
And I began to test other functions.
I make a simple RAM test to figure out the difference in RAM usage between ESP32 and Arduino.
I found creating a new instance of a class will consume 20byte of RAM at least.
-----
An empty class takes 20byte.
A class with a 1byte variable takes 20byte.
A class with a uint16_t variable takes 20byte.
A class with a uint32_t variable takes 20byte.
A class with 3 uint32_t variables takes 28byte.
-----
It seems that a Class will at least consume 20byte no matter what it contains.
Here is my simple testing sketch:
Code: Select all
#define LEDPIN 2
bool isOn = true;
// TestClass
class myClass
{
public:
myClass(){};
~myClass(){};
//uint8_t content = 0;
//uint16_t content = 0;
//uint32_t content = 0;
};
// GetFreeRAM
#ifdef ESP32
// nothing
#else
#ifdef __arm__
// should use uinstd.h to define sbrk but Due causes a conflict
extern "C" char* sbrk(int incr);
#else // __ARM__
extern char *__brkval;
#endif // __arm__
#endif // ESP32
int getFreeRam()
{
#ifdef ESP32
return (int)ESP.getFreeHeap();
#else
char top;
#ifdef __arm__
return (int)(&top - reinterpret_cast<char*>(sbrk(0)));
#elif defined(CORE_TEENSY) || (ARDUINO > 103 && ARDUINO != 151)
return &top - __brkval;
#else // __arm__
return __brkval ? &top - __brkval : &top - __malloc_heap_start;
#endif // __arm__
#endif //ESP32
}
uint32_t count;
uint32_t count_max = 60000;
myClass **classArray;
void setup()
{
Serial.begin(115200);
pinMode(LEDPIN,OUTPUT);
Serial.println(F("Setup"));
count = 1;
}
void loop()
{
// Check count max
if (count > count_max)
{
count = count_max;
}
// Report count
Serial.print("Count: ");
Serial.println(count);
// Report Basic Size
Serial.print("sizeof(myClass*): ");
Serial.println(sizeof(myClass*));
Serial.print("sizeof(myClass): ");
Serial.println(sizeof(myClass));
// Create the class array
classArray = (myClass**)malloc(count * sizeof(myClass*));
for (uint32_t i = 0; i < count; i++)
{
classArray[i] = new myClass();
}
// Report FreeRam
Serial.print("CreateFreeRam: ");
Serial.println(getFreeRam());
digitalWrite(LEDPIN, isOn);
isOn = !isOn;
// Clear the class array
for (uint32_t i = 0; i < count; i++)
{
delete(classArray[i]);
}
free(classArray);
// Report FreeRam
Serial.print("ResetFreeRam: ");
Serial.println(getFreeRam());
// Loop Delay
delay(50);
count+=1;
}
Code: Select all
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:952
load:0x40078000,len:6084
load:0x40080000,len:7936
entry 0x40080310
Setup
Count: 1
sizeof(myClass*): 4
sizeof(myClass): 4
CreateFreeRam: 292048
ResetFreeRam: 292088
Count: 2
sizeof(myClass*): 4
sizeof(myClass): 4
CreateFreeRam: 292024
ResetFreeRam: 292088
Count: 3
sizeof(myClass*): 4
sizeof(myClass): 4
CreateFreeRam: 292000
ResetFreeRam: 292088
Count: 4
sizeof(myClass*): 4
sizeof(myClass): 4
CreateFreeRam: 291976
ResetFreeRam: 292088
Count: 5
sizeof(myClass*): 4
sizeof(myClass): 4
CreateFreeRam: 291952
ResetFreeRam: 292088
Count: 6
sizeof(myClass*): 4
sizeof(myClass): 4
CreateFreeRam: 291928
ResetFreeRam: 292088
Count: 7
sizeof(myClass*): 4
sizeof(myClass): 4
CreateFreeRam: 291904
ResetFreeRam: 292088
Count: 8
sizeof(myClass*): 4
sizeof(myClass): 4
CreateFreeRam: 291880
ResetFreeRam: 292088
Count: 9
sizeof(myClass*): 4
sizeof(myClass): 4
CreateFreeRam: 291856
ResetFreeRam: 292088
Count: 10
sizeof(myClass*): 4
sizeof(myClass): 4
CreateFreeRam: 291832
ResetFreeRam: 292088
And is there a way to make it smaller?