Maybe I'm missing something, as a new CIQ dev, but does loadResource() incur a permanent overhead? I've seen this with both string resources and jsonData.
Say I have a string that's roughly 400 chars which is only needed in initialization of an app. It's a 1D lookup table, to save memory....
I want to be able to load this data from a resource, and unload it when I don't need it anymore. But I've found there's practically no difference in declaring that string as a const and loading it as a resource, then unloading it. Obviously if I never load the string, I do save RAM, but I need to load it once.
To get good numbers, I started from 0 with a simple data field and did a test with a 350 character string. The only code difference between my test cases is one function call and a variable declaration.
Here are my test cases and numbers (in the simulator, for 230)
Case A: Run app with no string in code or resources.
Memory [current | peak]: 3.8 | 4.2
Case B: Add 350-char string as static const class var
Mem: 4.2 | 4.6
Case C: Remove const var and add to resources [redundant, I know]
Mem: 3.8 | 4.2
So far there's nothing unexpected.
Case D: Load string from resource, then immediately free it [set var to null]
Mem: 4.0 | 4.4 !
Where did that extra 200 bytes come from? (In my real app, where I actually use the string, I see no difference between case B and case D).
Case E: Load string from resource, never free it
Mem: 4.4 | 4.8 !
This is more memory-inefficient than just using a variable in code.
Maybe I'm missing something, so I've included my code below.
Replace square brackets with parentheses below:
using Toybox.WatchUi as Ui;
class memorytestView extends Ui.SimpleDataField {
function testCaseD[]
{
rez = Ui.loadResource[Rez.Strings.bigstring];
rez = null;
}
function testCaseE[]
{
rez = Ui.loadResource[Rez.Strings.bigstring];
}
var rez;
//static const caseBVar = "01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789";
// Set the label of the data field here.
function initialize[] {
SimpleDataField.initialize[];
label = "My Label";
/// 5 test cases
/// =============================
/// Case A [run as is]
/// Memory usage: 3.8, peak: 4.2
/// =============================
/// Case B: uncomment caseBVar above
/// Memory usage: 4.2, peak: 4.4
/// =============================
/// Case C:
/// Comment out caseBVar above
/// Add the following to resource file:
/// <string id="bigstring">01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789</string>
/// Memory usage: 3.8, peak: 4.2
/// =============================
/// Case D: Load bigstring from resources, free it [uncomment line below]
/// Memory usage: 4.0, peak: 4.4
//
//testCaseD[];
/// Case E: Load bigstring from resources, don't free it [uncomment line below]
/// Memory usage: 4.4, peak: 4.8
//
//testCaseE[];
}
function compute[info] {
return 0.0;
}
}