- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
根据 node.js 文档,一个 Node 在 32 位版本上有 512meg 的限制,在 64 位版本上有 1.4gig 的限制。 Chrome AFAICT 的限制类似。 (+/- 25%)
那么,为什么这段代码从不使用超过 ~424meg 的内存却会耗尽内存?
这是代码(代码是废话。这个问题不是关于代码在做什么,而是关于代码为什么失败)。
var lookup = 'superCaliFragilisticExpialidosiousThispartdoesnotrealllymattersd';
function encode (num) {
return lookup[num];
}
function makeString(uint8) {
var output = '';
for (var i = 0, length = uint8.length; i < length; i += 3) {
var temp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2]);
output += encode(temp >> 18 & 0x3F) + encode(temp >> 12 & 0x3F) + encode(temp >> 6 & 0x3F) + encode(temp & 0x3F);
}
return output;
}
function test() {
var big = new Uint8Array(64 * 1024 * 1024 + 2); // multiple of 3
var str = makeString(big);
console.log("big:", big.length);
console.log("str:", str.length);
}
test();
如您所见,makeString
通过一次附加 4 个字符来构建一个字符串。在这种情况下,它将构建一个长度为 89478988 (180meg) 的大字符串。由于 output
被追加,最后一次追加字符时,内存中将有 2 个字符串。旧的有 89478984 个字符,最后一个有 89478988 个字符。GC 应该收集任何其他使用的内存。
因此,64meg(原始数组)+ 180meg * 2 = 424meg。远低于 v8 限制。
但是,如果您运行示例,它将因内存不足而失败
<--- Last few GCs --->
3992 ms: Scavenge 1397.9 (1458.1) -> 1397.9 (1458.1) MB, 0.2 / 0 ms (+ 1.5 ms in 1 steps since last GC) [allocation failure] [incremental marking delaying mark-sweep].
4450 ms: Mark-sweep 1397.9 (1458.1) -> 1397.9 (1458.1) MB, 458.0 / 0 ms (+ 2.9 ms in 2 steps since start of marking, biggest step 1.5 ms) [last resort gc].
4909 ms: Mark-sweep 1397.9 (1458.1) -> 1397.9 (1458.1) MB, 458.7 / 0 ms [last resort gc].
$ node foo.js
<--- JS stacktrace --->
==== JS stack trace =========================================
Security context: 0x3a8521e3ac1 <JS Object>
2: makeString(aka makeString) [/Users/gregg/src/foo.js:~6] [pc=0x1f83baf53a3b] (this=0x3a852104189 <undefined>,uint8=0x2ce813b51709 <an Uint8Array with map 0x32f492c0a039>)
3: test(aka test) [/Users/gregg/src/foo.js:19] [pc=0x1f83baf4df7a] (this=0x3a852104189 <undefined>)
4: /* anonymous */ [/Users/gregg/src/foo.js:24] [pc=0x1f83baf4d9e5] (this=0x2ce813b...
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory
Abort trap: 6
Node 4.2.4和5.6.0都试过了
那么,问题是为什么内存不足?
一些我尝试过的东西。
我尝试加入 block
我没有无限期地附加到 output
,而是尝试检查它是否大于某个大小(如 8k)。如果是这样,我把它放在一个数组中,然后将输出重置为空字符串。
通过执行此操作,输出
永远不会超过 8k。该阵列持有180meg + 簿记。所以 180meg + 8k 比 180meg + 小很多180兆。它仍然耗尽内存。现在,在那个过程的最后,我加入数组,此时它实际上会使用更多内存(180meg + 180meg + 簿记)。但是,v8 在到达之前就崩溃了行。
我尝试将编码更改为
function encode(num) {
return 'X';
}
在这种情况下,它实际上运行到完成!所以我想,“啊哈!该问题必须与 lookup[num]
生成相关每次调用一个新字符串?所以我尝试了...
将 lookup
更改为字符串数组
var lookup = Array.prototype.map.call(
'superCaliFragilisticExpialidosiousThispartdoesnotrealllymattersd',
function(c) {
return c;
});
内存不足
这似乎是 v8 中的错误?由于此代码,它无法以某种奇怪的方式对未使用的字符串进行 GC,尽管 #2 与 #3 很奇怪,因为它们在内存使用方面看起来是等效的。
为什么 v8 在这些情况下会耗尽内存? (是否有解决方法)
最佳答案
TL;DR:您的示例是 v8 内部字符串表示之一的病态案例。您可以通过偶尔索引到 output
来修复它(有关原因的信息如下)。
首先,我们可以使用heapdump
查看垃圾收集器在做什么:
上面的快照是在 Node 内存不足之前不久拍摄的。如您所见,大多数事情看起来都很正常:我们看到两个字符串(非常大的 output
和要添加的小块),三个对同一数组 big
的引用(大约 64MB,与我们的预期相似),以及许多看起来并不异常的小项目。
但是,有一件事很突出:output
是一个惊人的 1.4+ GB。在拍摄快照时,它大约有 8000 万个字符长,因此假设每个字符 2 个字节,大约 160 MB。这怎么可能?
也许这与 v8 的内部字符串表示有关。引用 mraleph :
There are two types [of v8 strings] (actually more, but for the problem at hand only these two are important):
- flat strings are immutable arrays of characters
- cons strings are pairs of strings, result of concatenation.
If you concat a and b you get a cons-string (a, b) that represents result of concatenation. If you later concat d to that you get another cons-string ((a, b), d).
Indexing into such a "tree-like" string is not O(1) so to make it faster V8 flattens the string when you index: copies all characters into a flat string.
那么 v8 是否可以将 output
表示为一棵巨树?一种检查方法是强制 v8 压平字符串(如上面 mraleph 所建议的),例如通过在 for
循环内定期索引到 output
:
if (i % 10000000 === 0) {
// We don't do it at each iteration since it's relatively expensive.
output[0];
}
果然,程序成功运行了!
仍然存在一个问题:为什么上面的版本 2 可以运行?在那种情况下,v8 似乎能够优化大多数字符串连接(所有右侧的连接,它们都转换为 4 元素数组上的按位运算)。
关于javascript - 为什么在这种情况下 v8 会耗尽内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35354801/
尝试使用集成到 QTCreator 的表单编辑器,但即使我将插件放入 QtCreator.app/Contents/MacOS/designer 也不会显示。不过,相同的 dylib 文件确实适用于独
在此代码示例中。 “this.method2();”之后会读到什么?在返回returnedValue之前会跳转到method2()吗? public int method1(int returnedV
我的项目有通过gradle配置的依赖项。我想添加以下依赖项: compile group: 'org.restlet.jse', name: 'org.restlet.ext.apispark', v
我将把我们基于 Windows 的客户管理软件移植到基于 Web 的软件。我发现 polymer 可能是一种选择。 但是,对于我们的使用,我们找不到 polymer 组件具有表格 View 、下拉菜单
我的项目文件夹 Project 中有一个文件夹,比如 ED 文件夹,当我在 Eclipse 中指定在哪里查找我写入的文件时 File file = new File("ED/text.txt"); e
这是奇怪的事情,这个有效: $('#box').css({"backgroundPosition": "0px 250px"}); 但这不起作用,它只是不改变位置: $('#box').animate
这个问题在这里已经有了答案: Why does OR 0 round numbers in Javascript? (3 个答案) 关闭 5 年前。 Mozilla JavaScript Guide
这个问题在这里已经有了答案: Is the function strcmpi in the C standard libary of ISO? (3 个答案) 关闭 8 年前。 我有一个问题,为什么
我目前使用的是共享主机方案,我不确定它使用的是哪个版本的 MySQL,但它似乎不支持 DATETIMEOFFSET 类型。 是否存在支持 DATETIMEOFFSET 的 MySQL 版本?或者有计划
研究 Seam 3,我发现 Seam Solder 允许将 @Named 注释应用于包 - 在这种情况下,该包中的所有 bean 都将自动命名,就好像它们符合条件一样@Named 他们自己。我没有看到
我知道 .append 偶尔会增加数组的容量并形成数组的新副本,但 .removeLast 会逆转这种情况并减少容量通过复制到一个新的更小的数组来改变数组? 最佳答案 否(或者至少如果是,则它是一个错
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
noexcept 函数说明符是否旨在 boost 性能,因为生成的对象中可能没有记录异常的代码,因此应尽可能将其添加到函数声明和定义中?我首先想到了可调用对象的包装器,其中 noexcept 可能会产
我正在使用 Angularjs 1.3.7,刚刚发现 Promise.all 在成功响应后不会更新 angularjs View ,而 $q.all 会。由于 Promises 包含在 native
我最近发现了这段JavaScript代码: Math.random() * 0x1000000 10.12345 10.12345 >> 0 10 > 10.12345 >>> 0 10 我使用
我正在编写一个玩具(物理)矢量库,并且遇到了 GHC 坚持认为函数应该具有 Integer 的问题。是他们的类型。我希望向量乘以向量以及标量(仅使用 * ),虽然这可以通过仅使用 Vector 来实现
PHP 的 mail() 函数发送邮件正常,但 Swiftmailer 的 Swift_MailTransport 不起作用! 这有效: mail('user@example.com', 'test
我尝试通过 php 脚本转储我的数据,但没有命令行。所以我用 this script 创建了我的 .sql 文件然后我尝试使用我的脚本: $link = mysql_connect($host, $u
使用 python 2.6.4 中的 sqlite3 标准库,以下查询在 sqlite3 命令行上运行良好: select segmentid, node_t, start, number,title
我最近发现了这段JavaScript代码: Math.random() * 0x1000000 10.12345 10.12345 >> 0 10 > 10.12345 >>> 0 10 我使用
我是一名优秀的程序员,十分优秀!