2

I'm afraid im having the same probem as this unasnwered question.

I have a blank IFRAME In my cordova ionic IOS App for embedded YouTube videos.

I change the NG-SRC="" of iframe by clicking Next Video. But upon testing it appears that the phone/cordova/webview is caching the old content of the iframes. I can only get through 20 videos or so before crashing out of memory.

I have tried using angular.element.remove() to remove the iframe as well as setting the iframe src to blank first, and neither seemed to affect how much memory is in use, per Xcode. I've also tried the cordova plugin ClearCache and that didnt clear any memory either.

Please help! Is there a better way to embed youtube in a cordova app? I have spent weeks working on this all to have it crashing down around me (no pun intended)

My Video view is like:

<ion-view view-title="Random Video">

          <iframe id="youtube" sandbox="allow-scripts allow-same-origin" ng-src="{{video.url | trustAsResourceUrl}}" frameborder="0" allowfullscreen></iframe>

            <h4>{{video.title}}</h4>

      <button ng-click="nextVideo()">
</ion-view>

My controller is like:

angular.module('starter.controllers')
 .controller('VideoCtrl', function(VideoService) {    

        $scope.video = {};


      $scope.nextVideo = function() {
        $scope.video = null;  //doesnt seem to help
        //$scope.$destroy(); //doesn't help
        //angular.element(document.querySelector( '#youtube' )).attr("src", " ");
        //angular.element(document.querySelector( '#youtube' )).remove();

        //neither of the above 2 remove any memory

        VideoService.getVideo().then(function(response){
                $scope.video = response.data;
        });
      }

        $scope.nextVideo();


});

Note, when I load my app onto a website instead, and load in chrome, I can cycle through videos without seeing the memory usage go up (looking at taskmgr.exe at least)

Community
  • 1
  • 1
James Cooper
  • 417
  • 1
  • 3
  • 12

1 Answers1

0

It might seem like setting the iframe to an empty string should be enough, but for some browsers and some situations it isn't. It might be necessary to recursively delete event listeners and elements one by one. Maybe surprisingly, the recursive method (1) below is faster than just setting to an empty string (2):

1.Recursive

while (box.lastChild) {
  box.removeChild(box.lastChild);
}

2. Setting empty string

myNode.innerHTML = '';

See https://stackoverflow.com/a/3955238/1158376 for reference.

Additionally, in the recursive approach, one might need to apply special treatment to some items, for example first remove event listeners, nullify functions (http://javascript.crockford.com/memory/leak.html), and use dedicated cleanup methods, like with jQuery (http://javascript.info/tutorial/memory-leaks).

Another strategy you could try is to load a new web page with a fresh iframe for every video you play. Loading a new page should enable the browser to release the previously claimed memory.

Community
  • 1
  • 1
Per Quested Aronsson
  • 11,380
  • 8
  • 54
  • 76