I'm running the node js app inside of Docker container on production
My app has a memory leak and the container fails with OOM.
I'm trying to automatically create a heapdump using --heapsnapshot-near-heap-limit node option. Testing this on my local machine before I can test on production.
I'm emulating a memory leak. However, when memory is full, the node is killed but the heapdump file is empty. Why?
Here is my Dockerfile
# Use the official Node.js image as the base image
FROM node:18-buster-slim
# Set the working directory inside the container
WORKDIR /app
RUN apt-get update && apt-get install -y procps
# Copy the rest of the application files to the container
COPY . .
# Expose the port on which the Node.js app will run (you should use the same port in your Node.js app code)
EXPOSE 3000
# Command to run the Node.js app when the container starts
CMD ["node", "--max-old-space-size=50", "--heapsnapshot-near-heap-limit=3", "app.js"]
Here is my app.js
// app.js
const http = require('http');
const hostname = '0.0.0.0';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
const payload = [];
function consumeMemory() {
const data = 'x'.repeat(1000000); // Generate a large string
payload.push(data); // Add the string to the payload array
setTimeout(consumeMemory, 10); // Delay the next iteration by 100 milliseconds
}
consumeMemory(); // Start consuming memory
res.end('Hello, World!\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});