Why is socket.io file emission using so much memory?

Viewed 87

I have a NestJS/Node.js application using socket.io for websocket communication. In this particular case, while trying to send a BufferArray content nearly as big as 1MB, our server (with 500MB of RAM limit) crashes. Why is the socket connection demanding so much memory if the "file" being sent is just 1MB big?

This is the section of the app that listens for a ws command coming from the client and sends the content back:

@SubscribeMessage('report')
async listenToReportRequests(
    @MessageBody() data: any,
    @ConnectedSocket() client: Socket
) {
    console.log('Received report request');
    const params = data['0'];
    const reportType = data['1'];
    const timezone = data['2'];

    const report = await this.analyticsService.getExcelReport(
        params as IExcelReportOptions,
        reportType as EExcelType,
        timezone,
    );

    console.log('Finished generating the report', report.byteLength);
    client.emit('sendReport', report);
    console.log('Emitted report');
}

The crash log is shown as:

<--- Last few GCs --->

[124:0x53f0d40] 78114918 ms: Mark-sweep (reduce) 251.5 (258.0) -> 248.1 (255.3) MB, 1204.1 / 0.0 ms  (average mu = 0.147, current mu = 0.014) allocation failure scavenge might 
not succeed
[124:0x53f0d40] 78116398 ms: Mark-sweep (reduce) 251.3 (257.5) -> 249.2 (256.3) MB, 1476.4 / 0.0 ms  (average mu = 0.075, current mu = 0.003) allocation failure scavenge might 
not succeed


<--- JS stacktrace --->

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
 1: 0xa3aaf0 node::Abort() [node]
 2: 0x970199 node::FatalError(char const*, char const*) [node]
 3: 0xbba45e v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [nodal::Isolate*, char const*, bool) [node]
 5: 0xd769e5  [node]
 6: 0xd7756f  [node]
 7: 0xd853ab v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node]
 8: 0xd88f6c v8::internal::Heap::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [node]
 9: 0xd5764b v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationType, v8::internal::AllocationOrigin) [node]
10: 0x109fc0f v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [node]
11: 0x1448e19  [node]
Aborted (core dumped)

The Node.js version used is 14-slim and the socket libraries' versions are

"@types/socket.io": "^2.1.4", "@nestjs/platform-socket.io": "^7.6.13", "@nestjs/websockets": "^7.6.13",

Thank you very much in advance!

0 Answers
Related