JavaScript variable--stack memory or heap memory __java

Source: Internet
Author: User
Tags garbage collection

Heap and stack The two words we have touched many times, then what is the actual stack of what exists in the heap. Just take the variables in javascript:

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

The base type is the simple data segment that is stored in the stack memory, and the reference type refers to objects that are stored in the heap memory.

1. Basic Type

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

2. Reference type

Reference type, the value size is not fixed, the stack memory holds the address point to the object in the heap memory. is accessed by reference. As the following illustration shows: The stack memory holds only the access address of the object, allocating space for this value in heap memory. Because the size of this value is not fixed, they cannot be saved to stack memory. But the memory address size is fixed, so memory addresses can be stored in stack memory. In this way, 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 by address. For this, we call it access by reference.


Memory allocations in other languages are similar.

PS: When we see 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 is, so the program applies itself, so it is allocated in the heap.

                   

Why there is stack memory and heap memory points.

Usually related to the garbage collection mechanism. To make the program run with minimal memory.

When a method executes, each method builds its own stack of memory, and the variables defined within this method are placed into the stack memory one at a time, and the memory stack of the method is naturally destroyed as the execution of the method ends. Therefore, all variables defined in the method are placed in the stack memory;

When we create an object in a program, the object is saved to the runtime data area for reuse (because the object is typically created in a larger cost), the Run-time data area is heap memory. The object in the heap memory is not destroyed with the end of the method, even though it may be referenced by another reference variable (which is common when the parameter of the method is passed), and the object is not destroyed until an object references it without any reference variables. The system's garbage collection mechanism will be recycled when it is verified.

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.