【问题标题】:Aurelia, Electron: Possible EventEmitter memory leak detectedAurelia,Electron:检测到可能的 EventEmitter 内存泄漏
【发布时间】:2020-08-05 09:35:44
【问题描述】:

我正在使用 electron 和 aurelia 构建一个系统资源监视器作为项目。

Main.js

  var ramInfo = {};
  var result = await si.mem()
  ramInfo.total = parseInt(result.total / 1024 / 1024);
  ramInfo.used = parseInt(result.used / 1024 / 1024);
  ramInfo.percentUsed = parseInt((ramInfo.used / ramInfo.total) * 100);
  ramInfo.percentAvailable = parseInt((ramInfo.percentUsed - 100) * -1);
  event.sender.send('ram-reply', ramInfo);
})

Overview.js:

  async attached () {
    await this.getRamInfo();
    this.startDataRefresh();

  }

  async getRamInfo () {
    window.ipc.send('ram');
     await window.ipc.on('ram-reply', (event, result) => {
        this.system.ram = result;
        //This line gets logged an additional time each time the setInterval function runs
        console.log(this.system.ram);
        this.ramData.series = [this.system.ram.percentAvailable, this.system.ram.percentUsed];
        new Chartist.Pie('.ram-chart', this.ramData , this.options);
      });
      console.log("Break");
    }

  startDataRefresh() {
    let scope = this;
    setInterval(function() {
      scope.getRamInfo();
    }, 3000);
  }

我在电子控制台中收到以下错误:

MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 ram-reply listeners added to [EventEmitter]. Use emitter.setMaxListeners() to increase limit

我只认为 getRamInfo() 函数会每三秒运行一次,但是,函数的 console.log 部分会在每次函数运行时额外记录一次。我很确定这就是问题所在,我只是不确定为什么它在每个间隔运行多次。

编辑: 在将 setInterval 函数移动到 main.js 中时,我已经达成了部分解决方案:

ipcMain.on('ram', async (event) => {
  setInterval(async function() {
    var ramInfo = {};
    var result = await si.mem()
    ramInfo.total = parseInt(result.total / 1024 / 1024);
    ramInfo.used = parseInt(result.used / 1024 / 1024);
    ramInfo.percentUsed = parseInt((ramInfo.used / ramInfo.total) * 100);
    ramInfo.percentAvailable = parseInt((ramInfo.percentUsed - 100) * -1);
    event.sender.send('ram-reply', ramInfo)
  }, 3000);
})

似乎每次调用 ipcMain 的原始 setInterval 都会创建一个新的侦听器,并且每次每个侦听器都返回结果。我希望它依赖于打开的视图,因此最好通过视图来控制它。

【问题讨论】:

    标签: electron aurelia


    【解决方案1】:

    试试这个:

    async getRamInfo () {
        window.ipc.send('ram');
        return new Promise(resolve => window.ipc.once('ram-reply', (event, result) => resolve(result));
    }
    
    async refresh() {
        const ramInfo = await this.getRamInfo();
        this.ramData.series = [this.system.ram.percentAvailable, this.system.ram.percentUsed];
        new Chartist.Pie('.ram-chart', this.ramData , this.options);
        // ...
    }
    
    startDataRefresh() {
        if(!this.interval) {
            this.interval = setInterval(() => this.refresh(), 3000);
        }
    }
    
    stopDataRefresh() {
        if(this.interval) {
            clearInterval(this.interval);
            delete this.interval;
        }
    }
    

    主要部分 - window.ipc.once('ram-reply' - 使用 once 进行一次性事件订阅

    【讨论】:

      猜你喜欢
      • 2018-08-13
      • 1970-01-01
      • 1970-01-01
      • 2022-10-14
      • 1970-01-01
      • 2020-02-27
      • 2021-09-05
      相关资源
      最近更新 更多