Skip to content

Instantly share code, notes, and snippets.

@nantaphop
Created May 26, 2018 05:32
Show Gist options
  • Save nantaphop/f8a8bdf1540e2113aff47a186e8fff27 to your computer and use it in GitHub Desktop.
Save nantaphop/f8a8bdf1540e2113aff47a186e8fff27 to your computer and use it in GitHub Desktop.
จำลอง Memory Leak เพื่อทำลองทำ Heap Snapshot
const express = require('express')
// require เข้ามาเพื่อให้ Process เรารับ Sig USR2 เพื่อทำ Heap Memory Snapshot
const heamdump = require('heapdump')
const app = express()
const PORT = 3000
// Array ที่จะเก็บ Object เยอะๆ เป็นตัวทำ Memory Leak
let mem = []
app.get('/doLeak', (req, res) => {
// ทุกครั้งที่เรียกเข้ามา จะสร้าง Object 1 หมื่นตัวใส่ Array จำลองการเกิด Memory Leak
let date = new Date()
for(let i = 0; i < 10000; i++){
mem.push({
msg: `I'M CAUSE MEMORY LEAK!!: ${date}: ${mem.length}`
})
}
return res.send(`current object in mem variable: ${mem.length}`)
})
app.listen(PORT, () => {
console.log(`server listen at :${PORT} with PID: ${process.pid}`)
console.log(`Get Heap Snapshot by run 'kill -USR2 ${process.pid}'`)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment