JavaScript variable--stack memory or heap memory

Source: Internet
Author: User

Original http://blog.csdn.net/xdd19910505/article/details/41900693

Heap and stack The two words we have touched many times, then what is the existence of the stack of what is in the heap? Take The variables in JavaScript:

First, The variables in JavaScript are divided into basic types and reference types.

A basic type is a simple piece of data that is stored in the stack memory, whereas a reference type refers to an object that is stored in the heap memory.

1 . Basic type

The base types are Undefined, Null, Boolean, number, and string. These types occupy a fixed size of space in memory, and their values are stored in the stack space, which we access by value.

2 . Reference type

reference type, the value size is not fixed, the stack in memory of the address pointed to the object in the heap memory. is accessed by reference. As shown in: Stack memory is just the access address of the object,     because the size of this value is not fixed, they cannot be saved to the stack memory. But the memory address size is fixed, so memory addresses can be saved in the stack memory. Thus, when querying a variable of a reference type, the memory address is read from the stack and then the value in the heap is found through the address. For this, we call it access by reference.

Memory allocations in other languages are similar.

PS: When we see that a variable type is known, it is allocated in the stack, such as int,double. Other unknown types, such as custom types, because the system does not know how big it needs, so the program itself applies, so it is allocated in the heap.

Why is there a stack of memory and heap memory points?

This is usually related to the garbage collection mechanism. To make the program run with minimal memory.

When a method executes, each method builds its own memory stack, and the variables defined in the method are placed in the stack memory, and as the method executes, the memory stack of the method is destroyed naturally. Therefore, all variables defined in the method are placed in the stack memory;

When we create an object in the program, the object is saved to the runtime data area for reuse (because the object is typically created at a large cost), which is the heap memory. Objects in the heap memory are not destroyed with the end of the method, even after the method ends, the object may be referenced by another reference variable (which is common when the method's arguments are passed), and the object is not destroyed, only if an object does not have any reference variables to reference it. The system's garbage collection mechanism is only recycled when it is verified.

JavaScript variable--stack memory or heap memory

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.