Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid memory leaks in applications with angular?

I tried to find if my angularjs code has memory-leak, but not found yet.

I've read some articles about javascript memory-leak, but it's not useful for angularjs application, since it uses bi-binding which hide most of DOM operation to users.

So I have another question: how to write memory-leaked application with angular? Is there any common mistake pattern that we should avoid?

like image 505
Freewind Avatar asked Dec 16 '22 13:12

Freewind


1 Answers

Angular mostly handles it for you, but there are places where you need to think about memory. Since your services exists from the time they are created to when your application closes, it's easy to hog memory in such objects. Like if your implementing caching, you might end up holding cached references to objects that will never be used again, so you would need a strategy to release those objects.

Another place is in directives where you interact with the DOM. But as long as you listen to $scope.$on('$destroy', function () { /* Clean up code here */ }); and clean up after yourself, you should be fine.

like image 97
Anders Ekdahl Avatar answered Jan 21 '23 07:01

Anders Ekdahl