节点10.15.0:无法创建大矩阵

时间:2019-01-06 00:44:42

标签: node.js strassen

我正在上一门算法课程,并且刚刚了解了Strassen的乘法矩阵算法。我已经在节点中实现了它,并且可以在小型矩阵中使用。我想在一个非常大的矩阵上进行测试,但是当我尝试创建它时,节点核心会转储。我能够创建第一个大小为10000的矩阵,但是当我尝试创建第二个矩阵时会崩溃。完整的堆栈跟踪如下:

> const M2 = randomMatrix(10000);
undefined
> const M3 = randomMatrix(10000);

<--- Last few GCs --->

[55746:0x398dfc0] 614765011 ms: Mark-sweep 1318.6 (1439.7) -> 1318.6 (1439.7) MB, 528.7 / 0.0 ms  (average mu = 0.087, current mu = 0.000) allocation failure scavenge might not succeed
[55746:0x398dfc0] 614765516 ms: Mark-sweep 1318.6 (1439.7) -> 1318.6 (1439.7) MB, 505.1 / 0.0 ms  (average mu = 0.046, current mu = 0.000) last resort GC in old space requested


<--- JS stacktrace --->

==== JS stack trace =========================================

    0: ExitFrame [pc: 0x480f73dbe1d]
Security context: 0x26efb64f8041 <JSObject>
    1: randomMatrix [0x1ffe2ccfb921] [repl:~1] [pc=0x480f76ad37a](this=0x33298d21ad11 <JSGlobal Object>,n=10000)
    2: /* anonymous */ [0x1be321b7adb1] [repl:1] [bytecode=0x1be321b7ad41 offset=26](this=0x33298d21ad11 <JSGlobal Object>)
    3: InternalFrame [pc: 0x480f738ee75]
    4: EntryFrame [pc: 0x480f73892c1]
    5: ExitFrame [pc: 0x480f73dbe1d]
    6: r...

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
 1: 0x8db900 node::Abort() [node]
 2: 0x8db94c  [node]
 3: 0xad6c1e v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [node]
 4: 0xad6e54 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [node]
 5: 0xec44e2  [node]
 6: 0xec45e8 v8::internal::Heap::CheckIneffectiveMarkCompact(unsigned long, double) [node]
 7: 0xed06c2 v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [node]
 8: 0xed0ff4 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node]
 9: 0xed2509 v8::internal::Heap::CollectAllAvailableGarbage(v8::internal::GarbageCollectionReason) [node]
10: 0xed3cce v8::internal::Heap::AllocateRawWithRetryOrFail(int, v8::internal::AllocationSpace, v8::internal::AllocationAlignment) [node]
11: 0xe9d0e4 v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationSpace) [node]
12: 0x113c88e v8::internal::Runtime_AllocateInNewSpace(int, v8::internal::Object**, v8::internal::Isolate*) [node]
13: 0x480f73dbe1d 
Aborted (core dumped)

我包括了用于创建矩阵的代码:

/*
input:  two integers, min and max.
output:  a random integer in [min, max]
*/ 
function randomInteger(min, max) { 
    return Math.floor(Math.random() * (max - min + 1)) + min; 
}

/* 
input:  an integer n. 
output:  an n x n matrix, randomly filled with integers in [0, 100] 
*/ 
function randomMatrix(n) { 
    const M = new Array(n).fill().map( () => Array(n).fill() );

    for(let i = 0; i < n; i++) {
     for(let j = 0; j < n; j++) {
         M[i][j] = randomInteger(0, 100);
        }
    }

 return M;
}

这个问题有解决方案吗?

1 个答案:

答案 0 :(得分:2)

可能是您需要增强节点一点:

node --max-old-space-size=8192 <your-script.js>

可能的相关问题:

how to increase nodejs default memory?

其他阅读内容:

https://medium.com/@vuongtran/how-to-solve-process-out-of-memory-in-node-js-5f0de8f8464c