【发布时间】:2017-03-20 16:48:37
【问题描述】:
在 Excel 2016 的 office js 的共享 API 中使用异步函数时,会导致内存泄漏,特别是调用 binding.setDataAsnyc,它永远不会释放写入的数据。(泄漏在 Internet Explorer 在 excel 中运行插件的进程(它是 32 位版本))。
例子:
//1 Miliion row by 10 columns data parsed from a csv usually in chunks
var data = [];
var i,j;
for (i=0; i<1000000; i++) {
row = [];
for(j=0; j<10; j++) {
row.push("data" + i + "" + j);
}
data.push(row);
}
var limit = 10000;
var next = function (step) {
var columnLetter = getExcelColumnName(data[0].length);
var startRow = step * limit + 1;
var endRow = start + limit;
if (data.length < startRow)
return;
var values = data.slice(startRow - 1, endRow - 1);
var range = "A" + startRow + ":" + columnLetter + "" + endRow;
Office.context.document.bindings.addFromNamedItemAsync(range,
Office.CoercionType.Matrix, { id: "binding" + step },
function (asyncResult) {
if (asyncResult.status == "failed") {
console.log('Error: ' + asyncResult.error.message);
return;
}
Office.select("bindings#binding" + step).setDataAsync(values,
{
coercionType: Office.CoercionType.Matrix
}, function (asyncResult) {
//Memory keeps Increasing on this callback
if (asyncResult.status == Office.AsyncResultStatus.Failed) {
console.log("Action failed with error: " + asyncResult.error.message);
return;
}
next(step++);
});
});
}
next(0);
我尝试在setDataAsync 之后释放每个绑定,但内存仍然存在。回收内存的唯一方法是重新加载插件。
我尝试了另一种为范围赋值的方法:
range.values = values;
它不会泄漏,但花费的时间是 setDataAsync 的 3 倍(1M 行 x 10 列大约需要 210 秒),而 setDataAsync 大约需要 70 秒,但当然会泄漏并在该请求中消耗 1.1 GB 内存.
我也试过table.rows.add(null, values);,但性能更差。
我在没有setdataAsync 的情况下测试了相同的代码(立即调用 next)并且没有发生内存泄漏。
有其他人经历过吗? 无论如何,它周围有释放记忆吗? 如果没有,除了这3种方法也很快,还有其他方法可以在Excel中填充大量数据吗?
【问题讨论】:
标签: excel memory-leaks ms-office office-js office-addins