1

I've got a scenario where I need to poll a service every 30 seconds with a GET request and store it for the UI to display. I've wrapped my GET request in a setInterval w/ 30000ms. What I'm seeing is that after every call in the browser, the mem climbs between 100k and 200k. After about 8mb or so, the browser properly garbage collects and the mem falls back down by 4-5mb.

My problem is that with this application wrapped in Cordova 6.5, the memory climbs for about 4 days before the app crashes. The logs from the crash show issues related to memory running out. I've also examined the running application with xCode and notice the same memory climb as I see in a browser, which never gets collected. I'm inclined to think that there is some issue with garbage collection of an Observable that is instantiated within a setInterval call.

I've created a plunkr that shows the memory climb. Looking for 1) why this is happening, and 2) how to rectify it without having to destroy the view and reload it (which is what I've resorted to for the time being).

Plunkr

setInterval(() => {
  this.http.get('./people.json')
  .subscribe((response) => {
      this.people = response;
    }, (err) => {
      //console.log('Error');
    });
}, 15000);

Environment: Angular 4.0.0 / RxJS 5.2.0 / @angular/http 4.0.0 / Cordova 6.5 / iOS 10.3

Brant
  • 1,764
  • 11
  • 18
  • 1
    what are you doing with that 'people' value? – snorkpete Mar 29 '17 at 00:43
  • Same as in the plunkr. Nothing but storing it and overwriting every 30 seconds. There are some keys on the people object that are bound to the UI, like {{ people.name }}, 2 to be exact. – Brant Mar 29 '17 at 00:46
  • maybe [this](http://stackoverflow.com/questions/38008334/angular-rxjs-when-should-i-unsubscribe-from-subscription) will help. – Mavlarn May 22 '17 at 03:16

1 Answers1

0

Use .unsubscribe. In each interval you set a new subscription.