DynamicJsonBuffer are designed to be throwaway memory pools, they are not intended to be reused.
They are able to allocate memory, but not to release it; they always grow, as they are not able to shrink.
The only way to release the memory is to destroy the
That’s why using a global
JsonBuffer is always a bad idea!
ArduinoJson is designed to do one thing and to do it well: the JSON serialization.
So before trying to use a global
JsonBuffer, ask yourself first:
Am I really using ArduinoJson for serialization, or am I pushing it beyond its initial intent?.
In particular, you should not use
JsonArray to store the internal state of your program as this would be terribly inefficient. Instead, write your own data structure and use ArduinoJson only in serialization functions, as shown in What’s the best way to use the library?
Many projects use a JSON file to store their configuration: the file is read at startup and the content is kept in memory during the execution of the program.
In that situation, it’s tempting to use a global
JsonObject attached to a global
In theory, it’s OK to use a global read-only
JsonObject, because the
JsonBuffer won’t grow.
But in practice, it’s a huge waste of memory and processor time.
The best way to deal with this is to use custom data structures as suggested in What’s the best way to use the library?.
This is a win in four levels:
JsonVariant… have significant overhead)
One might think that a legitimate usage of a global buffer is to have a
StaticJsonBuffer in the
.data segment so that the compiler will issue an error if there is not enough memory.
While this seems like a good idea, it wastes a lot of RAM as it reserves memory that is used only a fraction of the time.