How to reduce sketch size?

I got an error that “Sketch uses 30,044 bytes (104%) of program storage space. Maximum is 28,672 bytes.”

I can't reduce function in sketch.I can't change ATmega32u4,now. The sketch has function of measuring sensor,logging on SD,connection with BLE module.

So please tell me hack.

Or,how to increase flash memory size?

I tried to change type of String to Char.

  • 5
    Impossible to answer without seeing the sketch in question. Some things (like long long data types) use a very large amount of program memory. You can't increase the flash memory size (on a particular processor). – Nick Gammon Dec 3 '17 at 5:47
  • Thank you. I agree.But I think that putting the whole sketch is not good idea. because It's hard for you to read.(and comment are Japanese mainly.) So,I will put Structure like that include,setup,loop,other method. How about that? – Ryosuke Dec 4 '17 at 3:44
  • 1
    If you can find some place to post the whole thing that would be better (it will be probably too big to fit here). I don't care about the comments personally because I usually ignore them. – Nick Gammon Dec 4 '17 at 3:52
  • This has 32516 characters. – Ryosuke Dec 4 '17 at 3:54
  • Now,I pleased that I heard generic techniques from @JRobert,@dannyf,@dda,@Michel Keijzers,too. Because I'd like to know "how to reduce sketch size" for making sketch in the future. – Ryosuke Dec 4 '17 at 5:00

Some generic techniques you can try:

  • Remove duplicate code and put it in a function.
  • If you have duplicate code that is ALMOST duplicate, parameterize it (i.e. add a function, make the difference a parameter(s) of the function and call from various locations with different argument(s).
  • Use a micro controller with more Flash memory.
  • Minimize the data storage, limiting array sizes for example (if these are in flash RAM).
  • If you use precalculated arrays, remove them and create a function calculating them real time (this will be a performance cost of course).
  • Check for dead code inside functions. A smart compiler will already remove uncalled functions, but do not check inside functions.
  • Remove or minimize debug/print statements. Each character cost a byte, which adds up quickly with many strings.
  • Try to remove complete libraries, like removing the Serial for print statements used only for debugging.
| improve this answer | |
  • 1
    I made code the function,in some cases this increase sketch size.So,It's difficult for me. I knew for the first time that A smart compiler will already remove uncalled functions, but do not check inside functions. For me, it became very helpful as well. Thank you! – Ryosuke Dec 12 '17 at 4:56

How to reduce sketch size?

without your code, tough to be specific. but generally:

1) try not to use floating pointmath;

2) try not to use printf() or its derivative;

3) try to minimize large arrays;

4) use functions instead of macros;

5) use modular programming;


Or,how to increase flash memory size?

1) use a bigger chip;

2) use onboard eeprom;

3) use outboard eeprom / flash / fram / ....

4) divide up the tasks among multiple chips;


| improve this answer | |
  • I don't know these point.Thank you for explaining very clearly – Ryosuke Dec 12 '17 at 4:48

If you're using donated libraries to run your hardware (such as the Dallas Temperature library) or provide software features (like SimpleTimer), the authors often provide a full complement of features, some of which you may not need. I often make my own versions of these libraries that provide the functionalities I must have, and cut out the others. I can always add one or two of them back into whichever sketch needs them, without burdening other sketches with their overhead.

In SimpleTimer, for instance, I seldom have need for enable/disable timer or for a timer that runs N-times and then stops, so I have a version of the library with those functions removed.

If you've declared some text strings or other data to live in Flash, and you have more spare RAM than spare Flash, let some of those objects live in RAM instead. Or reduce the length of output strings that are stored in Flash by writing more tersely, even at the cost of clarity in the output, or eliminating that outpu altogether (which eliminates not only the string data but the code to print it, as well).

Can you reduce any code segments or algorithms with more space-efficient code? Can you eliminate some functionality to save code space?

Note that trimming buffers and arrays won't help because those are in RAM, and data packing, such as storing a number of booleans as distinct bits within a word, will actually increase your code size (this is a common way to reduce RAM requirements when you can afford the code to do the packing/unpacking, but you have the opposite problem!).

| improve this answer | |
  • I learned from specific example very much. I used to Metro library,SoftwareSerial,etc.So I can use the solution. I did not understand this difference. (this is a common way to reduce RAM requirements when you can afford the code to do the packing/unpacking, but you have the opposite problem!). Therefore, I will be able to distinguish decreasing solution between Flash and RAM from now on. Thank you! – Ryosuke Dec 12 '17 at 5:06

There's no magic bullet. You have to work through your code and see what you can remove safely. If you have duplicate code, put it in a function and call this. If you Serial.print a lot of text, considering saving up on that. etc etc etc

| improve this answer | |
  • Yes,I decrease Serial.print.Thank you! – Ryosuke Dec 12 '17 at 4:46

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