Home Web Front-end JS Tutorial Understanding Javascript_01_Understanding memory allocation principle analysis_javascript skills

Understanding Javascript_01_Understanding memory allocation principle analysis_javascript skills

May 16, 2016 pm 06:18 PM
memory allocation

Primitive values ​​and reference values
In ECMAScript, variables can store two types of values, namely primitive values ​​and reference values.
Primitive values ​​refer to values ​​representing primitive data types (basic data types), that is, values ​​represented by Undefined, Null, Number, String, and Boolean types.
Reference values ​​refer to values ​​of composite data types, that is, Object, Function, Array, and custom objects, etc.

Stack and heap
are the same as original values The reference value corresponds to two structures of memory, namely the stack and the heap
The stack is a last-in-first-out data structure. In JavaScript, the behavior of the stack can be simulated through Array
Copy code The code is as follows:

var arr = []; //Create a stack
arr.push("apple");/ /Push the element "apple" ["apple"]
arr.push("orange"); //Push the element "orange" ["apple","orange"]
arr.pop(); //Pop up "orange" ["apple"]
arr.push("banana");//Push in the element "banana" ["apple","banana"]

us Let's take a look at the corresponding memory map:
Understanding Javascript_01_Understanding memory allocation principle analysis_javascript skills
The original values ​​are simple data segments stored on the stack, that is, their values ​​are stored directly at the location of the variable access.
The heap is a data structure based on a hash algorithm that stores data. In JavaScript, reference values ​​are stored in the heap.
The reference value is the object stored in the heap, that is, the value stored at the variable (i.e. the variable pointing to the object, stored in the stack) is a pointer pointing to the actual object stored in the heap.
Example: var obj = new Object(); obj is stored in the stack and points to the object new Object(), which is stored in the heap.
Then why are the reference values ​​placed on the heap and the original values ​​on the stack? Aren’t they both in memory? Why not put them together? Then, let’s explore the answer to the question!
First, let’s take a look at the code:
Copy the code The code is as follows:

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);

Then let’s take a look at the memory analysis diagram:
Understanding Javascript_01_Understanding memory allocation principle analysis_javascript skills
The variables num, bol, str are basic Data types and their values ​​are stored directly on the stack. obj, person, and arr are composite data types. Their reference variables are stored on the stack and point to the actual objects stored in the heap.

As can be seen from the above figure, we cannot directly manipulate the data in the heap, which means that we cannot directly manipulate the object, but we can manipulate the object through the reference to the object in the stack, just like we operate through a remote control It's the same as a TV, but the difference is that the TV itself doesn't have control buttons.


Now let us answer the question of why the reference value is placed on the heap and the original value is placed on the stack:

Remember one sentence: energy is conserved , it’s nothing more than a matter of time exchanging space, and space exchanging time.

The heap is larger than the stack, and the stack operates faster than the heap. The object is a complex structure and can be expanded freely. For example, arrays can be expanded infinitely. Objects can freely add properties. They are placed on the heap so as not to affect the efficiency of the stack. Instead, the actual object in the heap is found through reference and then operated. Compared with simple data types, simple data types are relatively stable and occupy only a small amount of memory. The reason why simple data types are not placed on the heap is because it takes time to find the actual object in the heap by reference, and this comprehensive cost is much greater than the cost of obtaining the actual value directly from the stack. So values ​​of simple data types are stored directly on the stack.

Summary:

The program is very simple, but it is the foundation of everything. The foundation is the most important, because skyscrapers are also built brick by brick.
Memory is the foundation for program execution. Understanding memory means understanding everything.
This is your hard work, encourage yourself, come on!

Reference:
JavaScript Advanced Programming
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the best practices for memory allocation in Java functions? What are the best practices for memory allocation in Java functions? May 02, 2024 pm 10:33 PM

Best practices for memory allocation in Java functions include using automatic memory management and ensuring that appropriate GC algorithms are used. Monitor memory allocation patterns and identify memory leaks or bottlenecks. Use object pooling to reuse objects of similar size. Avoid large numbers of short-lived allocations and consider using alternatives. Use the Null Object pattern to avoid creating unnecessary objects. Explicitly release native resources, ensuring memory that is not accessible to JavaGC is released.

Memory allocation analysis of golang function Memory allocation analysis of golang function Apr 29, 2024 pm 02:24 PM

Question: How to analyze the memory allocation of a Go function? Answer: Use the heapprofile function in the pprof package to generate a heap dump. Analyze the heap dump to determine the type and size of the allocation. Detailed description: Generate a heap dump: enable the heap profiler and call the heapprofile function. Analyze the heap dump: Use the gotoolpprof command to analyze the heap dump file to view allocation information.

Common errors in C++ function memory allocation and their solutions Common errors in C++ function memory allocation and their solutions Apr 22, 2024 pm 05:09 PM

Common errors in function memory allocation include: 1) dangling raw pointers; 2) memory leaks; 3) wild pointers; 4) freeing invalid pointers. Solutions: 1) Use smart pointers; 2) Use RAII; 3) Use memory pools.

An in-depth discussion of the memory allocation and expansion strategies of Golang slices An in-depth discussion of the memory allocation and expansion strategies of Golang slices Jan 24, 2024 am 10:46 AM

In-depth analysis of Golang slicing principle: memory allocation and expansion strategy Introduction: Slicing is one of the commonly used data types in Golang. It provides a convenient way to operate continuous data sequences. When using slices, it is important to understand its internal memory allocation and expansion strategies to improve program performance. In this article, we will provide an in-depth analysis of the principles of Golang slicing, accompanied by specific code examples. 1. Memory structure and basic principles of slicing In Golang, slicing is a reference type to the underlying array.

Analyze the differences between heap and stack in Java and their application scenarios Analyze the differences between heap and stack in Java and their application scenarios Feb 24, 2024 pm 11:12 PM

The difference between Java heap and stack and application scenario analysis require specific code examples. In Java programs, heap and stack are two commonly used data structures, and they assume different roles and functions in memory. Understanding the difference between heap and stack is crucial to writing efficient Java programs. First, let's take a look at the Java heap. The heap is an area used to store objects. All objects created in the program are stored in the heap. The heap is where memory is dynamically allocated and released while the program is running. It is not subject to any restrictions and can be automatically allocated and released as needed.

Pitfalls and best practices in memory allocation and destruction of C++ functions Pitfalls and best practices in memory allocation and destruction of C++ functions Apr 23, 2024 am 09:36 AM

In C++, there are pitfalls to be aware of when functions allocate and destroy memory, including memory leaks (holding pointers to memory that are no longer needed) and dangling pointers (pointing to freed memory). To prevent these problems, best practices include: using smart pointers (such as std::shared_ptr) to automatically manage memory; using RAII techniques to ensure that resources are released when an object goes out of scope; avoiding returning pointers to local variables; handling destructors carefully to release allocations of memory. By following these practices, you can ensure the reliability of your code and prevent memory leaks and dangling pointers.

Memory allocation and recycling methods of Golang functions Memory allocation and recycling methods of Golang functions May 16, 2023 pm 01:51 PM

Golang is an object-oriented programming language with its own garbage collection mechanism, so memory allocation and recycling are easier than other languages. In Golang, functions can also allocate memory and recycle memory. This article will introduce in detail the memory allocation and recycling methods of Golang functions. 1. Memory allocation of functions When a function is declared, the program will allocate a memory for it to store the code and static variables of this function. When a function is called, the program allocates a stack memory to store the local variables of the call.

Implement efficient memory allocation and garbage collection in Go language Implement efficient memory allocation and garbage collection in Go language Sep 28, 2023 am 11:10 AM

Achieving Efficient Memory Allocation and Garbage Collection in Go Language In modern programming languages, it is very important to manage memory effectively. As a language born for efficiency, the Go language provides simple and easy-to-use memory management and garbage collection mechanisms to help developers achieve efficient memory allocation and release without the need to manually handle memory. Memory allocation in Go language is the responsibility of the runtime system. It provides a memory area called the Heap for storing dynamically allocated objects, arrays, slices, etc. When we use n

See all articles