吞咽引起的内存泄漏nodemon

时间:2015-06-16 10:34:20

标签: node.js gulp mean nodemon gulp-livereload

我有这个简单的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 个答案:

答案 0 :(得分:3)

看起来问题是这个

.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'
  });
});