├── MemoryFree.cpp ├── MemoryFree.h ├── README.md ├── examples └── BareMinimum │ └── BareMinimum.ino ├── license ├── pgmStrToRAM.cpp └── pgmStrToRAM.h /MemoryFree.cpp: -------------------------------------------------------------------------------- 1 | 2 | #ifdef __arm__ 3 | // should use uinstd.h to define sbrk but Due causes a conflict 4 | extern "C" char* sbrk(int incr); 5 | #else // __ARM__ 6 | extern char *__brkval; 7 | #endif // __arm__ 8 | 9 | int freeMemory() { 10 | char top; 11 | #ifdef __arm__ 12 | return &top - reinterpret_cast(sbrk(0)); 13 | #elif defined(CORE_TEENSY) || (ARDUINO > 103 && ARDUINO != 151) 14 | return &top - __brkval; 15 | #else // __arm__ 16 | return __brkval ? &top - __brkval : &top - __malloc_heap_start; 17 | #endif // __arm__ 18 | } 19 | -------------------------------------------------------------------------------- /MemoryFree.h: -------------------------------------------------------------------------------- 1 | // memoryFree header 2 | // From http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1213583720/15 3 | // ...written by user "mem". 4 | 5 | #ifndef MEMORY_FREE_H 6 | #define MEMORY_FREE_H 7 | 8 | int freeMemory(); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Arduino library for Measuring and use less Free RAM 2 | 3 | This Library was written for the Arduino to both measure available RAM and use less RAM 4 | 5 | Did you know that the Arduino's built in serial.print() of a constant char array 6 | 7 | ```c 8 | Serial.Print("Hello"); 9 | ``` 10 | 11 | Actually allocates the "Hello" into a unique space of RAM, not just Flash. Hence every such occurrence of serial print of a constant string uses more and more RAM. Until you are out. And there is only 2K of RAM on the ATmega328. This is likely why you don't see many examples with a lot of debug prints. 12 | 13 | That said there is hope, use the "F()" function. Now natively supported in the IDE > 1.0.0. Same concept applies. 14 | 15 | Previously, I have both been suggested and found several work-a-rounds all similar in forcing the char array to be read directly from memory at print time. This can be done using some not as obvious AVR Libc commands provided with avr/pgmspace.h. Where the pgmStrToRAM library provides a simplified library to do such. 16 | 17 | Additionally the companion library MemoryFree is very useful in determining how much RAM is being used or left. Don't wonder or guess. I find it good practice to always dump it during setup to see the global static usage. Just in case. 18 | 19 | ## Installation 20 | 21 | * Download the Library 22 | * It will contain the following to be placed into corresponding library directories: 23 | 24 | ```bash 25 | .\pgmStrToRAM\MemoryFree.cpp 26 | .\pgmStrToRAM\MemoryFree.h 27 | .\pgmStrToRAM\pgmStrToRAM.cpp 28 | .\pgmStrToRAM\pgmStrToRAM.h 29 | .\pgmStrToRAM\examples\BareMinimum\BareMinimum.ino 30 | ``` 31 | 32 | * Place the pgmStrToRAM directory folder and contents into your arduino library folder, 33 | if the libraries folder does not exist - create it first! 34 | * Restart the IDE 35 | 36 | ## Example Usage 37 | 38 | BareMinimum.ino is a example of usage that both demonstrates how to print out the current available memory at the time the freeMemory() is executed along with how to using getPSTR("hello") to put the "hello" into Flash without using any static RAM. 39 | 40 | include the desired library(s) at the beginning of sketch 41 | 42 | ```c 43 | #include 44 | #include 45 | ``` 46 | 47 | then at the desired location 48 | 49 | ```c 50 | Serial.println(getPSTR("Old way to force String to Flash")); 51 | Serial.println(F("New way to force String to Flash")); 52 | Serial.println(F("Free RAM = ")); 53 | ``` 54 | 55 | Please note that the memory is dynamic and always changing. Where Static Global Variables and Members  create a base minimum of usage that can typically be read at setup(). When functions and routines dive deeper into the stack more memory will be dynamically consumed and released. So there needs to be a margin from completely running out. 56 | 57 | ## Back Ground 58 | 59 | Based on my tests I see that Serial.Print of actual variables e.g. 60 | Serial.Print( (int8_t) value, DEC); 61 | Does not increase RAM usage for each occurrence. 62 | However it would appear that overloaded object member of an char array (e.i. const string) creates a unique location in static RAM and definitely does not share it. As one might think or expect. 63 | 64 | A more indepth look at avr-libc and its features can be found at 65 | 66 | ## Alternatives 67 | 68 | It is worth noting that there are many other sources of equivalent methods. Although note widely known. One such alternative is "SdFATlib" found at library file called SdFatUtil.h 69 | 70 | ```c 71 | #include // required 72 | #include 73 | 74 | PgmPrint("Free RAM: "); 75 | Serial.println(FreeRam()); 76 | ``` 77 | 78 | ## Gratitude 79 | 80 | Special Thanks to GreyGnome, William Greiman and others for providing the example this is based off. 81 | -------------------------------------------------------------------------------- /examples/BareMinimum/BareMinimum.ino: -------------------------------------------------------------------------------- 1 | #include ; 2 | #include ; // not needed for new way. but good to have for reference. 3 | 4 | void setup() { 5 | // put your setup code here, to run once: 6 | Serial.begin(115200); 7 | Serial.println(getPSTR("Old way to force String to Flash")); // forced to be compiled into and read 8 | Serial.println(F("New way to force String to Flash")); // forced to be compiled into and read 9 | Serial.println(F("Free RAM = ")); //F function does the same and is now a built in library, in IDE > 1.0.0 10 | Serial.println(freeMemory()); // print how much RAM is available in bytes. 11 | // print how much RAM is available. 12 | } 13 | 14 | void loop() { 15 | // put your main code here, to run repeatedly: 16 | 17 | } 18 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2012 Michael P. Flaga 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /pgmStrToRAM.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | char *to_print; 6 | char *pgmStrToRAM(PROGMEM const char *theString) { 7 | free(to_print); 8 | to_print=(char *) malloc(strlen_P(theString) + 1); 9 | strcpy_P(to_print, theString); 10 | return (to_print); 11 | } -------------------------------------------------------------------------------- /pgmStrToRAM.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define getPSTR(s) pgmStrToRAM(PSTR(s)) 4 | 5 | char *pgmStrToRAM(PROGMEM const char *theString); 6 | --------------------------------------------------------------------------------