heapsnapshot-near-heap-limit 为 docker 容器创建空堆转储

heapsnapshot-near-heap-limit creates empty heapdump for docker container

提问人:Aleks Makhomet 提问时间:8/18/2023 更新时间:8/18/2023 访问量:94

问:

我正在生产环境中的 Docker 容器内运行 node js 应用程序

我的应用存在内存泄漏,容器失败并显示 OOM。

我正在尝试使用--heapsnapshot-near-heap-limit节点选项自动创建堆转储。在生产环境中进行测试之前,请在本地计算机上进行测试。

我正在模拟内存泄漏。但是,当内存已满时,节点将被终止,但堆转储文件为空。为什么?

这是我的 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"]

这是我的应用.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}/`);
});

节点.js OOM

评论

0赞 Jason 10/11/2023
我在节点 18 上遇到了同样的问题。改用 --heap-prof 解决它

答: 暂无答案