JavaScript Memory detailed

Source: Internet
Author: User

JavaScript has an automatic garbage collection mechanism (tag cleanup, reference counting), which means that the execution environment (described in detail in the execution of the environment) is responsible for managing the use of memory during code execution. In languages such as C and C + +, a developer's basic task is to manually track memory usage, which is a source of many problems, and in writing a JavaScript program, developers no longer care about memory usage. The allocation of required memory and the recovery of useless memory are fully automatic management.
Because JavaScript has an automatic garbage collection mechanism, memory is not a frequently mentioned concept for front-end development, and is easily overlooked. In particular, many are not computer professional friends in the front end, will be the memory of the cognitive more vague, and even some people simply is ignorant. For example, how to essentially distinguish between basic types and reference types? The closures? Shallow copy and deep copy? Event queue? Wait a minute.
If you want to fundamentally understand these principles, then you have to have a detailed understanding of memory.
There are three common data structures in JavaScript, namely stacks, heaps, queues, and so on.

One, stack data structure
Stack data structure access mode: Advanced post-out, LIFO (LIFO). such as the real-life table tennis box access to table tennis process.


The table tennis is stored in the same way that the data is accessed in the stack. The top-level table tennis in the box is 5, it must be put in at the end, but can be used first. And we want to use the bottom of the table tennis 1, it must be the above 4 table tennis out, so that table tennis 1 in the box on the top floor. This is the stack space advanced, after the first out of the characteristics. The storage principle of stack space is shown in detail in the diagram.
The stack data structure is usually a simple data segment, occupying small space, fixed size, and frequently used data.

Second, heap data structure

The heap data structure is a tree-like structure. Tree structure Everyone may hear the word is very alarmed, we can be in the browser console to print a random object, you will find that there are a lot of nested inside the layer of other things, then this shape is called its tree structure. The way it accesses data is very similar to a bookshelf and a book.
Although the book is also neatly stored on the shelves, but we just know the name of the book, we can easily take out the books we want, instead of playing table tennis from a ping pong box, we have to take all the above table tennis to get the middle of a table tennis. As in JSON-formatted data, the Key-value we store can be unordered, because the difference in order does not affect our use, we only need to care about the name of the book.
The heap data structure is usually occupied with large space, the size is not fixed, if stored in the stack, will affect the performance of the program running.

Third, queue data structure

How the queue data structure is accessed: FIFO. As in line with security, the person in front of the line must be the first person to check.

JavaScript Memory detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.