Memory Concerns

Hello,

I’ve taken this demo source (http://codepen.io/ionic/pen/HjnFx), compiled it with Cordova and ran it through XCode on my iOS device. All I’ve done in the app was navigating back and forth for 5 mins. During that time the memory consumption went from 19MB to 30MB.

I haven’t run any other tests and I don’t know if this is related to Ionic, Cordova or whatever. I just thought I should share this with you as I find this to be very alerting.

Has anyone else made this kind of observations? Or am I worrying about nothing?

Cordova 3.3.1, Ionic 0.9.19, XCode 5.0.2, iOS 7.0.4 on iPhone 5

We’ll definitely take a look into this and I created an issue:

Thanks for letting us know.

My app is currently a bit more complicated so far. I ran it for about 8 mins doing all the various tasks. It clocks in about the same.

It starts at 23.2MB. Other AngularJS projects I’ve run through Xcode are about the same; so, I don’t think this is specific to Ionic. However, I’ve never published an app at all; so, I don’t know if ~30MB is a big deal or not.

30mb is nothing. iOS wont force quit apps until you’re at least over 100mb and that’s only on the oldest devices. iPhone 5+ wont quit until you’re over 500mb.

Unfortunately, I seem to be having a problem where navigating between pages causes my memory usage to creep up steadily. I believe it’s related to improperly designed services. I think I need to add something to my $destroy methods, but I’m not sure what. Is this a common problem? The services I suspect are data model services that all inject a custom ajax service (custom to handle tokens, api server address, etc…).

It may be more of an angular question, but has anyone seen this? It takes a while, but I can get my apps up to several hundred mb in just a few mins of quickly clicking around.

I wish I had something more helpful to answer except after a few minutes of use, my demo app starts to jitter and run slower than when its freshly started. I have started to look into angular open bugs relating to memory and I found this

Certainly, I need to get to the bottom of this before I am finished. I consider there a problem somewhere but haven’t worked out yet if its me, ionic or angular

I use ng-repeat

I found the culprit! My services involved ajax calls, so when a controller loaded, I’d display the current (or cached) version of the data and then call the API server to get an update. I’d also pass the service a callback method to update my controller’s $scope instance of that data. That callback would get added to an array of callbacks to fire when new data is available. When the ajax request completes, I’d iterate through that array firing all the callbacks. I could use broadcasts, but I thought this would be more efficient.

What I wasn’t doing was clearing my callbacks when I left that page, so the automatic $destroy process was unable to completely remove the old scope/controller, and the array of callbacks would grow infinitely. The fix was to change the service’s list of callback to be an object instead of an array, keyed by the associated controller name. This meant that every new instance of a controller would overwrite the old one and it would allow the old controller’s scope to be completely removed from memory.

Bonehead mistake.

1 Like