admin管理员组文章数量:1391981
I am confused about how memory allocation and function calls tracking happen in Javascript. One thing I am quite sure about is that there is a call stack in JS, where function calls are stored in LIFO manner. But when it es to memory allocation, I am confused which of the following arguments is correct:
- Primitive variables like string and numbers, are stored in something called as stack, which is different from call stack. Whereas non-primitive variables like objects and functions are stored in heap memory, but their references are stored in stack. (As per these articles: /, / and /)
or
- There are only two things: Call stack and heap memory. Call stack will store function calls in LIFO manner. Whereas heap memory will store variables whether primitive or non-primitive. (As per these videos: ;t=32s, )
If 1st argument is correct, then how call stack and stack are connected to each other such that function in call stack can identify which variables to pick from stack for its execution?
I am confused about how memory allocation and function calls tracking happen in Javascript. One thing I am quite sure about is that there is a call stack in JS, where function calls are stored in LIFO manner. But when it es to memory allocation, I am confused which of the following arguments is correct:
- Primitive variables like string and numbers, are stored in something called as stack, which is different from call stack. Whereas non-primitive variables like objects and functions are stored in heap memory, but their references are stored in stack. (As per these articles: https://blog.alexdevero./memory-life-cycle-heap-stack-javascript/, https://felixgerschau./javascript-memory-management/ and https://felixgerschau./javascript-event-loop-call-stack/)
or
- There are only two things: Call stack and heap memory. Call stack will store function calls in LIFO manner. Whereas heap memory will store variables whether primitive or non-primitive. (As per these videos: https://www.youtube./watch?v=7rOpIX-7ErA&t=32s, https://www.youtube./watch?v=xFNWb7KiG58)
If 1st argument is correct, then how call stack and stack are connected to each other such that function in call stack can identify which variables to pick from stack for its execution?
Share Improve this question asked Jun 26, 2022 at 8:42 Vaibhav SharmaVaibhav Sharma 751 silver badge4 bronze badges1 Answer
Reset to default 6JavaScript will keep on stack whatever it knows at pile time the size of (primitives have a fixed size so they can be put on the stack, as well as references to objects -- this is why you have size limitations).
Objects and functions on the other hand don't have a known fixed size at pilation time so they need to be stored in a dynamic place (heap). It allocates as much memory as it's needed.
The stack will be used to get to the heap as well (remember, refference is kept on the stack).
When the stack does not contain the function reference or the object reference, it will be garbage collected and the heap will be freed up.
JavaScript has a refference counting algorithm to identify if any of the allocated variables are still needed in heap memory.
Every function is a closure in javascript, so it knows the variables that are needed for it (the ones defined in the upper closure and the ones defined inside of it).
Hope this helps.
If you want to dive deeper into how everything works, this article covers the question that you had
A stack is usually a continuous region of memory allocating local context for each executing function.
LE:
This article has a great visualization of the stack (with primitives associated in the local context for each function)
This is the stack memory area and there is one stack per V8 process. This is where static data including method/function frames, primitive values, and pointers to objects are stored. The stack memory limit can be set using the --stack_size V8 flag.
本文标签: Confusion between stackcall stack and memory heap in JavascriptStack Overflow
版权声明:本文标题:Confusion between stack, call stack and memory heap in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744668701a2618691.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论