繁体   English   中英

尝试发送无限期 UDP 数据包时节点崩溃(内存泄漏)

[英]Node crash (memory leak) when try sending indefinite UDP packets

我正在尝试发送依赖于 boolean 的数据包。 即用户可以打开或关闭数据包发送。 我将它留给一个无限循环while (true)以测试它是否真的有效。 在测试时,我无法让它工作,并导致 memory 泄漏:

node .\index.js

<--- Last few GCs --->

[27804:00000211114F63D0]    19049 ms: Mark-sweep (reduce) 4095.2 (4100.8) -> 4094.9 (4102.8) MB, 2062.3 / 0.0 ms  (+ 1.0 ms in 2693 steps since start of marking, biggest step 0.0 ms, walltime since start of marking 3422 ms) (average mu = 0.555, current mu[27804:00000211114F63D0]    21401 ms: Mark-sweep (reduce) 4096.2 (4104.3) -> 4095.9 (4105.1) MB, 2351.2 / 0.0 ms  (+ 0.0 ms in 17 steps since start of marking, biggest step 0.0 ms, walltime since start of marking 2352 ms) (average mu = 0.348, current mu =

<--- JS stacktrace --->

FATAL ERROR: MarkCompactCollector: young object promotion failed Allocation failed - JavaScript heap out of memory
 1: 00007FF79F32021F napi_wrap+109311
 2: 00007FF79F2C5286 v8::internal::OrderedHashTable<v8::internal::OrderedHashSet,1>::NumberOfElementsOffset+33302
 3: 00007FF79F2C6056 node::OnFatalError+294
 4: 00007FF79FB9054E v8::Isolate::ReportExternalAllocationLimitReached+94
 5: 00007FF79FB753CD v8::SharedArrayBuffer::Externalize+781
 6: 00007FF79FA1F85C v8::internal::Heap::EphemeronKeyWriteBarrierFromCode+1516
 7: 00007FF79FA0A49B v8::internal::NativeContextInferrer::Infer+59243
 8: 00007FF79F9EF9CF v8::internal::MarkingWorklists::SwitchToContextSlow+57327
 9: 00007FF79FA0361B v8::internal::NativeContextInferrer::Infer+30955
10: 00007FF79F9FA73D v8::internal::MarkCompactCollector::EnsureSweepingCompleted+6269
11: 00007FF79FA0286E v8::internal::NativeContextInferrer::Infer+27454
12: 00007FF79FA067FB v8::internal::NativeContextInferrer::Infer+43723
13: 00007FF79FA10052 v8::internal::ItemParallelJob::Task::RunInternal+18
14: 00007FF79FA0FFE1 v8::internal::ItemParallelJob::Run+641
15: 00007FF79F9E38E3 v8::internal::MarkingWorklists::SwitchToContextSlow+7939
16: 00007FF79F9FABEC v8::internal::MarkCompactCollector::EnsureSweepingCompleted+7468
17: 00007FF79F9F9434 v8::internal::MarkCompactCollector::EnsureSweepingCompleted+1396
18: 00007FF79F9F6F98 v8::internal::MarkingWorklists::SwitchToContextSlow+87480
19: 00007FF79FA255E1 v8::internal::Heap::LeftTrimFixedArray+929
20: 00007FF79FA276C5 v8::internal::Heap::PageFlagsAreConsistent+789
21: 00007FF79FA1C971 v8::internal::Heap::CollectGarbage+2033
22: 00007FF79FA1AB75 v8::internal::Heap::AllocateExternalBackingStore+1317
23: 00007FF79FA3AF67 v8::internal::Factory::NewFillerObject+183
24: 00007FF79F76AE7F v8::internal::interpreter::JumpTableTargetOffsets::iterator::operator=+1039
25: 00007FF79FC18EFD v8::internal::SetupIsolateDelegate::SetupHeap+463949
26: 000001F5C6286310

这是导致此 memory 泄漏的代码:

const dgram = require("dgram");

const HOST = "192.168.10.161";
const PORT = 1337;
const message = Buffer.from("the time is ...");
const client = dgram.createSocket("udp4");

client.on("close", function () {
  console.log("Client UDP socket closed : BYE!");
});

// This will become while(shouldSend) in the future
while (true) {
  client.send(message, PORT, HOST, function (err, bytes) {
    if (err) {
      throw err;
    }
  });
}

Ofc 使用While(true)将导致 memory 泄漏,因为它基本上是一个无限循环。 它不等待任何东西

所以逻辑应该

//我想阅读一条用户消息,然后通过UDP连接发送它

所以读取用户消息的代码应该是这样的

const readline = require("readline");
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

rl.on("line",(input)=>{
    console.log(input);
    //this is the message the user types
    if (input == "close"){
        rl.close();
    }
})

rl.on("close",()=>{
    console.log("bye");
})

你想要做的是 stream 通过你的客户的消息

在同一个文件中添加您的代码。 除了虽然真实的部分

const dgram = require("dgram");

const HOST = "192.168.10.161";
const PORT = 1337;
const message = Buffer.from("the time is ...");
const client = dgram.createSocket("udp4");

client.on("close", function () {
  console.log("Client UDP socket closed : BYE!");
});

现在添加client.send

// editing this part 


rl.on("line",(input)=>{
    const message = Buffer.from(input);
    client.send(message, PORT, HOST, function (err, bytes) {
    if (err) {
      throw err;
    }
  });
    if (input == "close"){
      // close the connection as well; 
        client.close();
        rl.close();
    }
})

暂无
暂无

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

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