Problem description
Create a new SSR project with NUXT and generate PDF based on the HTML output from SSR.
Start the service with pM2.
The program calls the generated interface 200 times in a row, and the memory increases rapidly, all the way up to 1.4GB, and then restarts automatically.
Run heapdump to view the memory snapshot
1) Generate the. Snapshot file
Var headpdump = require(nuxt.config.js)'heapdump')
setTimeout(function () {
headpdump.writeSnapshot('/tmp/' + Date.now() + '.heapsnapshot'.function(err, filename) {
console.log('dump written to', filename)
})
}, 3 * 60000)
Copy the code
2).snapshot file generated by momery module load in Chrome debugger (will continue to grow to 1.4g)
The closure reference is abnormal:
router.js
There’s a rewrite of the push method,
// TODO: remove in Nuxt 3
const emptyFn = () => {}
const originalPush = Router.prototype.push
Router.prototype.push = function push (location, onComplete = emptyFn, onAbort) {
return originalPush.call(this, location, onComplete, onAbort)
}
Copy the code
After the native code is commented out, the problem is fixed and memory does not surge any more.Copy the code
But when deployed to a server, the problem persists.
Please forget the above analysis, I was misled too.
The real culprit
When pM2 starts the application, it automatically uses the server/index.js node startup file.
pm2 start server/index.js
Use the above command to start the service, unexpectedly also no error.
Solution: Modify the PM2 startup command:
Json: “publish”: “nuxt build && NPM start”
Pm2 start NPM –name “your project name” — run publish
To solve!
Pm2 start server/index.js (pm2 start server/index.js)