I'm trying to understand how to avoid memory leaks in AngularJS. There seems to be almost no documentation about what to clean up, which is as one would expect if garbage collection were perfect and omniscient. However, my reading indicates that it's easy to leave bound DOM elements which prevent garbage collection, this StackOverflow answer says that we must manually unbind any bindings set in directives, and on a smaller scale, clean up $timeouts as well. I came across this article, which instructs me to look for "detached" DOM elements.
Using just the sample $modal code from the AngularJS UI site, I find numerous sizable detached elements after opening and closing the modal dialog. It doesn't seem to grow with repeated clicking, but if I have many dialogs, each one leaves its footprint seemingly forever. I tried setting modalInstance = null
after close, but that didn't accomplish anything.
Are detached elements truly bad? If so, how should the AngularJS UI sample modal code be modified to eliminate these?
More importantly, are there any simple, clear, instructions for what specifically to clean up when using AngularJS? It's nice that they provide an $on.$destroy function, but I can't find any clear instructions indicating what I'm responsible for cleaning up, or how to do it.