• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --expose-gc
2'use strict';
3
4const common = require('../common');
5if (process.config.variables.asan)
6  common.skip('ASAN messes with memory measurements');
7
8const assert = require('assert');
9const net = require('net');
10
11// Tests that, when receiving small chunks, we do not keep the full length
12// of the original allocation for the libuv read call in memory.
13
14let client;
15let baseRSS;
16const receivedChunks = [];
17const N = 250000;
18
19const server = net.createServer(common.mustCall((socket) => {
20  baseRSS = process.memoryUsage.rss();
21
22  socket.setNoDelay(true);
23  socket.on('data', (chunk) => {
24    receivedChunks.push(chunk);
25    if (receivedChunks.length < N) {
26      client.write('a');
27    } else {
28      client.end();
29      server.close();
30    }
31  });
32})).listen(0, common.mustCall(() => {
33  client = net.connect(server.address().port);
34  client.setNoDelay(true);
35  client.write('hello!');
36}));
37
38process.on('exit', () => {
39  global.gc();
40  const bytesPerChunk =
41    (process.memoryUsage.rss() - baseRSS) / receivedChunks.length;
42  // We should always have less than one page (usually ~ 4 kB) per chunk.
43  assert(bytesPerChunk < 650, `measured ${bytesPerChunk} bytes per chunk`);
44});
45