Deep understanding of memory allocation in javascript

Source: Internet
Author: User

Deep understanding of memory allocation in javascript

 

There are two types of variables in javascript: the original value and the reference value. The original value refers to the value of the original data type, such as undefined, null, number, string, or boolean type. Reference values refer to values of the composite data type, such as Object, Function, and Array.

 

The locations where the original value and reference value are stored in the memory are stack and heap, respectively. Raw values are simple data segments stored in the stack, and their values are directly stored in the variable access location. The reference value is the object stored in the heap.

The value stored in the stack is a pointer pointing to the actual object stored in the stack.

Let's look at a piece of code:

 

Function Person (id, name, age) {this. id = id; this. name = name; this. age = age;} var num = 10; var bol = true; var str = "abc"; var obj = new Object (); var arr = ['A ', 'B', 'C']; var person = new Person (100, "jxl", 22 );

 

Let's look at the memory analysis diagram:

 

 

We can see that we cannot directly operate the data stored in the heap, but we can operate the object through the reference in the stack. So why should we separate them? Are not all stored in the memory?

The advantage of stack is that the access speed is faster than the heap speed, second only to the registers directly located in the CPU. However, the disadvantage is that the data size and lifetime in the stack must be fixed, and there is no flexibility. The advantage of heap is that the memory size can be dynamically allocated, and the lifetime does not need to be told to the compiler in advance. The Garbage Collector automatically collects the data that is no longer in use, but the disadvantage is that the memory is dynamically allocated at runtime, so the access speed is slow.

Therefore, compared with simple data types, they occupy a relatively small amount of memory. If they are placed in the heap, searching will waste a lot of time, and putting the data in the heap into the stack will also affect the efficiency of the stack. For example, objects and arrays can be infinitely expanded and placed in the heap where the size can be dynamically allocated.



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.