Close to running out of memory

Former Member
Former Member
I'm developing an App for the Fenix 3 and it consumes 59KB. I already had it crash once for running out of memory but it only happened once. How do I bring this footprint down?
I already did some optimizations to reduce the number of variables I use and to try not to create too many objects. It helped a bit but nothing drastic. What sort of objects or variables are the ones that consume the most space? Any tips ?

The memory per app should grow to 128 KB, just like Chronos. !


Thanks
Jesus
  • Former Member
    Former Member over 8 years ago
    633

    @Jesus

    I have found that arrays (and likely dictionarys) consume HUGE amounts of memory compared to the data they hold.

    Eg, I had a 2 dimensional array holding about 200 integers (array of 100 full of array of 2) which should have consumed about 200 * 4 or 800 bytes. Mych more and I'd run out of memory.

    What I have been trying (with mixed success due to what I think is a bug in the FW with toNumberWithBase) is that I can place huge (by comparison) amounts of data into a single string (I was able to get about 10K into a single string) and I was encoding my data in base 36 with 2 characters per number and using the substring method to pull out the 2 byte pairs. With this I was able to get WAY more data into the app than trying to store them in an array of integers.

    My suspected bug was if you try to decode in base 36 the characters 0X (zero X) it would work in the simulator but crash the watch. You could likely get around this with a base that didnt include X and still get a lot more data into a string than you could in an array.

    Hope this helps.
    Andrew
  • If you want to have something like a 2 dimensional array, but don't want the overhead, there are a couple of things to do:

    var ARRAYSIZE=200;
    var part1=0,part2=ARRAYSIZE;

    var array=new[ARRAYSIZE*2];
    // and then use something like [part1+i] and [part2+i] to access the two "dimensions"

    //or just use
    var array1=new[ARRAYSIZE],array2=new[ARRAYSIZE]; //as the two dimensions
  • Former Member
    Former Member over 8 years ago
    @jim,

    Just tried your suggestion. Looks like it works great. My arrays are getting loaded via JSON so I'll have to try that too but its better than my string approach.

    Thanks
  • When you define a 2 dimensional array (say 200x2) is you're basically creating an "array or arrays", so in this case you'd be defining 201 arrays.
    var array=new[ARRAYSIZE];

    for(var i=0;i<ARRAYSIZE;i++) {
    array=new[2];
    }
    [/code]

    The first suggestion I posted results in only one array, and the second, 2 arrays. It's not only memory you need to worry about, but available objects, and using an actual 2 dimensional array, you burn up many more objects.
  • Former Member
    Former Member over 8 years ago
    266

    AndrewCumming and others,

    I don't have arrays. Well, I have arrays with initialization data when the app starts but then I free them after initialization by setting the identifier to null. My app is structure hierarchically, with the App object containing other objects. The app is not trivial but it also isn't something that I would consider big. I see a few dictionaries but those must be from the SDK because my app does not use dictionaries.
    One thing I noticed is that when I open one of the app pages the KBs jump for 51 to 59. That is an awful lot for a layout that is composed of text fields and a circle.

    Jesus
  • AndrewCumming and others,

    I don't have arrays. Well, I have arrays with initialization data when the app starts but then I free them after initialization by setting the identifier to null. My app is structure hierarchically, with the App object containing other objects. The app is not trivial but it also isn't something that I would consider big. I see a few dictionaries but those must be from the SDK because my app does not use dictionaries.
    One thing I noticed is that when I open one of the app pages the KBs jump for 51 to 59. That is an awful lot for a layout that is composed of text fields and a circle.

    Jesus


    Try to reduce your use of classes. I usually write data fields where we have a nominal 16k, and most of that isn't available in later versions of the SDK. I've merged all classes together that are not absolutely needed (eg needed to extend an SDK base class). I've also ended up hard coding all data that can be hardcoded rather than use consts or variables (eg x,y data when drawing to screen).

    Makes ugly code but saves a lot of memory.

    An example is: https://gitlab.com/nz_brian/HiVisRunField
  • Former Member
    Former Member over 8 years ago
    Thanks for the answer!