我正在使用 Next.js 版本 8.0.4。
我面临的问题是,当我第一次构建应用程序并启动它时,它在服务器上占用了将近 150 Mb 内存,但随着时间的推移它会逐渐增长,并且它会在 3 或 4 天内变得非常大,具体取决于访问我的用户的数量网站。
我已经搜索了很多关于内存泄漏的信息,但我认为我的代码中没有做任何可能导致内存泄漏的事情。
我还注意到,每当我刷新页面或第一次加载它时,实际上当页面在服务器端呈现时,都会发生这种使用内存的增长。每次刷新时,我都会看到内存增加 1-2 Mb 并且不会被释放
但是当我在客户端并且我路由到客户端的其他页面时,一切都很好。
我不知道是什么原因造成的。如果有人能帮我解决这个问题,我将不胜感激。
https://i.stack.imgur.com/t4VDm.jpg
如果有帮助,这也是我的 next.config.js:
module.exports = withCss({
cssModules: true,
cssLoaderOptions: {
importLoaders: 1,
localIdentName: "[local]___[hash:base64:5]",
},
webpack(config, {dev, isServer}) {
if (ANALYZE) {
config.plugins.push(new BundleAnalyzerPlugin({
analyzerMode: 'server',
analyzerPort: isServer ? 8080 : 4000,
openAnalyzer: true
}))
}
config.plugins.push(new CompressionPlugin({
filename: "[path].gz[query]",
algorithm: "gzip",
test: /\.js$|\.css$|\.html$/,
threshold: 10240,
minRatio: 0.8,
cache: true,
compressionOptions: {level: 1}
}));
config.optimization.minimizer = [new UglifyJsPlugin({
test: /\.js(\?.*)?$/i,
cache: true,
parallel: true,
sourceMap: true,
extractComments: 'all',
uglifyOptions: {
warnings: false,
parse: {},
compress: {},
mangle: true,
output: null,
toplevel: false,
nameCache: null,
ie8: false,
keep_fnames: false,
}
})];
config.module.rules.push({
test: /\.(svg|eot|otf|ttf|woff|woff2)$/,
use: {
loader: 'url-loader',
options: {
limit: 100000,
publicPath: './assets/fonts',
outputPath: 'static/fonts',
name: '[name].[ext]'
}
}
});
if (Array.isArray(config.optimization.minimizer)) {
config.optimization.minimizer.push(new OptimizeCSSAssetsPlugin({}));
}
if (!isServer && !dev) {
config.optimization.splitChunks.cacheGroups.commons.minChunks = 2
}
return config
},
});