gulp & nodemon 导致的内存泄漏

Posted

技术标签:

【中文标题】gulp & nodemon 导致的内存泄漏【英文标题】:Memory leak caused by gulp & nodemon 【发布时间】:2015-09-01 03:03:42 【问题描述】:

我有一个简单的 gulpfile,它基本上编译了一些较少的文件并触发 livereload。我也有 nodemon 在代码更改时重新启动服务器。

var gulp = require('gulp'),
    less = require('gulp-less'),
    autoprefixer = require('gulp-autoprefixer'),
    minifycss = require('gulp-minify-css'),
    jshint = require('gulp-jshint'),
    uglify = require('gulp-uglify'),
    // imagemin = require('gulp-imagemin'),
    rename = require('gulp-rename'),
    concat = require('gulp-concat'),
    notify = require('gulp-notify'),
    cache = require('gulp-cache'),
    livereload = require('gulp-livereload'),
    nodemon = require('gulp-nodemon')
    // del = require('del'),
    path = require('path');

gulp.task('styles', function() 
  return gulp.src('public/css/less/app.less')
    .pipe(less())
    .pipe(autoprefixer('last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'))
    .pipe(gulp.dest('public/css/'))
    .pipe(rename(suffix: '.min'))
    .pipe(minifycss())
    .pipe(gulp.dest('public/css/'))
    .pipe(notify( message: 'Styles task complete' ))
    .pipe(livereload());
);

// Watch
gulp.task('watch', function() 

  livereload.listen();

  gulp.watch('public/css/less/*.less', ['styles']);
  gulp.watch(['public/app/**']).on('change', livereload.changed);

);


gulp.task('demon', function () 
  nodemon(
    ignore: [
      './public/',
      './node_modules/',
      './tests/'
    ]
  )
    .on('start', ['watch'])
    .on('change', ['watch'])
    .on('restart', function () 
      console.log('restarted!');
    );
);

// Default Task
gulp.task('default', ['demon']);

问题是一切正常运行了 5 次,然后我收到警告:

(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.
Trace
    at StatWatcher.addListener (events.js:160:15)
    at Object.fs.watchFile (fs.js:1175:8)
    at Gaze._pollFile (/Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:329:10)
    at /Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:411:12
    at Array.forEach (native)
    at /Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:409:11
    at iterate (/Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/helper.js:52:5)
    at Object.forEachSeries (/Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/helper.js:66:3)
    at Gaze._initWatched (/Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:354:10)
    at Gaze.add (/Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:177:8)
    at new Gaze (/Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:74:10)
    at gaze (/Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:86:10)
    at Object.module.exports [as watch] (/Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/index.js:12:17)
    at Gulp.watch (/Users/kris/Sites/SmartSaver/node_modules/gulp/index.js:35:16)
    at Gulp.gulp.task.nodemon.ignore (/Users/kris/Sites/SmartSaver/gulpfile.js:36:8)
    at module.exports (/Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/orchestrator/lib/runTask.js:34:7)

说完就崩溃了:

[gulp] [nodemon] starting `node ./bin/www`
[12:24:43] Starting 'watch'...
[12:24:43] 'watch' errored after 9.52 ms
[12:24:43] Error: EMFILE: Too many opened files.
    at Gaze._handleError (/Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:436:31)
    at Gaze._watchDir (/Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:302:17)
    at /Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:358:10
    at iterate (/Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/helper.js:52:5)
    at /Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/helper.js:61:11
    at /Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:420:5
    at iterate (/Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/helper.js:52:5)
    at /Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/helper.js:61:11
    at /Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:420:5
    at iterate (/Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/helper.js:52:5)
    at /Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/helper.js:61:11
    at /Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:420:5
    at iterate (/Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/helper.js:52:5)
    at /Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/helper.js:61:11
    at /Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:420:5
    at iterate (/Users/kris/Sites/SmartSaver/node_modules/gulp/node_modules/vinyl-fs/node_modules/glob-watcher/node_modules/gaze/lib/helper.js:52:5)

我搜索了警告,Node 中似乎存在错误,但我认为我在这里做错了,例如可能初始化了太多侦听器或未正确关闭它们。

感谢任何反馈。

谢谢!

【问题讨论】:

【参考方案1】:

看起来问题是这样的

.on('start', ['watch'])
.on('change', ['watch'])
.on('restart', function () 
  console.log('restarted!');
);

每次文件更改时,您都会触发watch,它会附加额外的侦听器。重新启动几次后,您将超过最大侦听器数。

这是一个标准的 nodemon 设置,是你真正需要的:

gulp.task('dev', function () 
  nodemon(
    script: 'your-start-script.js',
    ext: 'js'
  );
);

【讨论】:

非常感谢 Yuri :) 你是对的,删除所有这些作品就好了。 gulp.task('demon', function () nodemon( ignore: [ './public/', './node_modules/', './tests/' ] ) ); // Default Task gulp.task('default', ['demon', 'watch']); 我修改了上面的 sn-p 以包括那些可能有相同问题的监视任务。再次感谢尤里。

以上是关于gulp & nodemon 导致的内存泄漏的主要内容,如果未能解决你的问题,请参考以下文章

Gulp Watch 和 Nodemon 冲突

将 browsersync 与 gulp/nodemon 一起使用

Gulp-nodemon 和监视任务

Nodemon 与 gulp watch 绑定时崩溃并重新启动两次以上

如何在调试模式下运行 gulp-nodemon?

找不到模块“gulp-nodemon”。在 Windows 7 上,32 位。使用电源外壳