How much memory is this stack of variables?

I have programs acting all wacky, and I haven’t figured out how to work my ESP Prog debugger yet, but I remember this causing problems in other scenarios.
How much memory am I tying up right here in just these dozen or so lines?

typedef struct struct_message
{
  int id;
  float temperature;
  float humidity;
  float pressure;
  float LPG;
  float CO;
  float smoke;
  float moisture;
  float UV;
  float CO2;
  float NH3;
  float NO2;
  float VOC;
  float TVOC;
  float H2;
  float EtOH;
  int readingId;
} struct_message;

Is it 17 x 4 bytes = 68? Is that a lot?

I think that with ESP you can call printf to output to the serial terminal. If this works for you, then:

printf("Float: %d, int: %d\n", sizeof(float), sizeof(int));

Cheers,
Norm.

…or even sizeof(struct_message) to see how big the compiler sees the whole structure. Don’t forget that the compiler can add padding between values to line them up on various word boundaries.
Susan

2 Likes

Thank you. I added it, but now I can’t upload anything.

Boo.

Thanks again,

If you are using the Arduino framework, then try this:

void setup() {
    Serial.begin(115200);
    Serial.print("Float: ");
    Serial.print(sizeof(float));
    Serial.print(" Int: ");
    Serial.print(sizeof(int));
}

void loop() {
    // Does nothing.
}

Then, in the Serial Monitor, set the baud rate to 115200 and see what appears.

Cheers,
Norm.

I used Aussie Susan’s line, and it says 68. 68 bytes, is that a lot? Is that enough to affect performance?

ESP32 has 4MB, but I know all the space has to be created and allocated in the C++.

Yes indeed, what @aussiesusan says is completely correct, and will include the padding bytes, if any, between fields to keep things aligned for best efficiency. My bad, I didn’t notice it was a struct! :cry:

68 bytes is not normally a lot, no. It might be on an Arduino Uno which only has 2KB Static RAM for variables, but if the ESP32 has 4MB (Static RAM) then you are hardly scratching the surface.

What does the stats at the end of a compilation say about the memory usage? That should give you a clue about how much (percentage) of Static and Flash RAM have been used. Your program would normally go into the Flash RAM and variables in the Static RAM.

I assume that the ESP32 has both Static RAM and Flash RAM? I haven’t checked!

Cheers,
Norm.

“Wrote 773728 bytes (490900 compressed) at 0x00010000 in 11.3 seconds (effective 549.2 kbit/s)…
Hash of data verified.”

So 490,900 bytes. I know I’ve seen documentation that says it’s 4MB flash memory.

  • 520 KB of SRAM, 448 KB of ROM and 16 KB of RTC SRAM.
    タイトルなし

This program works fine, and it’s only 379 lines. But the struct is in the two senders that don’t work fine. They’re considerably longer.

Sender is less memory: 445590. Actually about 100 lines shorter…

Whether the size of a struct is large or small really depends on how it is used. If you have a (say) 1000 array of those structs then that is about 68K bytes. If you are simply sending one instance over WiFi then it is (probably) trivial.
However what you mention above seems more related to downloading your program and that would not necessarily be the data structures as they would normally be created in RAM as part of the C runtime startup code.
Therefore you need to show us how you are USING that struct for us to help further.
Susan

Right. In this case, the struct moves around. So it needs space along the way.
Looks like any struct will be 68K. The same value was reported for a 4-member struct and a 13-member struct, all of them floats, some of them going to four decimal places.

Is there a ‘sizeOf’ I can place that would let me see that struct working through the program as actual variables?

Humidity, Temperature, Pressure, LPG, CO, NH3, etc. are all in the struct, but the struct is manifest in Loop when we grab values from the sensors and then arrange them for broadcast.

I’m not worried about physical memory, there’s plenty of address space available. I’m worried about the capacity of the processor to juggle so many values simultaneously- is there room in the pipe? I have no background in hardware (or software), so this may be a delightful amateur question.

I get the feeling that there are some basic misunderstandings about how variables are used and stored in C going on here.
For a start - what do you mean by “the struct moves around”?
Also the ‘sizeof’ operator can be passed an argument of either a type (as in the examples above) or a variable.
It seems to me that you are writing a program that logs weather related data and then sends a data packet to some other device.
I am actually doing something almost identical in that I have a ESP32-C3 connected to a BME680 that I sample every so often and then send via WiFi to a central server.
The pseudo code for my device looks like:

while(1)
{
struct data_packet sample; // Create a variable to hold the sample data
Read_BME680_data( &sample); // Fill the variable elements with the required values
Write_To_WiFI_Server( &sample); // Send of the data
sleep(30000); // Wait for 5 minutes and then carry on
}

In this case you only create 1 instance of the struct (that is 68 bytes in your case) and it is recreated each time through the loop. (My pseudo code create a new variable each time on the stack. You can also place the variable definition outside the loop and simply reuse the memory location.)
Total RAM used by this part of the code for the variables is 68 bytes.
If by 'the struct moves around" you mean that it is referenced elsewhere (in the above I pass the address of the struct to the code that fills in the values and also the code that sends the packet via WiFi - the data itself does not ‘move’ and always stays in the same location in RAM) then I hope you can see that this is not the case.
My reference to 68K makes the clear assumption that you are creating an array that has 1000 elements but as you have not shared any code (even a snipped of the way you use the struct) I have no way of knowing if this really is the case - it is something I made up by way of example.
I take it your reference to the ‘pipe’ means the actual way you are "broadcast"ing (to use your term) your data. If this really is a pipe (and that is a very specific mechanism for data transfer between processes in the same computer) then as long as you are reading the data from the pipe in another process, then you will normally not have an issue. Of course if you stop reading the pipe then it will fill up, not because of the size of the data you are pushing through it, but because you are writing to it but not reading from it at the other end.
Susan