angular/angular.js — pull request #6181 by renz45 (2014-02-07 19:53:02+00:00)
Title: ngTransclude memory leak fix

I've been profiling a one page app using Angular.js and have had a tough time nailing down this memory leak. I'm using Angularjs for a custom template system that outputs data from a content DSL containing educational content. In this system I swap out a lot of templates full of other angular directives which in themselves sometimes compile/transclude content. After a few days of searching I've finally narrowed down one of the larger memory leaks. Using `transclude: true` in a directive is fine, no problems with leaks, but as soon as `ng-include` is used within the template I was getting a lot of detached DOM fragments in the Chrome memory profiler.

Here is a sample app which replicates the problem:

```html

angular.js bug test

```

So running this using `ng-transclude` in the `secondLevel` directive, you can click the buttons a few times to see the transclude working, but when profiled in dev tools we get:

![screen shot 2014-02-07 at 2 16 42 pm](https://f.cloud.github.com/assets/599521/2113722/0cbe0782-9031-11e3-8a5e-26a1119df673.png)

As you can see there are 4 detached Dom node Trees as a result, not a huge deal in this smaller app, but in my larger app it's causing 350+ detached nodes and about 5mb heap increase per template change. This is a fairly large problem for a one page app since we never reload the page.

Now if we swap out `ng-transclude` with the proposed changes (`ng-transclude-fix`) we get:
![screen shot 2014-02-07 at 2 19 20 pm](https://f.cloud.github.com/assets/599521/2113725/1375f40e-9031-11e3-8001-40df4e6b4bae.png)

No detached dom nodes!

The tests pass, I'm not entirely sure how you would even test for detached Dom nodes and memory heap size. From my testing, it seems like the act of assigning $transclude to the controller (or scope for that matter) causes the detached nodes. This fix uses the new transclude function included within the `link` function to handle the transclude, instead of passing the $transclude object from the controller to the linking function. This is functionally the same, and eliminates the leak.