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!

  • sorry mate, I guess my explanation was bad, FlowState's just given me the answer I needed,

    I talked about GET the properties and for device prior 2.4 we have to use this method,

    and the Dev User Guide is clear about the confusion which can be made with the "old" method

  • There would only be a difference if you overrode the function in question in your own app class.

  • I've already asked about it but want to confirm:
    is length of variable influence on memory consumption.
    The answer was YES, but it's rather strange after looking in debug.xml and CIQ_LOG. I think that name is not important because compiler change it into numbers in other case it doesn't need to do it and in log should include name of function and var.


  • I came across this post that seems to be solid: https://forums.garmin.com/developer/connect-iq/f/discussion/6267/performances-dos-and-donts

    Also, if you're moving a lot of pngs to and from memory I suggest using https://tinypng.com/ - I use them on all my Android projects when optimizing apps.

  • Former Member
    Former Member over 4 years ago in reply to Joe Berger

    Thanks Joe.

    IIRC, compressing PNGs won't make a difference on Garmin since all images are compiled anyway.

  • More context about memory usage for properties and whether they're accessed directly from disk or cached.

    https://forums.garmin.com/developer/connect-iq/f/discussion/7721/migrating-from-properties-to-storage

    Application.Properties (and the older API AppBase.[get|set]Properties) load the dictionary of keys/values into memory when application loads, and save it out when the application shuts down. This means application memory is consumed by this dictionary at all times.

    So it's likely that calling getProperty() all the time would not affect battery life, but I still probably wouldn't do it.

  • According to: https://developer.garmin.com/connect-iq/connect-iq-faq/how-do-i-create-a-connect-iq-background-service/

    You also can’t pass information between the main process and the background with global variables. The word “process” is important here: global variables are per process, so the same global is only global for that process. A variable defined globally exists in both the main process and background process, but each maintains its own copy and they’re never synced..

    It means that if  I have such code:

    //first file
    var globalVar1;//for example big bitmap 5kB
    
    class PSX1_View extends UII.WatchFace
    {
        ...
    }
    
    
    //second file
    (:background)
    class PSX1_App extends APP.AppBase
    {
        function getInitialView()
        {
            mV = new PSX1_View();
            return [mV];
        }
    
        function getServiceDelegate()
        {
            return [new BackgroundService()];
        }
        ...
    }
    
    //third file
    (:background)
    class BackgroundService extends SYS.ServiceDelegate
    {   
        ...
    }

    bitmap is in 3 procese and consumes 15kb memory instead of 5?

  • I'm not sure why you have the bitmap as a global variable, but until you load something into that variable it's just a null object.  So if you never load anything to that variable in the background service, it takes little memory and your AppBase when run as the main app shares that with your view, so between the two, it's just 5k and not 2x5k.

    I can see from what you posted where the bitmap is actually loaded.

  • It was example it can be anything, I have "big" (in Gramin's wf 768 bites in very big) global object I need it to show the same data in few controls. This object is need for view and it's drawable.

    But what about question: ... but each maintains its own copy ...

    Is it mean that app, background and "view" process keep consumes memory?  Maybe not 3x768 but "olny" 2x768 because PSX1_App and BackgroundService can be in the same process.

    How many processes is in this example? 1,2, 3?

    I noticed that is impossible to access global var in background it's separated, but I can use const but const is not changed that they same ve same values in main and background so it run well.

  • THis is a case where I wouldn't have it as a global, or inside app base.  I'd keep it as a var inside the view class.