prosource

Node.js의 메모리 사용량을 모니터링하는 방법은 무엇입니까?

probook 2023. 7. 27. 22:05
반응형

Node.js의 메모리 사용량을 모니터링하는 방법은 무엇입니까?

Node.js의 메모리 사용량을 모니터링하려면 어떻게 해야 합니까?

내장 프로세스 모듈에는 현재 Node.js 프로세스의 메모리 사용량에 대한 통찰력을 제공하는 방법이 있습니다.다음은 64비트 시스템의 Node v0.12.2의 예입니다.

$ node --expose-gc
> process.memoryUsage();  // Initial usage
{ rss: 19853312, heapTotal: 9751808, heapUsed: 4535648 }
> gc();                   // Force a GC for the baseline.
undefined
> process.memoryUsage();  // Baseline memory usage.
{ rss: 22269952, heapTotal: 11803648, heapUsed: 4530208 }
> var a = new Array(1e7); // Allocate memory for 10m items in an array
undefined
> process.memoryUsage();  // Memory after allocating so many items
{ rss: 102535168, heapTotal: 91823104, heapUsed: 85246576 }
> a = null;               // Allow the array to be garbage-collected
null
> gc();                   // Force GC (requires node --expose-gc)
undefined
> process.memoryUsage();  // Memory usage after GC
{ rss: 23293952, heapTotal: 11803648, heapUsed: 4528072 }
> process.memoryUsage();  // Memory usage after idling
{ rss: 23293952, heapTotal: 11803648, heapUsed: 4753376 }

이 간단한 예에서 10M 요소 배열을 할당하면 소비자가 약 80MB를 사용한다는 것을 알 수 있습니다.heapUsed).
V8의 소스 코드(,Array::New , )를 보면 어레이에서 사용하는 메모리가 고정 값에 포인터 크기를 곱한 길이를 더한 값임을 알 수 있습니다.후자는 64비트 시스템에서 8바이트이며, 이는 8 x 10 = 80MB의 메모리 차이가 타당함을 확인합니다.

또한 노드 프로세스가 아닌 글로벌 메모리를 알고 싶은 경우:

var os = require('os');

os.freemem();
os.totalmem();

설명서 참조

node.js:

const formatMemoryUsage = (data) => `${Math.round(data / 1024 / 1024 * 100) / 100} MB`;

const memoryData = process.memoryUsage();

const memoryUsage = {
  rss: `${formatMemoryUsage(memoryData.rss)} -> Resident Set Size - total memory allocated for the process execution`,
  heapTotal: `${formatMemoryUsage(memoryData.heapTotal)} -> total size of the allocated heap`,
  heapUsed: `${formatMemoryUsage(memoryData.heapUsed)} -> actual memory used during the execution`,
  external: `${formatMemoryUsage(memoryData.external)} -> V8 external memory`,
};

console.log(memoryUsage);

/*
{
  "rss": "177.54 MB -> Resident Set Size - total memory allocated for the process execution",
  "heapTotal": "102.3 MB -> total size of the allocated heap",
  "heapUsed": "94.3 MB -> actual memory used during the execution",
  "external": "3.03 MB -> V8 external memory"
}
*/

express.js 프레임워크를 사용하는 경우 express-status-monitor를 사용할 수 있습니다.통합이 매우 쉬우며 CPU 사용량, 메모리 사용량, 응답 시간 등을 그래픽 형식으로 제공합니다.

enter image description here

리눅스/유닉스(참고: Mac OS는 유닉스)에서 사용합니다.top메모리 사용량별로 프로세스를 정렬하려면 ShiftMM(+)을 누릅니다.

윈도우즈에서는 작업 관리자를 사용합니다.

언급URL : https://stackoverflow.com/questions/20018588/how-to-monitor-the-memory-usage-of-node-js

반응형