memory

What is memory before we talk about shallow copy and deep copy

Memory partition

Memory partitions are almost the same in any programming language

1. Let’s start with the five memory Spaces defined in C

1 to 1. The stack

The stack is used to hold the parameters and local variables of a function. It is allocated by the compiler and released automatically by the compiler when the function is finished executing.

1-2. The heap

The heap is used to hold space allocated by dynamic allocation functions, configured manually by the programmer, and must be freed by the programmer using free. If you forget to use free, the allocated space will remain occupied, leading to memory leaks.

1-3. The global area

The global area is used to store global and static variables and is allocated and released by the compiler during the entire run of the program.

1-4. Text constant area

The text constants area is used to store text constants. For example, char*c=’123456′, where ‘123456’ is a literal constant stored in the literal constant area. The literal constant area is also allocated and freed by the compiler.

1-5. Program code area

The program code area is used to store the binary code of the program.

2. Memory space for javascript in the browser

2-1. What compiler is mentioned in C?

A compiler is a compiler that compiles high-level languages into machine code, such as Java, C, C#, and C++. Here, we need to understand the language type.

  1. Compiled languages use a compiler to compile source code into machine code: C/C++
  2. Interpreted languages do not require intermediate compilation, but rather are translated line by line by interpreter at execution time: Python, Javascript, and text.py or.js files are interpreted directly by the Python interpreter or V8, without compilation.
  3. Compilation and interpretation, such as Java, where text formatted.java files are compiled into.class binary byte files that are interpreted and executed by the JVM.

So JS doesn’t need a compiler and runs line by line.

2-2. Js – Stack memory

The browser allocates a block of memory in the computer’s memory, The ECStack (Execution Context Stack), which is used specifically for code Execution, generates a new ECS for each page opened

Basic data types

String, Boolean, Number, undefined, null, Symbol,

var a = 12;
Copy the code

Create a value -> Create a variable -> associate variables with values Basic data type values are stored directly in stack memory

The role of ECS
  1. Provide an environment for JS code to execute from top to bottom (code executes on the stack)
  2. Since primitive values are relatively simple, they are stored directly in a place in the stack memory, and when the stack memory is destroyed, the stored primitive values are also destroyed
Free stack memory
  1. In general, the stack memory created by the execution of the function is automatically freed by the browser after the function is executed.
  2. Sometimes when a function completes, the stack memory cannot be freed.
  3. The global scope is created when the page is loaded and destroyed when the page is closed.
  4. The global scope is released when the page is closed or refreshed. (Once the stack memory is freed, the values stored in the stack memory are also destroyed.)
  5. Private scope: Normally, when the function completes, the private scope (stack memory) created is released automatically, but there are special cases.
  6. After the function is executed, some contents in the current stack memory are occupied by unexpected variables in the stack memory. At this time, the stack memory cannot be released, and the basic values stored in the stack memory will not be released, and will be saved all the time. The most typical example is closures.

2-3. Js – Heap memory

Heap memory: The space in which reference values are stored for reference type values (objects: key-value pairs, functions: code strings). When memory is freed, the reference value is completely lost.

(1) Object data type, common object {}, array object [], regular object /^$/, new Date, Math function (2) Function data type

Reference data type
var a = {n:12};
Copy the code

Create a heap memory -> store key-value pairs in memory -> place the heap memory address on the stack for variables to call. So when you’re referring to a data type, you create a heap, you store things in it, and then you put the address on the stack for variable association using all the pointer assignments, all the associated points of Pointers

Heap memory release

When the heap is not occupied by any variables or other objects, the browser recyles memory when it is free, destroying all unused memory

Google Chrome (WebKit), at regular intervals to find if the object is occupied reference counter: when the object reference is 0 release it

Heap memory uses the null object pointer to free space.

var obj = {}; The heap memory corresponding to the current object is occupied by the variable obj and cannot be destroyed. obj = null; Since null is an empty object pointer (it does not point to any heap memory), the last heap memory is not used, and Google Chrome will automatically free (destroy/reclaim) any unused heap memory at idle time.

Why does null display object

For NULL, although it is a primitive type, object is displayed, which is a long-standing Bug

In the original version of JS, the 32-bit system was used. For the sake of performance, the type information of variables was stored in low order. The beginning of 000 represents an object, while null represents all zero, so it was wrongly judged as object. Although the internal type determination code has changed, the Bug has persisted.

2-4. Global object GO

The Global Object (GO) browser side points WINDOW to GO

The browser sends built-in attribute methods to a separate memory Heap. Any memory that has been opened has a hexadecimal memory address, which makes it easier to locate the memory later

2-5. Execute context EC

EC (Execution Context) : The environment in which the code itself is executed

  • Global execution context EC (G)
  • The code in a function is handled in a separate, private execution context
  • Block-level execution context

The resulting global execution context goes into the stack memory to perform “push” and after executing the code, it may release the resulting context from the stack.

2-6. The variable object VO

  • VO (Varibate Object) variable Object

In the current context, a place to store created variables and values (each execution context has its own variable Object; the function private context is called an AO (Activation Object), but is also a variable Object)

  • VO(G) global variable object:

The space used to store global variables in the global context, it’s not GO, it’s just that in some cases things in VO (G) are associated with things in GO. “Mapping mechanism.”

2-7. Variable promotion

Variable promotion is a property unique to var and function

The so-called variable promotion and function promotion is the promotion of all variable declarations and function declarations to the top of the global scope (only the promotion of variable declarations does not initiate the promotion of assignments).

2-8)

3. Shallow copy and deep copy

The fundamental difference between a deep copy and a shallow copy is whether or not you actually get a copy of an object, rather than a reference.

If B copies A, change A to see if B changes: if B also changes, it is shallow copy (modify the same value in the heap). If B does not change, it is deep copy (modify different values in the heap).

  • ShallowCopy simply adds a pointer to an existing memory address
  • A deepCopy adds a pointer to a new memory and makes the added pointer point to the new memory

3-1. Shallow copy instance

The copy just has a new pointer to the address in the heap

const a = [1.2.3.4.5.6];
const b = a;

b[0] = 2;
console.log(a); / / 6 [2]
Copy the code

3-2. Deep-copy example

Copying creates a new location in the heap, and a new pointer points to it

let arr = [1.2.3.4.5.6];
let arrs = arr.concat();
arrs[0] = 2;
console.log(arr); / / [6]
console.log(arrs); / / 6 [2]
Copy the code

see

1.Baidu search front-end code including HTML, CSS, JS is compiled and run?
2.Blog Garden – Lemon baby
3.Blog Park – Zhang Xiaozhong
4.CSDN- Whale Scene whale Lakat