Translation|Takes You to Memory Management in Seconds - Part 1 (of 3)
To understand why ArrayBuffer and SharedArrayBuffer were added to JavaScript, you need to know something about memory management.
You can think of the memory in the machine as a pile of boxes. It's like your mailbox in the office, or the cash box your kids use.
If you want to leave something for the other kids, you can put it in a box.
Next to each box is a number, and these numbers are the memory address to tell someone where to find what you left for them.
Each of these boxes has the same dimensions and can hold a certain amount of information. The size of the box depends on the machine. This size is called word length. It is usually 32 or 64 bits. But for convenience, here we use 8-bit long.
If we wanted to put the number 2 in one of the boxes, we could easily do that. The numbers are very Easy to convert to binary。
What if what we want is not digital? Like the letter H?
We need a similarUTF-8encoding come round Replace these things with numbers。 And to convert these into numbers, We need a similar Tools for encoder rings。 After that we can store it。
When we want to take it out of the box come round at the time of, It must be converted back through the decoder H。
When you're working with JavaScript, you don't actually need to think about memory. Memory is abstracted away and you don't touch it directly.
Instead, the JS engine acts as an intermediary and manages the memory for you.
Let's say there's a piece of JS code used to create a variable (assuming the JS code uses React).
The JS engine uses an encoder to convert the value to binary.
It will find space in memory that can hold that binary, a process called allocating memory.
The engine then tracks whether the variable can still be accessed from anywhere in the program. If the variable is no longer accessible, the JS engine can store new values in reclaimed memory.
This process of monitoring variables (strings, objects, or other types) in memory and releasing the memory occupied by variables that are no longer in use is called garbage collection.
Languages like JavaScript that do not handle memory directly are called memory management languages.
such Automatic memory management Can make it easier for developers。 But it also adds some overhead, And this overhead can sometimes make performance unpredictable。
Compared to languages that automatically manage memory, Languages that require manual memory management are somewhat different。 for example, we come round see how React How to use C Language Writing to Memory( It is now possible to access the information viaWebAssembly come round realize)。
C does not have the abstraction layer that JavaScript has over memory. Instead, it runs directly on memory. You can load things from memory or you can store the content in memory.
When you compile C or another language to WebAssembly, the tool you use will add some helper code to WebAssembly. For example, it will add the code used to encode and decode the bytes. These codes are called runtime environments. The runtime environment handles some of the things that the JS engine should be doing.
However, for manually managed languages, garbage collection will not be included at runtime.
This doesn't mean you have to deal with it entirely on your own. Even in languages with manual memory management, there is usually some help from the language runtime. For example, in C, the runtime records which memory addresses are available in a table called the free list.
You can use the function malloc (shorthand for memory allocation) to request some memory address that can hold data. This will take these addresses away from the idle list. When you have finished processing this data, you shall call the function free Release off of the malloc The memory requested by the function. After that, these addresses will be added back to the idle list.
You have to figure out when to call these functions. That's why it's called manual memory management - you have to manage your own memory.
As a developer, it can be difficult to figure out when to clear different parts of memory. If you do this at the wrong time, you may experience bugs and even cause security vulnerabilities. If you don't, your memory will run out.
This is why many modern languages use Automatic memory management the reasons for the—— Avoiding human error。 But it comes at the cost of performance。 I'll explain more about this in my next post。