Reduce NVS writes to Flash
Posted: Sun Jan 17, 2021 10:10 am
Hi,
I'm saving important data to flash using the Preferences library, mainly because prefs is more convenient to use than the nvs library.
One thing that strikes me is that preferences issues a "commit" with every "put"-call I make. Now here's the issue: I'm assuming that nvs rewrites an entire sector whenever a commit is executed. Hence my thought is that when I save 10 variables, the sector will be written 10 times - even when all my variables are physically located in the same sector, because of the "commit" which goes with every "put". Now let's assume that I collect my 10 variables in a single struct and I only save the entire struct with a single call to putBytes, that will result in a single write to flash i.s.o. 10 separate writes. If I'm right, that should extend the "lifetime" of my flash in terms of write cycles by a factor of 10?
Of course I could make calls to nvs functions directly, i.s.o. using preferences, but in essence the challenge remains the same: reduce the number of commits as much as possible. And that also means, limit the number of different namespaces, since commit is tied to a namespace and has to be issued for every namespace separately. Am I right or am I missing something?
I'm saving important data to flash using the Preferences library, mainly because prefs is more convenient to use than the nvs library.
One thing that strikes me is that preferences issues a "commit" with every "put"-call I make. Now here's the issue: I'm assuming that nvs rewrites an entire sector whenever a commit is executed. Hence my thought is that when I save 10 variables, the sector will be written 10 times - even when all my variables are physically located in the same sector, because of the "commit" which goes with every "put". Now let's assume that I collect my 10 variables in a single struct and I only save the entire struct with a single call to putBytes, that will result in a single write to flash i.s.o. 10 separate writes. If I'm right, that should extend the "lifetime" of my flash in terms of write cycles by a factor of 10?
Of course I could make calls to nvs functions directly, i.s.o. using preferences, but in essence the challenge remains the same: reduce the number of commits as much as possible. And that also means, limit the number of different namespaces, since commit is tied to a namespace and has to be issued for every namespace separately. Am I right or am I missing something?