0

I have a fairly complicated sketch with about 1k lines of code. I should also say that I'm not a programmer by trade...

As I go over my code and try to make it more efficient, it'd be good to know how many global variables are in my code and whether/if all of them are necessary as global. So:

1 - Is there an easy way to count how many global variables you've used (again, imagine complex code by a non-coder), and

2 - Is there an easy way in the Arduino IDE to jump to it?

  • 1
    You can show us the code and we tell you how to make it more efficient. – Jot Dec 13 '18 at 5:20
  • @Jot - a thousand lines? – Jaromanda X Dec 13 '18 at 5:40
  • @JaromandaX, sure, a normal sketch for a arduino Uno can be 3000 lines, 1000 lines is not a large sketch. – Jot Dec 13 '18 at 7:38
  • If you compile your code ("verify"-button) you can see the size of the program, and the amount of memory used for global variable, at the bottom of the screen. – Gerben Dec 13 '18 at 10:39
  • globals aren't as evil on MCUs as they are on PCs; used right, they can save precious RAM. – dandavis Dec 13 '18 at 17:37
1

1 - The number of variables is not important, but the number of used memory (in bytes) is important. That is shown at the bottom of the Arduino IDE by the compiler when verifying or uploading a sketch.

2 - It is not possible to go to, for example, the largest global variable. You can use the search function to search for something.

For a small microcontroller, for example a Attiny or Atmega8, every byte that uses memory can be important.

For a Arduino Uno and larger, the optimization is done more globally.

To optimize for ram, look for the hungriest memory eaters:

  • For the Arduino Uno and other avr microcontrollers, the text with Serial.println can be put in PROGMEM with the F() macro.
  • Many String objects might need a lot of heap memory.
  • Arrays can get large quickly.
  • Arrays of large elements (float, struct) can get large even more quickly.
  • The SD library uses a lot of ram memory. There is a small version of the SD library.
  • Some graphical libraries for displays use a lot of memory.

Try to avoid to save a single byte, unless you check the resulting memory usage. The compiler can optimize code in a way that conflicts with someone trying to save a byte.

  • When an integer is changed into a byte, perhaps all the calculations are still done with integers and the compiler needs more memory to convert from byte to integer and back.
  • Perhaps that integer was only used in registers and did not use a memory location.
  • A 'const' integer uses probably no ram memory at all.

Final tip: Don't be afraid to upgrade to an other Arduino board with more memory. If you project uses almost all the memory, then it is not possible to add something to your project. When the project runs for a while, you might want to add an extra sensor or extra functionality.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.