Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit d64d41e

Browse files
committed
fix(Scope): more scope clean up on $destroy to minimize leaks
Due to a known V8 memory leak[1] we need to perform extra cleanup to make it easier for GC to collect this scope object. V8 leaks are due to strong references from optimized code (fixed in M34) and inline caches (fix in works). Inline caches are caches that the virtual machine builds on the fly to speed up property access for javascript objects. These caches contain strong references to objects so under certain conditions this can create a leak. The reason why these leaks are extra bad for Scope instances is that scopes hold on to ton of stuff, so when a single scope leaks, it makes a ton of other stuff leak. This change removes references to objects that might be holding other big objects. This means that even if the destroyed scope leaks, the child scopes should not leak because we are not explicitly holding onto them. Additionally in theory we should also help make the current scope eligible for GC by changing properties of the current Scope object. I was able to manually verify that this fixes the problem for the following example app: http://plnkr.co/edit/FrSw6SCEVODk02Ljo8se Given the nature of the problem I'm not 100% sure that this will work around the V8 problem in scenarios common for Angular apps, but I guess it's better than nothing. This is a second attempt to enhance the cleanup, the first one failed and was reverted because it was too aggressive and caused problems for existing apps. See: #6897 [1] V8 bug: https://code.google.com/p/v8/issues/detail?id=2073 Closes #6794 Closes #6856 Closes #6968
1 parent ba48797 commit d64d41e

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

src/ng/rootScope.js

+28-3
Original file line numberDiff line numberDiff line change
@@ -731,15 +731,40 @@ function $RootScopeProvider(){
731731

732732
forEach(this.$$listenerCount, bind(null, decrementListenerCount, this));
733733

734+
// sever all the references to parent scopes (after this cleanup, the current scope should
735+
// not be retained by any of our references and should be eligible for garbage collection)
734736
if (parent.$$childHead == this) parent.$$childHead = this.$$nextSibling;
735737
if (parent.$$childTail == this) parent.$$childTail = this.$$prevSibling;
736738
if (this.$$prevSibling) this.$$prevSibling.$$nextSibling = this.$$nextSibling;
737739
if (this.$$nextSibling) this.$$nextSibling.$$prevSibling = this.$$prevSibling;
738740

739-
// This is bogus code that works around Chrome's GC leak
740-
// see: https://github.com/angular/angular.js/issues/1313#issuecomment-10378451
741+
742+
// All of the code below is bogus code that works around V8's memory leak via optimized code
743+
// and inline caches.
744+
//
745+
// see:
746+
// - https://code.google.com/p/v8/issues/detail?id=2073#c26
747+
// - https://github.com/angular/angular.js/issues/6794#issuecomment-38648909
748+
// - https://github.com/angular/angular.js/issues/1313#issuecomment-10378451
749+
741750
this.$parent = this.$$nextSibling = this.$$prevSibling = this.$$childHead =
742-
this.$$childTail = null;
751+
this.$$childTail = this.$root = null;
752+
753+
// don't reset these to null in case some async task tries to register a listener/watch/task
754+
this.$$listeners = {};
755+
this.$$watchers = this.$$asyncQueue = this.$$postDigestQueue = [];
756+
757+
// prevent NPEs since these methods have references to properties we nulled out
758+
this.$destroy = this.$digest = this.$apply = noop;
759+
this.$on = this.$watch = function() { return noop; };
760+
761+
762+
/* jshint -W103 */
763+
// not all browsers have __proto__ so check first
764+
if (this.__proto__) {
765+
this.__proto__ = null;
766+
}
767+
/* jshint +W103 */
743768
},
744769

745770
/**

test/ng/rootScopeSpec.js

+26-2
Original file line numberDiff line numberDiff line change
@@ -844,13 +844,15 @@ describe('Scope', function() {
844844
expect(log).toBe('123');
845845

846846
first.$destroy();
847+
848+
// once a scope is destroyed apply should not do anything any more
847849
first.$apply();
848-
expect(log).toBe('12323');
850+
expect(log).toBe('123');
849851

850852
first.$destroy();
851853
first.$destroy();
852854
first.$apply();
853-
expect(log).toBe('1232323');
855+
expect(log).toBe('123');
854856
}));
855857

856858

@@ -874,6 +876,28 @@ describe('Scope', function() {
874876
$rootScope.$broadcast(EVENT);
875877
expect(spy.callCount).toBe(1);
876878
}));
879+
880+
881+
it("should do nothing when a child event listener is registered after parent's destruction",
882+
inject(function($rootScope) {
883+
var parent = $rootScope.$new(),
884+
child = parent.$new();
885+
886+
parent.$destroy();
887+
var fn = child.$on('someEvent', function() {});
888+
expect(fn).toBe(noop);
889+
}));
890+
891+
892+
it("should do nothing when a child watch is registered after parent's destruction",
893+
inject(function($rootScope) {
894+
var parent = $rootScope.$new(),
895+
child = parent.$new();
896+
897+
parent.$destroy();
898+
var fn = child.$watch('somePath', function() {});
899+
expect(fn).toBe(noop);
900+
}));
877901
});
878902

879903

0 commit comments

Comments
 (0)