void storedata(File filename ,int type , String data)
    if(type == 1){
      Serial.println("Group Creation");
    //Storing Data into file
    pinMode(10, OUTPUT);
    if (!SD.begin(SD_CS_PIN)) {
    Serial.println("initialization failed!");
    Serial.println("initialization done.");
    filename = SD.open("filename.txt", FILE_WRITE);
    if (filename) {
    Serial.print("Writing to filename.txt...");
    SdFile file("sdfile.txt", O_WRITE | O_CREAT);
    if (!file.isOpen()) {
    //close the file:
    // if the file didn't open, print an error:
    Serial.println("error opening filename.txt");

Im using this function to write on the Sd card , before calling this function the sketch memory is 28802 bytes ie ) 93% , simply just by calling this function from setup increased the sketch memory to 102% and iam unable to compile is there any unusual memory usage in this code ?

  • Try shortening the text inside the serial.printf statements. That should already shave off a few bytes, though that's probably not enough.
    – Gerben
    Commented Mar 24, 2017 at 14:01
  • @Gerben What i dont Understand is Just this simple Function taking 10 % of memory,Im using functions all over my program , ill still try to reduce the code and optimize it what puzzles me is how could only this function could raise the memory from 93% to 103%? Commented Mar 24, 2017 at 14:07
  • You're forgetting that you are also calling some functions from the SD library. Those functions also contain a lot of code.
    – Gerben
    Commented Mar 24, 2017 at 14:10

2 Answers 2


The good thing is that flash memory is instantly to be checked (you get the result after a (succesfull) compilation.

Refactoring you can do to reduce space:

  • prevent duplicate code; try to make a function from duplicated code and call it whenever needed; this also makes the program much more easy to maintain (bugs in those part have to be solved only once, and changed only adapted once).
  • If the code is almost the same but not an exact duplicate, use parameters in the function for the differences.
  • Remove code that does not have a function but still is called. The compiler can automatically delete UNUSED code, but USED code that has no function you have to do yourself.
  • Normally PROGMEM and F() is used to store arrays/strings in FLASH instead of SRAM, since mostly SRAM is more scarce, in your case you should not use this. Try to optimize code using less lines. This does not mean you have to remove spaces or make big oneliners; that will not help. But changing code structurally without changing the functionality will reduce flash memory.
  • Remove unused libraries (although these are probably optimized out by the compiler)
  • Global variables/structs/arrays and lots of const variables will cost memory to. For const values it might be useful to use #defines, however this will reduce maintainability/type checking.
  • Remove debugging code (printf's for debugging functionality); I never tried but I think it should be feasible to make unit tests separately from the sketch code.
  • A tricky way that could help is to optimize the libraries and reducing code there when needed. It is not useful to throw away functions that will not be called though. And debugging information should not be present, but maybe you can remove some conditions or code fragments that will never occur.

If you have a function that isn't called the compiler will just delete it (optimise it out). It also deletes any functions that the function calls that aren't used anywhere else.

By then calling that function the compiler keeps it, along with any other functions it relies on.

That includes the SDFat library code, much of which will have been removed before.

To make it now fit you will have to optimise your program. Look for blocks of code that do the same thing and replace them with calls to a function. Look for parts that could be replaced by a loop and an array instead of discrete calls to the same function(s) over and over again. Things like that.

Either that or use a board that has enough flash for your project.

  • Im not using SD library , im using Sdfat library since it uses less memory. Commented Mar 24, 2017 at 13:55
  • It still needs that 512 byte buffer though - there's no getting away from that.
    – Majenko
    Commented Mar 24, 2017 at 13:55
  • 1
    The problem is too little flash memory, not RAM.
    – Gerben
    Commented Mar 24, 2017 at 13:57
  • 1
    Ah. Well, optimise your program then. Get rid of repeated blocks of code by using functions. Either that or use a board with more flash.
    – Majenko
    Commented Mar 24, 2017 at 13:58
  • I have changed my answer in the light of this new infor,ation.
    – Majenko
    Commented Mar 24, 2017 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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