loadResource mem usage

Hi

I am loading some strings from strings.xml resource file.

Recently I added a interface version string and found out that this one call of loadResource used up 400 bytes.

Can somebody explain why? 

I tried it on other strings and there the loadResource only needed about 20 bytes.

I have a logmessage with free memory at the end of the onUpdate function.

Before the change 1664 free

if(str == null || str.length() < 14 || !str.substring(0,1).equals("B")) {

changed to

<string id="IfVersion">B</string>
..
if(str == null || str.length() < 14 || !str.substring(0,1).equals(WatchUi.loadResource(Rez.Strings.IfVersion))) {

after the change 1248

And this change: 1680 before

if(subtype == 3) { aTexts[i] = "2.8.7";	}

changed to

<string id="Version">2.8.7</string>
..
if(subtype == 3) { aTexts[i] = WatchUi.loadResource(Rez.Strings.Version);	}

after the change 1664.

Can anybody tell me why the first change used so much space ?

thanks

regards

Erich

  • The first call to loadResource() loads the whole string resource table (and other resource tables) into memory, including any strings that are used only for app settings (and not by the actual watch app). If you add more string resources, you should see that 400 byte change get larger. Note that this table's size is only affected by the number of strings, and not the size of each string.

    I just did a test in the simulator: each entry in the table takes up exactly 12 bytes, plus a fixed overhead of 36 bytes.

    You'll see this if you look at Globals in the memory viewer:

    - With at least one loadResource(): several entries for Global/Rez/* (e.g. Global/Rez/Strings)

    - Without loadResource(): no entries for Global/Rez

    This is why, in my memory-hungry apps, I avoid calling loadResource() at all. 

    The use of loadResource() can save memory if you have a small number of very large resources (which are not all loaded simultaneously), since memory usage for the resource tables is based on the number of elements and not their sizes. If you have a large number of small resources, and/or lots of config settings (with associated string resources), then it becomes very wasteful.

    To be clear, app memory usage is affected by resources in the following ways:

    - The total number of resources (assuming you have at least 1 loadResource()) call. (This memory usage will remain constant after the first call to loadResource())

    - The size of any resources currently loaded in memory by loadResource(). (This usage can go up and down during the lifetime of the app, when the variables loadResource() is assigned to go in and out of scope.)

  • Hi

    Thank you for your explanation. But I think it doesn't quite explain the behaviour I see, or I dont understand it correctly.

    I am using loadResource about 5 more times in the datafield, these two are not the only loadResource calls in my datafield!

    And the first example used 400 bytes more, altough I had 4 more loadResource in other parts already.

    EDIT: Sorry, your explanation describes my behaviour because the other parts where I call loadResource do not get called in that case and it IS the only call of the loadResource.

    thank you!