I'm working on an embedded C++ project that involves logging certain types of statistical data (like successful accesses, failed attempts, and other events) to the flash memory (just incrementing these values). Due to the nature of the system, power can be lost at any moment, making it important that all data is written to the flash.

However, I'm also aware that flash memory has a limited lifespan in terms of write/erase cycles, especially when writing to the same address repeatedly. So, I'm looking for ways to optimize the number of writes to the flash memory without losing any statistics.

I'm currently considering solutions like write caching, wear leveling, and data compression. I'm not sure which method to implement due to their advantages and/or disadvantages, especially in an environment where power loss can occur unexpectedly.

Any insights or suggestions would be greatly appreciated.

Regards Dario

  • Sharing your research helps everyone. Tell us what you've tried and why it didn't meet your needs. This demonstrates that you've taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask
    – gnat
    Commented Aug 25, 2023 at 13:13
  • 1
    This sounds more like a hardware problem than a softwareengineering issue. I think this question is better suited for electronics.stackexchange.com , where you have been active in the past, as far as I can see. When you ask there, be careful not to crosspost, hence delete your question here beforehand.
    – Doc Brown
    Commented Aug 25, 2023 at 13:48
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer.
    – Community Bot
    Commented Aug 25, 2023 at 14:26
  • In these sorts of cases, to avoid data loss, typically the whole system has to be engineered to be transactional, with data not erased from source until the target has confirmed that its storage is permanent. If power cuts unexpectedly, then things just start over when power resumes, drawing from the permanent storage of the source.
    – Steve
    Commented Aug 25, 2023 at 14:45

2 Answers 2


Having worked in this kind of area in the past, it's quite tricky to get right. Two techniques that make it more comprehensible:

  • journaling. Rather than overwriting the same block all the time, have a circular buffer in which you always write "forwards". At boot time you need to determine which blocks are valid and whether the last one has been completely written, e.g. by having a block counter and putting a checksum at the end of the block. You can have logical blocks that are smaller than the minimum erase block of the Flash, if you are careful.

  • tallying. Flash writes are in one direction. usually a block starts out as all-1 after erase, and writes can convert 1->0. But you need an erase to set all the block back to 1. So, if you count with tally marks you can count 1111 -> 1110 -> 1100 -> 1000 -> 0000 -> erase. That saves you erase cycles.

  • 1
    Thanks! Im going to try these methods and inform you, which one we implemented. Commented Aug 28, 2023 at 5:52

Thanks to all for the responses.

Our selected approach is "wear leveling", since we have a rater big flash memory, and it is a fast way to store the data.

Regards Dario

  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Sep 6, 2023 at 19:53

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.