Skip to content
frankdownunder edited this page Jan 7, 2019 · 10 revisions

Minimising Memory Usage

Literals use up memory, so its a good idea to move them to flash. Be aware though that it can be slower! Further reading, see the header FlashString.h in the sming source code

F macro Instructs the compiler to store the literal in flash (of which there is plenty) rather than RAM (which is limited)

Example: String system = F("system");

_F macro This function uses the stack, and is generally preferable to the F macro, but need to be mindful of scope. The content is lost as soon as the containing block goes out of scope. Used as a function parameter, that means the end of the function call. Its fine to use it as a parameter to a function call though.

Example: println(_F("Debug started"));

Example: commandOutput->print(_F("Welcome to the Tcp Command executor\r\n"));

Bad: char* s = _F("string")

An assignment such as this will not work because the temporary will be out of scope after the statement, hence s will point to garbage. In this instance PSTR_ARRAY(s, "string") can be used.

PSTR_ARRAY

To come

IMPORT_FSTR

To come See also HttpServer_ConfigNetwork sample for example of IMPORT_FSTR

JSON keys

Tip - Use the F macro without leading underscore.

Example: root[F("offset")] = something;

Bad: root[_F("offset")] = something;

According to the ArduinoJson docs it should take an internal copy of char* strings, but it doesn't! Use F() instead.

Sming/Sming/Libraries/ArduinoJson/include/ArduinoJson/JsonObjectKey.hpp