sql.js FATAL ERROR: invalid array length Allocation failed - JavaScript heap out of memory - node.js

We are trying to insert (1200 records) into sqlite database (existing database of size 106 MB) from node js, using sqlite-chiper node module and
getting the below error.
We have even increased the --max-old-space-size=10192 till 10GB still we are getting the below error.
<--- Last few GCs --->
[16316:00000292A20A0C10] 15038 ms: Scavenge 1178.9 (1213.6) -> 1178.9 (1213.6) MB, 115.4 / 0.0 ms (average mu = 0.999, current mu = 0.999) allocation failure
[16316:00000292A20A0C10] 16143 ms: Mark-sweep 1752.7 (1787.4) -> 1743.6 (1779.3) MB, 517.7 / 0.0 ms (+ 5.4 ms in 12 steps since start of marking, biggest step 0.9 ms, walltime since start of marking 2513 ms) (average mu = 0.945, current mu = 0.933) al
<--- JS stacktrace --->
==== JS stack trace =========================================
0: ExitFrame [pc: 00007FF7BDBA2C6D]
Security context: 0x02fbb1f408a1 <JSObject>
1: write [0000029FB013E4D9] [E:\Product\Trunk\node_modules\sql.js\js\sql.js:~2114] [pc=00000379B65AE651](this=0x029fb01375f9 <Object map = 0000014672038741>,0x038bd5a3ff79 <JSObject>,0x032891c17b29 <Int8Array map = 0000014672002781>,6378736,4096,112812032,0x028694c804a9 <undefined>)
2: write [0000032891C1CAB9] [E:\Product\Trunk\node_modules\s...
FATAL ERROR: invalid array length Allocation failed - JavaScript heap out of memory
Writing Node.js report to file: report.20200831.233420.16316.0.001.json
Node.js report completed
1: 00007FF7BCFD094F napi_wrap+124431
2: 00007FF7BCF72696 public: bool __cdecl v8::base::CPU::has_sse(void)const __ptr64+34502
3: 00007FF7BCF73356 public: bool __cdecl v8::base::CPU::has_sse(void)const __ptr64+37766
4: 00007FF7BD776F4E private: void __cdecl v8::Isolate::ReportExternalAllocationLimitReached(void) __ptr64+94
5: 00007FF7BD75EF91 public: class v8::SharedArrayBuffer::Contents __cdecl v8::SharedArrayBuffer::Externalize(void) __ptr64+833
6: 00007FF7BD62C85C public: static void __cdecl v8::internal::Heap::EphemeronKeyWriteBarrierFromCode(unsigned __int64,unsigned __int64,class v8::internal::Isolate * __ptr64)+1436
7: 00007FF7BD64287D private: class v8::internal::HeapObject __cdecl v8::internal::Factory::AllocateRawFixedArray(int,enum v8::internal::AllocationType) __ptr64+61
8: 00007FF7BD649C34 private: class v8::internal::Handle<class v8::internal::FixedArray> __cdecl v8::internal::Factory::NewFixedArrayWithFiller(enum v8::internal::RootIndex,int,class v8::internal::Object,enum v8::internal::AllocationType) __ptr64+52
9: 00007FF7BD649BF1 public: class v8::internal::Handle<class v8::internal::FixedArray> __cdecl v8::internal::Factory::NewFixedArray(int,enum v8::internal::AllocationType) __ptr64+65
10: 00007FF7BD52AD9F public: class v8::Isolate * __ptr64 __cdecl v8::Message::GetIsolate(void)const __ptr64+7663
11: 00007FF7BD3DE55A public: class v8::internal::Object __cdecl v8::internal::OrderedHashMap::ValueAt(int) __ptr64+61274
12: 00007FF7BDBA2C6D public: virtual bool __cdecl v8::internal::SetupIsolateDelegate::SetupHeap(class v8::internal::Heap * __ptr64) __ptr64+567949
13: 00000379B65AE651
Any suggestion on how to resolve this problem ?

Since we are getting memory error in sql.js, we have moved to sqlite3 for node js to resolve this problem

Related

Angular on Azure DevOps running out of memory even with max_old_space_size set

I have an angular project that I recently upgraded to Angular 13. This project is building in Azure DevOps.
About 1 in 3 builds fail due to memory issues. I get lots of different errors each run, but they all are about memory.
I have scoured the internet, and I have been using the max_old_space_size but that doesn't seem to solve my problem. Is there a better way to make the angular build actually use less memory during build?
Here are the 3 errors I often see. Note each run is different, and sometimes it builds fine.
##[error]Error(0,0): Error [main.5aa4446f2024043f.js: ;]DataCloneError: Data cannot be cloned, out of memory.
Error : Optimization error [main.5aa4446f2024043f.js]: DataCloneError: Data cannot be cloned, out of memory. [D:\a\1\s\MyApp\MyApp.Web\MyApp.Web.csproj]
at WorkerInfo.postTask (D:\a\1\s\MyApp\MyApp.Web\ClientApp\node_modules\piscina\dist\src\index.js:305:23)
at ThreadPool._onWorkerAvailable (D:\a\1\s\MyApp\MyApp.Web\ClientApp\node_modules\piscina\dist\src\index.js:518:24)
at D:\a\1\s\MyApp\MyApp.Web\ClientApp\node_modules\piscina\dist\src\index.js:381:46
at AsynchronouslyCreatedResourcePool.maybeAvailable (D:\a\1\s\MyApp\MyApp.Web\ClientApp\node_modules\piscina\dist\src\index.js:237:17)
at WorkerInfo.onMessage (D:\a\1\s\MyApp\MyApp.Web\ClientApp\node_modules\piscina\dist\src\index.js:424:26)
at WorkerInfo._handleResponse (D:\a\1\s\MyApp\MyApp.Web\ClientApp\node_modules\piscina\dist\src\index.js:289:14)
at MessagePort.<anonymous> (D:\a\1\s\MyApp\MyApp.Web\ClientApp\node_modules\piscina\dist\src\index.js:258:51)
at MessagePort.[nodejs.internal.kHybridDispatch] (node:internal/event_target:643:20)
at MessagePort.exports.emitMessage (node:internal/per_context/messageport:23:28)
Another one here:
#FailureMessage Object: 0000001D9D3F9EB0
#
# Fatal error in , line 0
# Fatal process out of memory: Zone
#
#
#
#FailureMessage Object: 0000001D9D2FA470
1: 00007FF783AF79CF public: __cdecl v8::internal::CodeObjectRegistry::~CodeObjectRegistry(void) __ptr64+114207
2: 00007FF783A13E9F public: class std::basic_ostream<char,struct std::char_traits<char> > & __ptr64 __cdecl std::basic_ostream<char,struct std::char_traits<char> >::operator<<(__int64) __ptr64+65103
3: 00007FF7846F26C2 void __cdecl V8_Fatal(char const * __ptr64,...)+162
4: 00007FF7843A55DE public: void __cdecl v8::SharedArrayBuffer::Externalize(class std::shared_ptr<class v8::BackingStore> const & __ptr64) __ptr64+286
5: 00007FF783F3FA57 private: unsigned __int64 __cdecl v8::internal::Zone::NewExpand(unsigned __int64) __ptr64+279
6: 00007FF783D4EAE4 public: virtual char const * __ptr64 __cdecl disasm::NameConverter::NameOfXMMRegister(int)const __ptr64+17108
7: 00007FF7847752F0 public: void __cdecl v8::internal::compiler::Schedule::AddGoto(class v8::internal::compiler::BasicBlock * __ptr64,class v8::internal::compiler::BasicBlock * __ptr64) __ptr64+48
8: 00007FF7848E47D2 private: void __cdecl v8::internal::compiler::Scheduler::ComputeSpecialRPONumbering(void) __ptr64+3490
9: 00007FF7848E3B88 private: void __cdecl v8::internal::compiler::Scheduler::ComputeSpecialRPONumbering(void) __ptr64+344
10: 00007FF7848E752D private: static void __cdecl v8::internal::compiler::Scheduler::PropagateImmediateDominators(class v8::internal::compiler::BasicBlock * __ptr64)+3101
11: 00007FF7848E29A5 private: void __cdecl v8::internal::compiler::Scheduler::BuildCFG(void) __ptr64+277
12: 00007FF7848E380E public: static class v8::internal::compiler::Schedule * __ptr64 __cdecl v8::internal::compiler::Scheduler::ComputeSchedule(class v8::internal::Zone * __ptr64,class v8::internal::compiler::Graph * __ptr64,class v8::base::Flags<enum v8::internal::compiler::Scheduler::Flag,int>,class v8::internal::TickCounter * __ptr64,class v8::internal::ProfileDataFromFile const * __ptr64)+270
13: 00007FF7847A2CA9 public: bool __cdecl v8::internal::compiler::LoopPeeler::CanPeel(class v8::internal::compiler::LoopTree::Loop * __ptr64) __ptr64+185
14: 00007FF7847A83FB public: class v8::internal::compiler::LifetimePosition __cdecl v8::internal::compiler::LiveRange::NextStart(void)const __ptr64+2043
15: 00007FF7847A3BC1 public: class v8::internal::compiler::LifetimePosition __cdecl v8::internal::compiler::LiveRange::End(void)const __ptr64+177
16: 00007FF78433CFF1 public: enum v8::internal::CompilationJob::Status __cdecl v8::internal::OptimizedCompilationJob::ExecuteJob(class v8::internal::RuntimeCallStats * __ptr64,class v8::internal::LocalIsolate * __ptr64) __ptr64+49
17: 00007FF78430DF49 private: void __cdecl v8::internal::OptimizingCompileDispatcher::CompileNext(class v8::internal::OptimizedCompilationJob * __ptr64,class v8::internal::LocalIsolate * __ptr64) __ptr64+57
18: 00007FF78430EA1A public: void __cdecl v8::internal::OptimizingCompileDispatcher::QueueForOptimization(class v8::internal::OptimizedCompilationJob * __ptr64) __ptr64+714
19: 00007FF783A1682D public: class std::basic_ostream<char,struct std::char_traits<char> > & __ptr64 __cdecl std::basic_ostream<char,struct std::char_traits<char> >::operator<<(__int64) __ptr64+75741
20: 00007FF783B46EDD uv_poll_stop+557
21: 00007FF784960120 public: class v8::internal::compiler::Operator const * __ptr64 __cdecl v8::internal::compiler::RepresentationChanger::Uint32OverflowOperatorFor(enum v8::internal::compiler::IrOpcode::Value) __ptr64+146416
22: 00007FF80C834ED0 BaseThreadInitThunk+16
23: 00007FF80D26E39B RtlUserThreadStart+43
One More:
<--- Last few GCs --->
[6640:000001CC0BCB3FC0] 21334 ms: Scavenge 87.4 (107.8) -> 78.6 (111.3) MB, 9.5 / 0.0 ms (average mu = 0.992, current mu = 0.990) allocation failure
[6640:000001CC0BCB3FC0] 21397 ms: Scavenge 91.4 (111.8) -> 82.6 (115.5) MB, 21.7 / 0.0 ms (average mu = 0.992, current mu = 0.990) allocation failure
[6640:000001CC0BCB3FC0] 21783 ms: Scavenge 95.6 (116.0) -> 86.7 (119.5) MB, 158.3 / 0.0 ms (average mu = 0.992, current mu = 0.990) allocation failure
<--- JS stacktrace --->
<--- Last few GCs --->
[6640:000001CC0BCB3FC0] 21334 ms: Scavenge 87.4 (107.8) -> 78.6 (111.3) MB, 9.5 / 0.0 ms (average mu = 0.992, current mu = 0.990) allocation failure
[6640:000001CC0BCB3FC0] 21397 ms: Scavenge 91.4 (111.8) -> 82.6 (115.5) MB, 21.7 / 0.0 ms (average mu = 0.992, current mu = 0.990) allocation failure
[6640:000001CC0BCB3FC0] 21783 ms: Scavenge 95.6 (116.0) -> 86.7 (119.5) MB, 158.3 / 0.0 ms (average mu = 0.992, current mu = 0.990) allocation failure
<--- JS stacktrace --->
<--- Last few GCs --->
[6640:000001CC0BD4A7B0] 21478 ms: Scavenge 56.2 (76.6) -> 47.2 (80.1) MB, 17.0 / 0.0 ms (average mu = 0.996, current mu = 0.996) allocation failure
[6640:000001CC0BD4A7B0] 22725 ms: Scavenge 60.5 (80.8) -> 51.3 (82.6) MB, 1020.4 / 0.0 ms (average mu = 0.996, current mu = 0.996) allocation failure
[6640:000001CC0BD4A7B0] 27389 ms: Scavenge 62.7 (83.1) -> 54.9 (85.6) MB, 1839.8 / 0.0 ms (average mu = 0.996, current mu = 0.996) allocation failure
<--- JS stacktrace --->
##[error]EXEC(0,0): Error : MarkCompactCollector: young object promotion failed Allocation failed - JavaScript heap out of memory
EXEC : FATAL error : MarkCompactCollector: young object promotion failed Allocation failed - JavaScript heap out of memory [D:\a\1\s\BehaviorLive\BehaviorLive.Web\BehaviorLive.Web.csproj]
##[error]EXEC(0,0): Error : MarkCompactCollector: young object promotion failed Allocation failed - JavaScript heap out of memory
EXEC : FATAL error : MarkCompactCollector: young object promotion failed Allocation failed - JavaScript heap out of memory [D:\a\1\s\BehaviorLive\BehaviorLive.Web\BehaviorLive.Web.csproj]
##[error]EXEC(0,0): Error : MarkCompactCollector: young object promotion failed Allocation failed - JavaScript heap out of memory
You didn't jot it down so I ask. Did you use command like:
cross-env NODE_OPTIONS=--max-old-space-size=8192 npm build
It depends on the Azure Agent that you use for deployment. The memory available for the agent might not be enough to complete the build, especially when the agent is trying to process parallel jobs.
You can debug and find exactly what amount of memory will get the job done in your local system.
You can start by keeping
max_old_space_size=1024
And increase size untill build succeeds. I found ours work with 2048.
Now if you can get a dedicated agent which can allocate atleast that amount of memory to run the job, then you will have a reliable solution.
EDIT - Another solution would be to try a different build tool. I assume you are using webpack, there could be other bundlers out there that are more efficient.

Nodejs outof memory when reading big file

I have the following code to stream a file:
const lineReader = readline.createInterface({
input: fs.createReadStream(filePath),
});
lineReader.on('line', (line) => {
console.log(line);
LineEntity.create({line: "some data"});//write some data to db
});
when executing the above code, I'm getting out of memory exception after 30-60 seconds.
when I execute the following, it runs perfectly without stopping...
//just to show that the problem is not with accessing the database
while(true){
await LineEntity.create({line: "some data"});//and without await
}
when I execute the following, it runs perfect also.
lineReader.on('line', (line) => {
console.log(line);
});
The problem is with the combination of the file reading and writing to the db.
Separately, they run perfect. together I'm getting out of memory exception
The error:
<--- Last few GCs --->
[3903:0x47b4180] 42294 ms: Mark-sweep 2032.1 (2072.5) -> 2028.0 (2053.4) MB, 161.9 / 0.0 ms (+ 602.9 ms in 141 steps
since start of marking, biggest step 6.3 ms, walltime since start of marking 838 ms) (average mu = 0.205, current mu =
0.172) finalize[3903:0x47b4180] 43117 ms: Mark-sweep 2034.1 (2053.4) -> 2024.0 (2067.7) MB, 805.3 / 0.0 ms (average
mu = 0.120, current mu = 0.020) allocation failure scavenge might not succeed
<--- JS stacktrace --->
==== JS stack trace =========================================
0: ExitFrame [pc: 0x140dcd9]
1: StubFrame [pc: 0x148be23]
Security context: 0x05a03e5408d1 <JSObject>
2: save [0x1f10ab61b579] [/home/ron/work/hawking/node_modules/sequelize/lib/model.js:3950] [bytecode=0x22c0629957a1
offset=2439](this=0x02d3eccd2f21 <Model map = 0x2e79bc5fda69>,0x02d3eccd2ee9 <Object map = 0x4a68f580439>)
3: /* anonymous */ [0x2b6513fc9bf9](this=0x2ccc7e8c2b21 <JSGlobal Object>,0x1460087004b1 <undefined>)...
FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
1: 0xa1a640 node::Abort() [node]
2: 0xa1aa4c node::OnFatalError(char const*, char const*) [node]
3: 0xb9a68e v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [node]
4: 0xb9aa09 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [node]
5: 0xd57c85 [node]
6: 0xd58316 v8::internal::Heap::RecomputeLimits(v8::internal::GarbageCollector) [node]
7: 0xd64bd5 v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [node]
8: 0xd65a85 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8
::GCCallbackFlags) [node]
9: 0xd6853c v8::internal::Heap::AllocateRawWithRetryOrFail(int, v8::internal::AllocationType, v8::internal::AllocationO
rigin, v8::internal::AllocationAlignment) [node]
What am I doing wrong?
Thanks

node canvas getImageData() out of memory

I have a nodejs application that transforms images.
I run my application in pm2 with 32gb of ram.
When trying to get the image data of an image i am getting this error.
I have tried to increase the node js memory limit using --max-old-space-size
tried to increase it to 8192 as well as 32678 but it I still get an error.
m[9161:0x41069d0] 18179 ms: Mark-sweep 22.3 (29.2) -> 22.0 (25.2) MB, 23.0 / 0.0 ms (+ 1.5 ms in 5 steps since start of marking, biggest step 0.8 ms, walltime since start of marking 55 ms) (average mu = 0.998, current mu = 0.963) finalize incremental m[9161:0x41069d0] 27013 ms: Mark-sweep 23.2 (25.2) -> 22.2 (25.2) MB, 3.8 / 0.0 ms (+ 0.1 ms in 2 steps since start of marking, biggest step 0.1 ms, walltime since start of marking 168 ms) (average mu = 0.999, current mu = 1.000) finalize incremental m
<--- JS stacktrace --->
==== JS stack trace =========================================
Security context: 0x21639e1c08d1 <JSObject>
0: builtin exit frame: getImageData(this=0x0529133cc801 <CanvasRenderingContext2D map = 0x381c66f8e359>,32150,22958,0,0,0x0529133cc801 <CanvasRenderingContext2D map = 0x381c66f8e359>)
1: getPoints [0x324589ad9b31] [/var/apps/print-export-worker/getPoints.js:11] [bytecode=0x340f496a0d29 offset=86](this=0x2d04988cd319 <JSGlobal Object>,0x0529133cc801 <CanvasRenderingContext2D map = 0x381c...
FATAL ERROR: v8::ArrayBuffer::New Allocation failed - process out of memory
1: 0xa09830 node::Abort() [node]
2: 0xa09c55 node::OnFatalError(char const*, char const*) [node]
3: 0xb7d71e v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [node]
4: 0xb7da99 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [node]
5: 0xb7db7b [node]
6: 0xb9b93e [node]
7: 0x7f7f87de455f Context2d::GetImageData(Nan::FunctionCallbackInfo<v8::Value> const&) [/var/apps/print-export-worker/node_modules/canvas/build/Release/canvas.node]
8: 0x7f7f87dd91f9 [/var/apps/print-export-worker/node_modules/canvas/build/Release/canvas.node]
9: 0xbe96a9 [node]
10: 0xbeb497 v8::internal::Builtin_HandleApiCall(int, unsigned long*, v8::internal::Isolate*) [node]
11: 0x13c5c79 [node]
Aborted
It turns out that all I needed to do was to upgrade my node version to 14Lts. There was some limitation in version 12.
The other way of getting the image data from an image without using node-canvas was using sharp like this.
sharp(canvas.toBuffer(), { limitInputPixels: false })
.raw()
.ensureAlpha()
.toBuffer()
.then((img) => {
let imageData = new Uint8ClampedArray(img)
return imageData
})
.catch(err => {
console.log('ERROR IN SHARP', err)
reject(err)
})
It has to use limitInputPixels into false if you are working with large images.

Trying to make a duel feature in Discord bot but while loop is causing FATAL error

So I have a discord bot and I want to make it be able to duel with players. However I kept running into a FATAL ERROR, which I figured was from the while loop. I'm kind of new at this, so is there some sort of rule against using while loop in discord.js? I've been stuck at this for days and I tried googling but I just confused myself even more.
my code:
msg.channel.send(`${taggedUser}, ${msg.guild.members.cache.get(msg.author.id).displayName} wants to duel you. Do you accept? (Reply yes or no.)`);
msg.channel.awaitMessages(m => m.author.id == taggedUser.id, {max: 1, time: 10000})
.then(collected => {
if (collected.first().content.toLowerCase() == 'yes') {
msg.channel.send("Let the battle begin!");
p1points = 100;
p2points = 100;
while (p1points !== 0 || p2points !== 0) {
const randomOption = results[Math.floor(Math.random() * results.length)];
msg.channel.send(`**${player1}** casted ${randomOption.message} (∩•̀ω•́)⊃-⋆`);
p2points = p2points + randomOption.xp;
msg.channel.send(`**${player1}**: ${p1points}, **${player2}**: ${p2points}`);
const randomOption2 = results[Math.floor(Math.random() * results.length)];
msg.channel.send(`**${player2}** casted ${randomOption2.message} ⋆-⊂(•̀ω•́∩)`);
p1points = p1points + randomOption2.xp;
msg.channel.send(`**${player1}**: ${p1points}, **${player2}**: ${p2points}`);
}
if (p1points === 0) {
msg.channel.send(`**${player1}** is DEFEATED. AAAAAAh! (ノ゚Д゚)八(゚Д゚)八(゚Д゚)ノィェーィ!`);
msg.channel.send(`CONGRATULATIONS, ${player2}! (っ◕‿◕)っ :heart:`);
}
if (p2points === 0) {
msg.channel.send(`**${player2}** is DEFEATED. AAAAAAh! (ノ゚Д゚)八(゚Д゚)八(゚Д゚)ノィェーィ!`);
msg.channel.send(`CONGRATULATIONS, ${player1}! (っ◕‿◕)っ :heart:`);
}
}
else if (collected.first().content.toLowerCase() == 'no') {
msg.channel.send("Yikes, are you scared? Maybe next time then!");
}
}).catch(() => {
msg.channel.send('No reply after 10 seconds, duel is cancelled!');
});
Error:
<--- Last few GCs --->
[4:0x3778960] 21250 ms: Mark-sweep 254.7 (258.3) -> 254.2 (258.5) MB, 201.4 / 0.0 ms (average mu = 0.119, current mu = 0.012) allocation failure scavenge might not succeed
[4:0x3778960] 21556 ms: Mark-sweep 255.0 (258.5) -> 254.5 (258.8) MB, 303.1 / 0.0 ms (average mu = 0.059, current mu = 0.012) allocation failure scavenge might not succeed
<--- JS stacktrace --->
==== JS stack trace =========================================
0: ExitFrame [pc: 0x13c5b79]
1: StubFrame [pc: 0x13c6a4d]
Security context: 0x26d9b2a808d1 <JSObject>
2: /* anonymous */ [0x2c9d420f9409] [internal/modules/cjs/loader.js:~605] [pc=0x270b156db629](this=0x2c9d420c0799 <JSFunction Module (sfi = 0x26d9b2ab0d21)>,0x15d111435a01 <String[#7]: ../User>,0x0ac218d43aa1 <JSArray[1]>,0x18752e1c06e9 <false>)
3: /* anonymous */ [0x2c9d420f69f1] [internal/modules/cjs/loader.js:~892] [pc=...
FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
Writing Node.js report to file: report.20200522.160742.4.0.001.json
Node.js report completed
1: 0xa09830 node::Abort() [node]
2: 0xa09c55 node::OnFatalError(char const*, char const*) [node]
3: 0xb7d71e v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [node]
4: 0xb7da99 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [node]
5: 0xd2a1f5 [node]
6: 0xd2a886 v8::internal::Heap::RecomputeLimits(v8::internal::GarbageCollector) [node]
7: 0xd37105 v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [node]
8: 0xd37fb5 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node]
9: 0xd3aa6c v8::internal::Heap::AllocateRawWithRetryOrFail(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [node]
10: 0xd0163b v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationType, v8::internal::AllocationOrigin) [node]
11: 0x104300e v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [node]
12: 0x13c5b79 [node]
Read the full error, specifically this line
FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
Looking for JavaScript heap out of memory brings up some answers like this answer and this one
I made a small guess out of what I see going on in the code you provided.
First, you need to make sure you aren't mixing up your operators. In your snippet example, the way you are using the or operator is telling the while loop to execute until both player's points equal exactly 0. And since you are only adding to the player points and not subtracting the points will never equal 0 unless the .xp property is a negative number.
Secondly, for those times where a players points can end up being less than 0 then I'd recommend doing
while (p1points > 0 && p2points > 0) {
instead of
while (p1points !== 0 && p2points !== 0) {
because if you use > 0 then it'll stop executing whenever one of the players points are exactly 0 or less than 0.

Node.js | JavaScript heap out of memory, but HEAP is only 1GB

I get this error:
FATAL ERROR: invalid table size Allocation failed - JavaScript heap out of memory
but I'm using --max-old-space-size=12288 and I have 16GB of memory
Using process.memoryUsage() I can see the heap only goes up to ~1GB;
Also, the error always happens at the same line of code, namely types.js:~94, which is part of a library I'm using.
This makes me thing that there's a bug somewhere, maybe in the argument I pass to the function, but I can't figure out how to spot it.
What should I look for?
The full error is:
<--- Last few GCs --->
[28425:0x2739620] 406975 ms: Scavenge 3689.5 (3833.4) -> 3673.7 (3833.9) MB, 4.2 / 0.0 ms (average mu = 0.991, current mu = 0.990) allocation failure
[28425:0x2739620] 407874 ms: Scavenge 3728.8 (3873.4) -> 3713.1 (3873.9) MB, 4.5 / 0.0 ms (average mu = 0.991, current mu = 0.990) allocation failure
[28425:0x2739620] 408785 ms: Scavenge 3768.1 (3913.4) -> 3752.4 (3913.9) MB, 4.5 / 0.0 ms (average mu = 0.991, current mu = 0.990) allocation failure
<--- JS stacktrace --->
==== JS stack trace =========================================
0: ExitFrame [pc: 0x2a42914841bd]
Security context: 0x281401b9e6c9 <JSObject>
1: decode [0x1c8f15ad00a9] [/media/Data/dev/btc-crawler/node_modules/bitcoin-protocol/src/types.js:~94] [pc=0x2a429178f88b](this=0x1c8f15acb299 <Object map = 0x736c200e801>,buffer=0xeeb6cf17c29 <Uint8Array map = 0x3d1fa97d04e9>,offset=4,end=0x25b4eaf822e1 <undefined>)
2: _transform [0x38503cda5d21] [/me...
FATAL ERROR: invalid table size Allocation failed - JavaScript heap out of memory
1: 0x89c2f0 node::Abort() [node]
2: 0x89c33c [node]
3: 0xa8f05e v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [node]
4: 0xa8f278 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [node]
5: 0xe7cad2 [node]
6: 0xf88847 v8::internal::HashTable<v8::internal::StringTable, v8::internal::StringTableShape>::New(v8::internal::Isolate*, int, v8::internal::PretenureFlag, v8::internal::MinimumCapacity) [node]
7: 0xf9a3c8 v8::internal::HashTable<v8::internal::StringTable, v8::internal::StringTableShape>::EnsureCapacity(v8::internal::Handle<v8::internal::StringTable>, int, v8::internal::PretenureFlag) [node]
8: 0xf9a58a v8::internal::StringTable::LookupKey(v8::internal::Isolate*, v8::internal::StringTableKey*) [node]
9: 0xfa03f4 v8::internal::StringTable::LookupString(v8::internal::Isolate*, v8::internal::Handle<v8::internal::String>) [node]
10: 0xf678aa v8::internal::LookupIterator::PropertyOrElement(v8::internal::Isolate*, v8::internal::Handle<v8::internal::Object>, v8::internal::Handle<v8::internal::Object>, bool*, v8::internal::LookupIterator::Configuration) [node]
11: 0x1112512 in the code, v8::internal::Runtime::GetObjectProperty(v8::internal::Isolate*, v8::internal::Handle<v8::internal::Object>, v8::internal::Handle<v8::internal::Object>, bool*) [node]
12: 0x1112849 [node]
13: 0x1114f09 v8::internal::Runtime_KeyedGetProperty(int, v8::internal::Object**, v8::internal::Isolate*) [node]
14: 0x2a42914841bd
Line 94 of types.js is the entry point of the decode function, defined as follows:
var buffer12 = struct.Buffer(12)
function decode (buffer, offset, end) {
var bvalue = buffer12.decode(buffer, offset, end)
for (var stop = 0; bvalue[stop] !== 0; ++stop);
for (var i = stop; i < bvalue.length; ++i) {
if (bvalue[i] !== 0) throw new Error('Found a non-null byte after the first null byte in a null-padded string')
}
return bvalue.slice(0, stop).toString('ascii')
}
You have a typo!
The correct usage is node --max-old-space-size=4096 somescript.js
4096 is 4gb in this case, adjust it accordingly in your case...
Run node --v8-options for a list of all valid v8 options,
or check this list of v8 flags

Resources