Javascript variables, scopes, and memory problems, javascript

Source: Internet
Author: User

Javascript variables, scopes, and memory problems, javascript

Javascript variables, scopes, and memory problems

(1) JavaScript variables can be used to save two types of values.: Basic type value and reference type value. The value of the basic type is derived from the following five basic data types: Undefined, Null, Boolean, Number, and String. The basic type value and reference type value have the following features:

1. The basic type value occupies a fixed size in the memory, so it is stored in the stack memory;

2. copying a value of the basic type from one variable to another will create a copy of the value;

3. The value of the reference type is an object and is stored in heap memory;

4. variables that contain reference type values do not actually contain the object itself, but a pointer to the object;

5. Copy the value of the reference type from one variable to another, and copy the pointer. Therefore, both variables eventually point to the same object;

6. Determine which basic type of a value can use the typeof operator, and determine which reference type of a value can use the instanceof operator.

(2) All variables(Including basic and reference types) are stored in an execution environment (also called scope), which determines the lifecycle of a variable, and which part of the code can access the variables. The following is a summary of the execution environment:

1. The execution environment can be divided into a global execution environment (also known as a global environment) and a function execution environment;

2. Each time you enter a new execution environment, a scope chain is created to search for variables and functions;

3. The local environment of a function not only has the right to access the variables in the function scope, but also has the right to access its contained (parent) environment and even the global environment;

4. The global environment can only access the variables and functions defined in the global environment, but cannot directly access any data in the local environment. The execution environment of the variables helps determine when to release the memory.

(3) JavaScript is a programming language with an automatic garbage collection mechanism. Developers do not have to worry about memory allocation and collection.. We can summarize the garbage collection routines in JavaScript as follows.

1. The value of the exit scope will be automatically marked as recoverable, so it will be deleted during garbage collection.

2. "Mark clearing" is currently the mainstream garbage collection algorithm. The idea of this algorithm is to add a mark to the currently unused value and then recycle its memory.

3. Another garbage collection algorithm is "reference count". The idea of this algorithm is to track the number of times all values are referenced. Currently, JavaScript Engines no longer use this algorithm;

4. However, this algorithm may still cause problems when accessing non-native JavaScript objects (such as DOM elements) in IE. When the Code contains cyclic references, the "reference count" algorithm will cause problems.

6. In order to ensure effective memory recovery, you should promptly release the global objects, global object attributes, and reference of cyclic reference variables that are no longer in use.

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

Related Article

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.