xxxxxxxxxx
const used = process.memoryUsage().heapUsed / 1024 / 1024;
console.log(`The script uses approximately ${Math.round(used * 100) / 100} MB`);
xxxxxxxxxx
import os from 'node:os';
const bytesToMega = (bytes: number) => Math.round((bytes / 1024 / 1024) * 100) / 100;
const getMemoryUse = () => {
const { heapTotal, heapUsed } = process.memoryUsage();
const percentUsed = ((heapUsed * 100) / heapTotal).toFixed(2);
return `${bytesToMega(heapUsed)}MB / ${bytesToMega(heapTotal)}MB (${percentUsed}%)`;
};
export default {
logToStream(stream: NodeJS.WritableStream, interval: number = 1000) {
const id = setInterval(() => {
stream.write(getMemoryUse() + os.EOL);
}, interval);
const closeInterval = () => {
clearInterval(id);
process.exit(0);
};
process.on('SIGINT', closeInterval);
process.on('SIGKILL', closeInterval);
},
getMemoryUse,
};
xxxxxxxxxx
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);