javascript - JavaScript 如何决定为数值分配多大的内存?

标签 javascript memory memory-management

像 Java/C 这样的编程语言有 int、long、byte 等,它们可以建议解释器在运行时准确地为一个数字分配多少内存。如果您要处理大量变量,这会节省大量内存。

我想知道没有这种原始变量类型声明(JavaScript、Ruby)的编程语言如何决定为 var a = 1 分配多少内存。如果它分配了 1 个字节,那么在下一行中,如果我执行 a = 99999999999 ,它将必须清除该变量并重新分配。这不会是一项昂贵的手术吗?

或者他们是否为所有变量分配了一个非常大的内存空间以便one size fit all

最佳答案

Here is a good explanation.

JavaScript values

The type JS::Value represents a JavaScript value.

The representation is 64 bits and uses NaN-boxing on all platforms, although the exact NaN-boxing format depends on the platform. NaN-boxing is a technique based on the fact that in IEEE-754 there are 2**53-2 different bit patterns that all represent NaN. Hence, we can encode any floating-point value as a C++ double (noting that JavaScript NaN must be represented as one canonical NaN format). Other values are encoded as a value and a type tag:

On x86, ARM, and similar 32-bit platforms, we use what we call "nunboxing", in which non-double values are a 32-bit type tag and a 32-bit payload, which is normally either a pointer or a signed 32-bit integer. There are a few special values: NullValue(), UndefinedValue(), TrueValue() and FalseValue(). On x64 and similar 64-bit platforms, pointers are longer than 32 bits, so we can't use the nunboxing format. Instead, we use "punboxing", which has 17 bits of tag and 47 bits of payload. Only JIT code really depends on the layout--everything else in the engine interacts with values through functions like val.isDouble(). Most parts of the JIT also avoid depending directly on the layout: the files PunboxAssembler.h and NunboxAssembler.h are used to generate native code that depends on the value layout.

Objects consist of a possibly shared structural description, called the map or scope; and unshared property values in a vector, called the slots. Each property has an id, either a nonnegative integer or an atom (unique string), with the same tagged-pointer encoding as a jsval.

The atom manager consists of a hash table associating strings uniquely with scanner/parser information such as keyword type, index in script or function literal pool, etc. Atoms play three roles: as literals referred to by unaligned 16-bit immediate bytecode operands, as unique string descriptors for efficient property name hashing, and as members of the root GC set for exact GC.

根据 W3Schools:

This format stores numbers in 64 bits, where the number (the fraction) is stored in bits 0 to 51, the exponent in bits 52 to 62, and the sign in bit 63:

值(又名分数/尾数):52 位 (0 - 51)
指数:11 位 (52 - 62)
符号:1 位(63)

另请阅读这篇文章 here .

关于javascript - JavaScript 如何决定为数值分配多大的内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44643606/

相关文章:

javascript - 当屏幕宽度 > 700px 时隐藏 JavaScript?

javascript - 使用 Google 应用程序脚本将文本从 PDF 转换为文本

linux - 另一个程序是否有可能在分配后重新使用空闲内存,然后由其他已经运行的程序释放?

java - 内存中的图像消耗的内存远多于其文件大小

android - 使用 Handler 可能发生内存泄漏?

c++ - 内存没有正确对齐?

c++ - 在 vector 中的指针中使用删除运算符

linux - 如何从用户空间访问linux中的物理内存?

javascript - 未捕获的类型错误 : Cannot read property '$store' of null

javascript - 2 条 SVG 线重叠时的颜色问题