Suggestions for Finding a Memory Leak in an Expres

2019-09-04 01:45发布

问题:

I have an app built in Express 3.20. I keep it up and running with PM2. Watching the memory usage in PM2, I see it only takes a couple of hours for the usage to become >500MB for each process, at which point it starts having problems.

About 90% of the requests received by my app are for thumbnail images. So I am guessing that the memory leak is coming from the portion of my code that serves thumbnail images to the user.

The way the code works, a user requests an image of a certain size. The script checks to see if an image of that size already exists in the cache/ folder. If it it does, it sends that file to the client. If it does not, it uses graphicsMagick to create a new image in the cache/ folder, from the original image, and then sends it.

I am including the relevant code below. Can anyone tell me if I am doing anything obviously wrong here, that is causing things to hang around in memory?

Thanks (in advance) for your help.

var fs = require( "graceful-fs" ),
    gm = require( "gm" );

function isOriginalNewer( orig, cache ) {
    return orig.ctime > cache.ctime ? true : false;
}

function checkIfCachedImageUpToDate( originalPath, cachePath, cb ) {
    fs.stat( originalPath, function( err, stat1 ) {
        if ( err ) return cb( err );
        fs.stat( cachePath, function( err, stat2 ) {
            if ( err ) return cb( err );
            if ( isOriginalNewer( stat1, stat2 ) ) {
                cb ( null, false );
            } else {
                cb( null, true );
            }
        });
    });
}

function createNewCachedImage( originalPath, cachePath, cacheSubDir, size, cb ) {
    fs.exists( "cache/" + cacheSubDir, function ( exists ) {
        if ( exists ) {
            gm( originalPath ).resize( size, null, "^" ).quality( 95 ).write( cachePath, cb );
        } else {
            fs.mkdir( "resources/cache/" + cacheSubDir, 0750, function() {
                gm( originalPath ).resize( size, null, "^" ).quality( 95 ).write( cachePath, cb );
            });
        }
    });
}

function checkIfCached( originalPath, cachePath, cb ) {
    fs.exists( cachePath, function( exists ){
        if ( exists ) {
            checkIfCachedImageUpToDate( originalPath, cachePath, function( err, isUpToDate ) {
                if ( err ) return cb( err );
                if ( isUpToDate ) cb( null, true );
                else cb( null, false );
            });
        } else cb( null, false );
    });
}

function getImagePath( filename, type, size, cb ) {
    var originalPath = "originals/" + filename,
        cacheFilename = hash.md5( originalPath ) + "." + size + ".jpg",
        cacheSubDir = cacheFilename.substr( 0,2 ),
        cachePath = "cache/" + cacheSubDir + "/" + cacheFilename;

    checkIfCached( originalPath, cachePath, function( err, isCached ) {
        if ( err ) return cb( err );
        if ( isCached ) {
            cb( null, cachePath );
        } else {
            fs.exists( originalPath, function( exists ) {
                if ( !exists ) return cb( "File Not Found " + originalPath );
                createNewCachedImage( originalPath, cachePath, cacheSubDir, size, function( err ) {
                    if ( err ) return cb( err );
                    cb( null, cachePath );
                });
            });
        }
    });
}

module.exports = function( app ){
    app.get( "/image/:filename/:size", isLoggedIn, function( req, res, next ){
        var p = sanitizeParams( req );
        getImagePath( p.filename, p.size, function( err, imagePath ){
            if ( err ) next( err );
            else res.sendfile( imagePath );
        });
    });
};