JavaScript - Garbage collection after function calls - Stack Overflow

I had some reads about garbage collection in JavaScript, according to them, local variables of function

I had some reads about garbage collection in JavaScript, according to them, local variables of functions are collected after the function returned (except for the cyclical references that need breaking circles for the GC to work).

What I'm trying to understand is, what does "function returned" in this context mean?

Does it mean:

  1. The function has to return values.

    or simply:

  2. The function call has ended.

Common sense (according to me at least) suggests 2), but in the case I'm wrong:

  • What about functions that don't return values?
  • Should I add an empty return; to the functions that don't return anything to make the garbage collector do its job?

I had some reads about garbage collection in JavaScript, according to them, local variables of functions are collected after the function returned (except for the cyclical references that need breaking circles for the GC to work).

What I'm trying to understand is, what does "function returned" in this context mean?

Does it mean:

  1. The function has to return values.

    or simply:

  2. The function call has ended.

Common sense (according to me at least) suggests 2), but in the case I'm wrong:

  • What about functions that don't return values?
  • Should I add an empty return; to the functions that don't return anything to make the garbage collector do its job?
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Sep 12, 2013 at 21:03 heytoolsheytools 7824 silver badges16 bronze badges 2
  • 2 The language specification does not specify when garbage collection occurs. Implementations are free to do it whenever they want, as long as they never collect anything that is still accessible to the program. The article you link to discusses some possible ways it could be done, but there is no requirement that it be done any of those ways. That article is quite old and GC technology has changed a lot since it was originally written. – Raymond Chen Commented Sep 12, 2013 at 21:09
  • Re your link: This is 2013. I think it's safe to assume no one is using Netscape Navigator 3 :-) – David Knipe Commented Sep 12, 2013 at 21:21
Add a ment  | 

1 Answer 1

Reset to default 9

Functions that don't contain a return statement implicitly return undefined. So a function "ending" and "returning" are the same thing.

Concerning garbage collection, keep in mind that local variables are not collected immediately after functions return; they can just bee eligible for garbage collection at that point. The garbage collector runs whenever the browser considers it must run, or believes it can run without affecting visible performance much (and that's implementation-dependent).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744846169a4596850.html

相关推荐

  • JavaScript - Garbage collection after function calls - Stack Overflow

    I had some reads about garbage collection in JavaScript, according to them, local variables of function

    2天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信