如何使用 Node.js 检查 ram 的当前使用情况? (Discord.js 机器人)

How can i check the current usage of ram with Node.js? (Discord.js Bot)

我想创建一个名为 _ram 的命令,它应该显示机器人 ram 的当前使用情况。我已经尝试过这些东西:

${client.performance.memory} //(Says memory is not defined)
${window.performance.memory} //(Window is not defined)

有什么显示方法吗?

如果你想知道你的Node.js进程使用了​​多少内存,你可以查询:

process.memoryUsage().heapUsed / 1024 / 1024;

它将以字节为单位输出您的进程使用的内存。它不会显示 Node.js 使用的实际内存,因为在这种情况下您还需要考虑 Node.js 垃圾收集器。

您必须使用:process.memoryUsage()其中 returns 一个以字节为单位描述进程内存使用情况的对象。

{
  rss: 4935680,
  heapTotal: 1826816,
  heapUsed: 650472,
  external: 49879
}

heapTotal and heapUsed refer to V8's memory usage. external refers to the memory usage of C++ objects bound to JavaScript objects managed by V8. rss, Resident Set Size, is the amount of space occupied in the main memory device (that is a subset of the total allocated memory) for the process, which includes the heap, code segment and stack.

var os = require('os');

var usedMemory = os.totalmem() -os.freemem(), totalMemory = os.totalmem();

var  getpercentage = 
  ((usedMemory/totalMemory) * 100).toFixed(2) + '%'

console.log("Memory used in GB", (usedMemory/ Math.pow(1024, 3)).toFixed(2))
console.log("Used memory" , getpercentage);