简体   繁体   English

javascript字符串内存分配

[英]javascript string memory allocation

How does Javascript allocates memory for strings? Javascript如何为字符串分配内存? I ran this simple code in chrome: 我在chrome中运行了以下简单代码:

var s = 'a';
var i;
for (i = 0; i < 5000000; i++) {
    s += 'b';
}

var s2 = s;
s2[4000000] = 'a';

and attached Windbg to chrome, added conditional breakpoint in HeapAlloc, VirtualAlloc, and VirtualAllocEx with condition "requested bytes parameter > 4.5M". 并将Windbg附加到Chrome,在HeapAlloc,VirtualAlloc和VirtualAllocEx中添加条件断点,条件为“请求的字节参数> 4.5M”。 when the above code ran I didn't see any such big allocation in the debugger. 当上面的代码运行时,我在调试器中没有看到这么大的分配。
I expected that at least for s2 I'll see such allocation. 我希望至少对于s2我会看到这样的分配。

when the above code ran I didn't see any such big allocation in the debugger. 当上面的代码运行时,我在调试器中没有看到这么大的分配。

Here s += 'b'; 这里s += 'b'; has static string 'b' . 具有静态字符串'b' It is very likely that string intering is at play. 字符串插入很有可能在起作用。

Try making these random strings or perhaps numbers as strings. 尝试将这些随机字符串或数字作为字符串。

I expected that at least for s2 I'll see such allocation. 我希望至少对于s2,我会看到这样的分配。

Here s2 is just a reference ienot creating more allocation. 这里s2只是一个参考,即不创建更多分配。 Try copying the whole array. 尝试复制整个数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM