How do I reduce application memory size?

Hey folks,

I'm trying to add support for Instinct to the meditate app. It builds fine with the latest 4.1.1 SDK but crashes immediately with OOM (out of memory) when launching against a simulator.

Launching it against a different target (Fenix6) indicates that the app peaks at 106kb which is somewhat more than 92kb that are available on instinct2 (as far as I understand).

How would you go about reducing the memory foot print?

I naively thought that removing resources would be the first step but it doesn't appear to work:

The original prg size 254kb, see below:

-rw-r--r-- 1 dmitry staff 254780 31 Mar 15:38 Meditate.prg

After I remove a bunch of images totalling 16kb:

-rw-r--r-- 1 dmitry staff 4126 31 Mar 15:38 ./resources/fonts/fontAwesome/fontAwesomeFreeRegular_0.png
-rw-r--r-- 1 dmitry staff 7314 31 Mar 15:38 ./resources/fonts/fontAwesome/fontAwesomeFreeSolid_0.png
-rw-r--r-- 1 dmitry staff 3511 31 Mar 14:06 ./resources/fonts/meditateIcons/meditateIcons.png
-rw-r--r-- 1 dmitry staff 1207 31 Mar 14:06 ./resources/fonts/meditateIcons/yoga.png

The prg size is reduced by 5kb only:


-rw-r--r-- 1 dmitry staff 249852 31 Mar 15:41 Meditate.prg

I then remove fonts (~12kb):

-rw-r--r-- 1 dmitry staff 1649 31 Mar 13:43 ./Meditate/resources/fonts/roboto/roboto46Bold.fnt
-rw-r--r-- 1 dmitry staff 2490 31 Mar 15:38 ./StatusIconFonts/resources/fonts/fontAwesome/fontAwesomeFreeRegular.fnt
-rw-r--r-- 1 dmitry staff 5678 31 Mar 15:38 ./StatusIconFonts/resources/fonts/fontAwesome/fontAwesomeFreeSolid.fnt
-rw-r--r-- 1 dmitry staff 1199 31 Mar 14:06 ./StatusIconFonts/resources/fonts/meditateIcons/meditateIcons.fnt

and the prg only drops 2kb:


-rw-r--r-- 1 dmitry staff 247532 31 Mar 15:44 Meditate.prg

Now, when I launch the app in the sim, it's at 96kb. And crushes immediately I'm assuming because I hacked away a bunch of resources.

Anyway TLDR:

1. App at peak is 106kb

2. I remove 28kb worth of stuff.

3. App at peak is 96kb now.

How would you suggest I go about reducing the app size so it works on Instincts?

  • Try decreasing the size of the NAMES of variables/classes you use.  For instance rather than using "heartbeatIntervalsSensor" you could use digraphs for variable names like "hs/*heartbeatIntervalsSensor*/", including the comment after every mention, to maintain readability. 

    props to 6629252 for trying to get this working on the small footprint'ed Instinct 2.  

  • All names are changed to symbol (numbers) so the length doesn't matter. But probably it can help limitation of symbols (e.g. use only var temp, temp everywhere you need temp) - only one symbol for all code.

  • For some reason using smaller symbol names saved a bunch of memory back in 2016 when I programmed a datafield for the Forerunner 630 (targeting connectIQ version 1x & 2x) which had to be less than 16k in size, otherwise I don't think I would have made those changes, haha.  but ok, I would hope names are now changed to symbols during the compilation process

  • maybe there was no symbols table. And I hope it run as symbol (I've made same test somewhen).

    But of course number of var, function classes etc produces bigger code.

  • Note that even if you can get your app to run with 92k, there are other things involved with the Instinct 2 - only B&W for one, but the biggie is the UI.  With the subscreen taking about 1/4th of the screen, you need to make changes to avoid or use that, which pretty much changes every screen/view in your app.  The amount of data you can display may be reduced unless you user a font no one can read without a microscope. Slight smile

  • I'm not sure why Insctinct2 is limited so severely. I suspect it's a marketing move -- if the only differentiator between instinct2 and Fenix is color and touch, that may not be good enough. 

    This is not my app, so I'm trying to learn my way through it. 

    Anyway, looking at the app, it consists of three barrels:

    1. HrvAlgos (HRV calculations, stress, etc)

    2. ScreenPicker (for moving between application screens)

    3. StatusIconFonts (fonts)

    4. the app itself.

    #1 I'm not gonna touch, 'cause that's what I"m primarily interested so I don't want to mess with that.

    #3 -- I'm going to completely remove any fonts and icons.

    #2 -- I'm going to roll up screen picker into main app. I don't know what the memory impact of using barrel is, but it must be non zero.

    For #4, there seems to be a lot of code in the "settings" folder; so I'm gonna try and blow it all away in favour of hardcoded values.

    In general, the app looks very well designed and implemented. Too bad I have to cripple it to fit it on Instinct2.

    yeah I know about the display. But this app specifically, a meditation app, is all about closing your eye during the activity so it's not that big of a deal.

  • Using the exact same code in a barrel vs directly take about 1k more memory.  The way I do barrels, is that I can either use the .barrel, or, with jungles, point right at the mc files.

  • If the Instinct 1 supported connectIQ or the Instinct 2 had more memory it would definitely be my favorite watch - the g-shock form factor appeals to me and other outdoor enthusiasts, and possibly also that the instinct looks like a basic watch, but isn't.  Yes, coding an already existing app to refit the new screen is difficult, but part of marketing we do as app developers.  The instinct is my preferred target, but unfortunately with such a small memory capability I'll need to wait until Instinct v3 comes out before it has the same capabilities as other >120k watches.

    By removing the fonts and settings, not to mention the screen picker (not sure what that is), you're sure to save a ton of space.  Removing the fonts in my own app enabled it to work on other earlier watches like FR630.  I've also stayed away from automated UI functions, and coded more of my own, ie re-using delegate classes helps too.  And thanks to the sharing of other developers and Garmin affiliated people on this forum, there are already other discussions about it you can read