Acknowledged
CIQQA-3247

Storage.setValue() Lacks Graceful Handling for Large Values and Low Memory

The use of Storage.setValue with large amounts of data is associated with several issues.

Inconsistent Documentation

The documentation for Storage.setValue states:

"There is a limit on the size of the Object Store that can vary between devices. If you reach this limit, the value will not be saved and an exception will be thrown. Also, values are limited to 32 KB in size."

However the Core Topics documentation directly contradicts the API reference. It states:

"The Application.Storage module manages persistent key-value pair data storage. Information is automatically saved to disk when Storage.setValue() is called. Keys and values are limited to 8 KB each, and a total of 128 KB of storage is available."

https://developer.garmin.com/connect-iq/core-topics/persisting-data/

https://developer.garmin.com/connect-iq/api-docs/Toybox/Application/Storage.html

Inadequate Error Handling

While an exception is defined for exceeding the overall storage limit, the behavior of the API when the per-value limit is exceeded remains undocumented and ambiguous. What exactly should happen if a single value exceeds the limit?

Based on my testing, I've observed the following issues:

Uncatchable Memory Errors
When attempting to store a Dictionary structure approaching ~90 KB in memory, Storage.setValue() fails with an uncatchable out-of-memory error. This occurs regardless of the free application memory, which ranged from 300 to 600 KB during my tests.

Glance-Specific Limitations
In Glance apps, the memory threshold is obviously lower. There also seems to be a shared memory pool among Glance apps, leading to cascading failures. For example, an out-of-memory error from Storage.setValue() in one Glance app can crash a second, unrelated Glance app performing a web request. See the example below.

---
Error: Out Of Memory Error
Details: 'Failed invoking <symbol>'
Time: 2025-05-22T04:40:47Z
Part-Number: 006-B4313-00
Firmware-Version: '20.22'
Language-Code: eng
ConnectIQ-Version: 5.1.1
Store-Id: 8a6b62f1-5450-4912-8799-2eeaf9779d97
Store-Version: 60
Filename: F56A2548
Appname: openHAB
Stack:
  - pc: 0x10001161
  - pc: 0x100008e6
---
Error: Out Of Memory Error
Details: failed inside handle_json_callback
Time: 2025-05-22T04:40:47Z
Part-Number: 006-B4313-00
Firmware-Version: '20.22'
Language-Code: eng
ConnectIQ-Version: 5.1.1
Store-Id: 261ec5d2-bcbe-4b8b-b770-5f92f2c19ba4
Store-Version: 74
Filename: F3ED5327
Appname: 'evcc-beta'
Stack:

Recommended Improvements

To improve robustness, Storage.setValue() should return a well-defined error code when a value is too large or when there is insufficient memory—similar to how Communications.makeWebRequest() returns -402 and -403. This would allow developers to gracefully handle such conditions instead of encountering uncatchable runtime crashes.