最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How does Angularjs handle Memory Management with ngView? - Stack Overflow

programmeradmin3浏览0评论

When you are using ngView with say 100 different views each with a different scope. Does Angular automatically handle destroying the old templates/scopes or do they stay in memory? I'm just curious if Angular handles this by itself before I go and start writing custom code to reduce the memory load. As of right now each new view I go to just stacks up in the memory.

This is an AngularJS specific question. I know how garbage collection works in javascript.

When you are using ngView with say 100 different views each with a different scope. Does Angular automatically handle destroying the old templates/scopes or do they stay in memory? I'm just curious if Angular handles this by itself before I go and start writing custom code to reduce the memory load. As of right now each new view I go to just stacks up in the memory.

This is an AngularJS specific question. I know how garbage collection works in javascript.

Share Improve this question edited Jun 5, 2013 at 19:34 mfrancis107 asked Jun 5, 2013 at 19:12 mfrancis107mfrancis107 1,4011 gold badge13 silver badges21 bronze badges 4
  • 5 Yes, js has garbage collection. But it will not collect anything that it believes to be in use. With angular it assumes that the $scopes are still in use, therefore it will never collect it. So I'm asking how to what do I need to do in Angular to make sure it can be collected. – mfrancis107 Commented Jun 5, 2013 at 19:30
  • You could always use "delete"? – Index Commented Jun 5, 2013 at 19:32
  • @KGChristensen That's not how delete works? – rounce Commented May 19, 2014 at 15:08
  • @rounce No, it's not as I later learned :) – Index Commented May 19, 2014 at 17:28
Add a comment  | 

1 Answer 1

Reset to default 22

One of the design decisions behind introducing scopes was to ease memory management. By partitioning model's space into sub-parts (scopes) we can remove unneeded parts of the model (scope) and add new when needed. So yes, scopes are important part of the whole memory-management puzzle.

When it comes to your specific question about ng-view - this directive will keep scope only for the currently active view. ng-view is one of the scope creating (and scope destroying!) directives. It will automatically create a new scope when a new view is navigated to and will automatically destroy a scope connected with the old view. This can be easily verified in the AngularJS source code.

The only memory-consuming part to consider are templates fetched over a network. All the templates referenced in a route are cached in the $templateCache. You could evict templates using sparingly if you determine that it tackles a specific perf bottleneck in your app. We just need to realize that it is trading time (network time) for memory consumption.

In short: no need to roll out your own scope-management for the ng-view - if you see any scope retention it should be reported as a bug.

发布评论

评论列表(0)

  1. 暂无评论