More Questions About Memory Reported by Compiler

Sorry but I have more questions regarding the memory reported being used by the compiler...

In my search to reduce the bytes my sketch code uses, I was deleting lines of code and then compiling to see how much memory the sketch uses.

I was surprised to see that deleting an INCLUDE made almost no difference in memory usage. Deleting an 50 element array also didn't seem to make much difference. Reducing text in Serial.print statements made a big difference but changing a Serial.print("xx") to Serial.print(F("xx")) actually uses more bytes although I thought the "F" function suppose to help with memory.

Lastly, as mention in a previous post, the reported bytes used by the program does not include the bootloader which takes up x bytes of the 32k (for the Uno).

JC

joecatch:
deleting an INCLUDE made almost no difference in memory usage. Deleting an 50 element array also didn't seem to make much difference. Reducing text in Serial.print statements made a big difference but changing a Serial.print("xx") to Serial.print(F("xx")) actually uses more bytes although I thought the "F" function suppose to help with memory.

Lastly, as mention in a previous post, the reported bytes used by the program does not include the bootloader which takes up x bytes of the 32k (for the Uno).

I'm not surprised by any of that. The compiler will attempt to optimise out code and data that is unused, and if you can remove the #includes and data definitions without changing any other code then they were obviously unused. Using the F() macros will reduce the SRAM consumption at runtime but at the cost of adding a little additional code to retrieve the string from flash memory.

Lastly, as mention in a previous post, the reported bytes used by the program does not include the bootloader which takes up x bytes of the 32k

You're not compiling the bootloader, so it isn't surprising the IDE doesn't report it,

There are 2 types of memory available on Arduinos, and most have a 3rd type:

  • Flash memory -- This is the memory used to hold the bootloader, the program instructions, and any constant data declarations used by the PROGMEM/F macros. This memory is read-only, and can only be changed when you are re-programming the chip. The AVR chip has what is known as a Harvard architecture (from the Harvard Mark 1 computer), where the read/write memory and the instruction memory are different. The Arm processors (like Due) have the flash memory and SRAM in the same address space (von Neumann or modified Harvard architecture), and the PROGMEM macros are not needed, just use the const type modifier.
  • SRAM memory -- This is the read/write memory used for program variables. When the IDE reports the memory size, it is reporting the flash memory size, and so changing array sizes won't show a difference. If you don't use PROGMEM on an AVR processor, strings go into SRAM.
  • EEPROM memory -- This is memory that you can read from or write to via function calls. Unlike normal SRAM memory, this memory is not cleared when the machine loses power. Normal variables don't go here, but you can use it for recording state, such as a data recorder. The Due does not have EEPROM.

In terms of include not making a difference, the Arduino IDE adds special compiler and linker options, so that any function that is not referenced from the main program is deleted.

With the avr's you have two areas of memory one for program and one for data. The compiler only reports code space usage.

So deleting an array saves data space which is not reported,

using the F() macro saves data (not reported) but uses more code space which is reported.

Marrk

Thank you guys for clearing that up.

I searched and found a couple of web pages listing some code to display SRAM usage. Will try those tonight and see how much SRAM I have left. (Arduino Playground - AvailableMemory

JC

As I stated in another thread, I downloaded some code which shows SRAM usage. My sketch started at 404 and went down to 187 after it loop a few times. Below 200 the sketch would start to act up like either hang or be unable to open the SD card. I corrected an error in the program where I was reading from an array and now the SRAM drops to 387 and stays there and the program is stable.

JC