【问题标题】:How to give warning if 80% of ram is used in node js如果在节点 js 中使用了 80% 的 ram,如何发出警告
【发布时间】:2021-02-12 07:07:06
【问题描述】:

我正在尝试编写一个程序,如果我使用了 80% 的内存,它会给我一个警告。但是当我将条件放在已用内存百分比上时,我没有得到任何响应。这是我的程序。

const os = require('os');


const CheckMemory = () =>{
    const total_memory = os.totalmem(); 
    const free_memory = os.freemem();
    const used_memory = total_memory - free_memory;
    const usedmemper = used_memory * 100 / total_memory; 
    return usedmemper.toFixed(2)+" %";
}


const Warning = (mem) =>{
    console.log(mem)
    if (mem <= 80 ) {
        console.log("You are currently using 80% of your available memory");
    }
} 


const Usedram = CheckMemory();
Warning(Usedram);

【问题讨论】:

    标签: javascript node.js linux typescript ecmascript-6


    【解决方案1】:

    错误在于您将字符串与整数进行比较,而 JavaScript 不会对此发出警告。

    假设内存是 99%。 CheckMemory 将在您的示例中返回字符串 99.00 %,然后您将与整数 80 进行比较:

    console.log("99.00 %" &lt;= 80);

    JavaScript 不会抱怨,而是返回 false。这就是为什么您永远不会看到打印的警告。

    要解决此问题,CheckMemory 应返回一个数字(介于 0 和 100 之间),而不是人类可读的字符串(如 80.23 %)。

    【讨论】:

      猜你喜欢
      • 2020-08-07
      • 1970-01-01
      • 1970-01-01
      • 2020-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-03
      相关资源
      最近更新 更多