Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
142 views
in Technique[技术] by (71.8m points)

javascript - JavaScript(或NodeJS)如何处理内存分配?(How does JavaScript(or NodeJS) handle memory allocation?)

Assume V8 context.

(假设V8上下文。)

Lets say an element of Number type has a size of 4 bytes.

(可以说Number类型的元素的大小为4个字节。)

Now, if I have an array,

(现在,如果我有一个数组,)

let a = [1,2,3,4,5];

Logically speaking, the starting addresses of blocks for each element should be 1000,1004,1008,1012,1016

(从逻辑上讲,每个元素的块的起始地址应为1000,1004,1008,1012,1016)

Now, lets say we have a string TestString which takes 10 bytes, and I do:

(现在,假设我们有一个字符串TestString ,它占用10个字节,我这样做:)

a[2] = 'TestString';

Such that the array becomes [1,2,'TestString',4,5] .

(这样数组就变成[1,2,'TestString',4,5] 。)

How does JS handle the memory allocation of TestString and managing the address space of blocks in the array?

(JS如何处理TestString的内存分配并管理数组中块的地址空间?)

  ask by Ayush Gupta translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

An array in JavaScript is really just a special type of object.

(JavaScript中的数组实际上只是一种特殊的对象类型。)

As such, the "indexes" of the array are really just properties storing an integer value, and the data being stored are just pointers to the allocated memory blocks-- very much like a linked list.

(这样,数组的“索引”实际上只是存储整数值的属性,而存储的数据只是指向已分配存储块的指针,这非常像一个链表。)

This is why you can use array methods like push or pop on the existing array without re-allocating the block of memory set aside for the array.

(这就是为什么您可以在现有数组上使用诸如push或pop之类的数组方法而无需重新分配为该数组预留的内存块的原因。)

I don't know the exact details of what V8 is doing, but I'm going to assume because of JavaScript's loosely/dynamically typed nature, it's probably not allocating memory contiguously like you used in your example-- there are just too many potential drawbacks to that for such a weak and dynamically typed language.

(我不知道V8在做什么的确切细节,但是由于JavaScript的松散/动态类型化性质,我将假设它可能不会像您在示例中所使用的那样连续分配内存-潜力太多了这种弱且动态键入的语言的缺点。)

JavaScript basically makes everything an object.

(JavaScript基本上使一切成为对象。)

An array is an object, the indexe values of the array, just pointers to objects.

(数组是一个对象,该数组的索引值只是对象的指针。)

Which is why every data type is a variable and has access to properties and methods.

(这就是为什么每种数据类型都是变量并且可以访问属性和方法的原因。)

In reality, the address spaces of:

(实际上,以下地址空间:)

let a = [1, 2, 3, 4, 5];

Would be pointers to the allocated memory blocks such as 548995, 48885, 3889282, 093838, 7883344. Or something like that.

(指向分配的内存块的指针,例如548995、48885、3888892、093838、7883344。或类似的东西。)

When you re-allocate any of them, JavaScript will find a block of memory in the heap and set the array index value to the allocated block's pointer.

(当您重新分配它们中的任何一个时,JavaScript将在堆中找到一个内存块,并将数组索引值设置为所分配的块的指针。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...