So I heard that an Arduino uno can only hold 32K bytes of storage though when i looked at one of my earlier projects the file had 23 .h and .cpp files attached to it. the Arduino file itself said it was only 21 KB but IRremoteInt.h was 20KB on its own. The .cpp files and .h files sum to 127KB but the arduino file is only 21KB. (the program ran just fine)Does the arduino program just have a much more effecient way of including those files or what do i need to be focusing on when it comes to the 32 - 30KB limit?
Source code doesn't go anywhere near the Arduino, so the sizes of the .ino/.cpp/.h is (mostly) irrelevant.
@davidvalen, your topic has been moved to a more suitable location on the forum.
The Arduino compiler and linker converts those files to machine code and data, which is stored on the Arduino. The result is much more compact than the source program files.
when considering data, you need to focus on the 1k of EEPROM and 1k of SRAM.
the SRAM is used for the stack and variables that can hold captured data and the EEPROM can be used for non-volatile storage.
but you might also consider the serial interface and throughput to off-load data
The size of code files is really not important. First off, they are compiled, and second, the compiler only includes binary code for functionality that is used. If you define a function in your sketch, but do not call it, the compiler does not generate code for it.
The compiled code goes into the Flash.
Note that by default, string contstants like when doing logging to Serial.print() goes on the heap, which lives in the 2K SRAM. To save space, you can instead use the F() macro:
Serial.println(F("Long and detailed log text"));
This puts the string into Flash, and saved precious SRAM.
The SRAM is used for global variables and call stack. When memory is tight, try to avoid functions with local variables like this:
void doSomething() {
byte buf[100];
}
When that function is called, it allocated 100 bytes in SRAM, on either the call stack or the heap (not sure which).
If you run out of SRAM, call stack and heap will "crash" into each other, and overwrite data, resulting in indeterminate behaviour.
![]()
Also note that different Arduino models have different amounts of memory. The atmega2560 has 8k of SRAM and i believe 256k of Flash.
Another honorable mention, regarding memory, particularly SRAM, is that various libraries consume memory. Using the SD-card library eats almost 1Kb of SRAM (for internal page buffers etc). I also found that the Wire library eats some 400+ bytes of SRAM.
Apart from libraries, your code has to be rather big and complex (many thousands of lines) to even fill up 32Kb of flash. My current project is about 3000 lines, and fills up about 60% of 32Kb flash, so far using only the SPI library, which consumes very little RAM.
your source code gets compiled and something different gets uploaded to your Arduino.
When you compile your sketch you get some information like:
Sketch uses 924 bytes (2%) of program storage space. Maximum is 32256 bytes.
Global variables use 9 bytes (0%) of dynamic memory, leaving 2039 bytes for local variables. Maximum is 2048 bytes.
and these two figures are relevant - not the pure size of your source code.
thank you i used these, took out some unnecessary print functions, and brought my dynamic storage from 65% to 13% using Serial.print(f()). that would not have been something I would have checked.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.