Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 垃圾收集如何处理mobx utils中的异步操作?_Javascript_Garbage Collection_Mobx_Mobx React_Mobx Utils - Fatal编程技术网

Javascript 垃圾收集如何处理mobx utils中的异步操作?

Javascript 垃圾收集如何处理mobx utils中的异步操作?,javascript,garbage-collection,mobx,mobx-react,mobx-utils,Javascript,Garbage Collection,Mobx,Mobx React,Mobx Utils,我想要一个清晰的方法来创建存储,当我知道新的服务数据可用时可以刷新,比如mobx utils中的lazyObservable,但是可以轻松地附加更多的计算值和操作函数 从CreateReact应用程序开始,使用index.js 任务管理器显示每次在控制台中运行window.store.refresh时内存使用量的增加。有趣的是,使用setIntervalwindow.store.refresh,3000实际上会导致内存使用的振荡,而不是线性上升。在这两种相互冲突的情况下,我对垃圾收集器如何看待此

我想要一个清晰的方法来创建存储,当我知道新的服务数据可用时可以刷新,比如mobx utils中的lazyObservable,但是可以轻松地附加更多的计算值和操作函数

从CreateReact应用程序开始,使用index.js

任务管理器显示每次在控制台中运行window.store.refresh时内存使用量的增加。有趣的是,使用setIntervalwindow.store.refresh,3000实际上会导致内存使用的振荡,而不是线性上升。在这两种相互冲突的情况下,我对垃圾收集器如何看待此设置感到困惑


我能做些什么来确保你的垃圾最终会被收集?我所关心的是保留从生成器返回的内容,而不是在此期间分配的内容。

仅通过查看mem图很难确定内存泄漏。Afaik V8不会总是释放所有内存,除非存在真正的内存不足。毕竟,过度收集会节省内存,但会消耗太多的CPU周期。您不能假设空闲VM会自动释放所有内存。因此,我通常使用一个节点脚本来测试它,该脚本会长时间运行并重复一个进程,并使用有限的内存来查看是否有东西真的泄漏

在你的例子中,据我所知,没有理由会有内存泄漏。在某种程度上,承诺是gc-ed的,这使得gc与之配套的闭包成为可能。chrome控制台将通过$\ux保留对您承诺的引用,这也可能解释内存增加的原因

要正确查看是否存在内存泄漏,请使用chrome/firefox中的探查器,并确保您强制使用GC chrome devtools中的按钮

import React from 'react';
import ReactDOM from 'react-dom';
import {observable} from 'mobx';
import {observer} from 'mobx-react';
import {asyncAction} from 'mobx-utils';

const flipACoin = () => Math.random() > 0.5;

function determineGodliness() {
  const screwYourRAM = new Array(1000000).fill(Math.random());

  return new Promise((resolve) => {
    setTimeout(() => {
      resolve([flipACoin(), flipACoin()]);
    }, 1500);
  });
}

function godStore() {
  const store = observable({
    alpha: false,
    omega: false,
    waiting: false,
    refresh: asyncAction(function *() {
      this.waiting = true;
      [this.alpha, this.omega] = yield determineGodliness();
      this.waiting = false;
    }),
    get isGod() {
      return this.alpha && this.omega;
    }
  });

  store.refresh();

  return store;
}

window.store = godStore();

const App = observer(({store}) => <p>{
  (store.waiting)
    ? 'The suspense is killing me!'
    : (store.isGod)
      ? 'I am the Alpha and the Omega'
      : 'I just work here.'
}</p>);

ReactDOM.render(<App store={window.store} />, document.getElementById('root'));