v8如何将阵列存储在碎片内存中 [英] How v8 stores arrays in a fragmented memory

查看:62
本文介绍了v8如何将阵列存储在碎片内存中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道v8如何解决将数组存储在碎片内存中的问题。基本上,v8中的数组数据结构是什么。我假设v8引擎必须处理内存碎片问题。我已经读到C分配了具有连续内存的数组,这很有意义,因为无论如何直接分配它。但是使用JavaScript时,它是动态的,因此似乎无法始终连续分配它们。

I am wondering how v8 solves the problem of storing arrays in a fragmented memory. Basically, what is the array data structure in v8. I assume under the hood v8 has to deal with the problem of memory fragmentation. I have read that C allocated arrays with contiguous memory which makes sense since you are allocating it directly anyways. But with JavaScript it is dynamic so it seems you can't allocate them contiguously always.

给出8个字节的空闲内存块○,并分配●,请设想这种情况。 / p>

Given memory blocks of 8 bytes free ○ and allocated ●, imagine this scenario.

○○○○○○○○○○○○○○○○○○○○○○○○○○○

然后添加5个数组:

●●●●●○○○○○○○○○○○○○○○○○○○○○○

然后将另一个数组添加到内存的不同部分:

Then you add another array to a different part of memory:

●●●●●○○○○◖◖◖○○○○○○○○○○○○○○○

问题是,如果在第一个数组中再添加10个项目,它将如何工作:

The question is, if you add 10 more items to the first array, how does it work:

●●●●●●●●●◖◖◖●●●●●●○○○○○○○○○

想知道您是否在其他地方跟踪数组结构,而不仅仅是它们是连续的(例如在C中)。

Wondering if you are keeping track of the array structure somewhere else instead of just the fact that they are contiguous (like in C).

推荐答案

V8开发人员在此处。每个数组(稀疏/字典模式和密集/数组模式)都有一个元素的后备存储。如果添加的元素多于后备存储可以容纳的数量,则会分配一个新的后备存储,并复制所有元素。在这种情况下,后备存储将增长一个因子(而不仅仅是一个元素),这可以为您带来摊销不变元素的添加性能。如果没有足够的内存(或连续内存)用于新的后备存储,则V8会因内存不足错误而崩溃。

V8 developer here. Every array (both sparse/dictionary and dense/array mode) has one backing store for elements. When more elements are added than the backing store can hold, a new backing store is allocated and all elements are copied over. In this event the backing store is grown by a factor (not just one element), which is what gives you amortized-constant element addition performance. When not enough memory (or contiguous memory) for a new backing store is available, V8 crashes with an out-of-memory error.

这篇关于v8如何将阵列存储在碎片内存中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆