Best practices for reducing memory usage?

Former Member
Former Member

Hi all,

I am currently sitting at 86kb / 91.8kb for my first Garmin watchface, and I'm looking to reduce memory usage. 

So far, I've found the following has helped quite a bit:

  • Reducing the amount of bitmaps being used, when possible (by using fonts or drawing shapes instead)
  • Reducing or removing unused code, such as calls to System.println() and others
  • Only loading fonts necessary to the appropriate screen resolution 

What other memory reduction tips do you have?

Thank you!

  • Yes, of course.

    I simply write good code (especially then app is still dev, tested ) and look on memory. And when I approach to limit starting to use tricks.

  • but is hard core coding

    Yeah, unfortunately Monkey C has a whole bunch of features that come at the cost of making your code bigger. Combined with the extremely limited RAM constraints of most Garmin devices, it means you just can't use a lot of those features when you want to.

    I simply write good code (especially then app is still dev, tested ) and look on memory. And when I approach to limit starting to use tricks.

    Sure, that's what I want to do too. But you'll find that if you write data fields for older or mid-range devices with 16-32 KB of available memory, you'll find yourself rewriting a lot of code just to avoid waste.

    It's happened to me enough times that sometimes I start out using a lot of these tricks instead of going back and rewriting everything.

  • You can put 14 /*FONT_SYSTEM_NUMBER_MILD*/ 

    I did it on a wf and I saved around 2kb 

    Exactly. In Garmin-land, 2 KB is a huge amount of memory. Sometimes ~100 bytes buys you a whole "feature".

    Maybe what we need to do is write a transpiler from (say) JavaScript to Monkey C. We could write beautiful, modern code which transpiles to a terrible form of Monkey C with almost no classes, hardcoded constants, etc. (Just half-kidding ofc. I'm sure for our purposes, optimizing by hand is still the best way to save memory.)

  • DF are rather simple memory, now I finish my own and even use buffered bitmap

    The simplest solution is asking Garmin for more memory to VM :)

  • hi,

    may be something else, not sure whether it is a good idea, if not, please let me know.

    I have for example a function for the settings where I put into var the settings.

    So far i've done: (Settings function is called onInitialize and OnSettingsChange

    var CLR1,CLR2,CLR3,...;
    
    function Settings(){
        var ApA = App.getApp;
        CLR1 = ApA.getProperty("CLR1");
        CLR2 = ApA.getProperty("CLR2");
        CLR3 = ApA.getProperty("CLR3");
        ...
    
    } // I know I could do a loop but it is just an example

    but I noticed when you have a lot of settings to save, if I do that, it save memory,

    but may be not good for the battery or else?

    var CLR1,CLR2,CLR3,...;
    
    function gAgP(O){return App.getApp().getProperty(O);}
    
    function Settings(){
        CLR1=gAgP("CLR1");
        CLR2=gAgP("CLR2");
        CLR3=gAgP("CLR3");
        ...
    }

  • when you read or write properties or storage that could involve hitting the file system (slow) which is why you want to minimized how many times you do it.

  • so the 1est one is better since I put all App.getApp() in var?

  • reading an array of 5 things from Storage is more efficient than reading 5 separate things.  And while it take a bit of memory, it's better to cache things in your app than reading it all the time.

  • it is what I do no? or did I miss something? the settings function is called only on Initialize or OnSettingsIpdate.

    If I do AgA = App.getApp(); does the var stock all the settings?

    sorry if I don't get you, do you have a small example?

  • With Application.Storage, doing

    Application.Storage.setValue(myKey,[1,2,3,4,5]);

    if better than doing

    Application.Storage.setValue(myKey1,1);

    Application.Storage.setValue(myKey2,2);

    Application.Storage.setValue(myKey3,3);

    Application.Storage.setValue(myKey4,4);

    Application.Storage.setValue(myKey5,5);