简体   繁体   English

如何在我的 Apollo GraphQL 服务器上调试 memory 泄漏?

[英]How can I debug a memory leak on my Apollo GraphQL server?

I have a graphql server with multiple endpoints.我有一个具有多个端点的 graphql 服务器。 It is basically just a CRUD app, so I'm honestly not sure why there's a memory leak.它基本上只是一个 CRUD 应用程序,所以老实说,我不确定为什么会有 memory 泄漏。 The only potentially leaky endpoint I have is one that uploads pics to S3.我拥有的唯一可能泄漏的端点是将图片上传到 S3 的端点。

I've been looking around and have tried taking heap snapshots and comparing them but I'm not even sure which endpoint is the culprit.我一直在环顾四周,并尝试拍摄堆快照并进行比较,但我什至不确定哪个端点是罪魁祸首。 This is the flow I've been following:这是我一直关注的流程:

  1. Start the server with the --inspect flag: nodemon --inspect --exec babel-node src/index.js使用--inspect标志启动服务器: nodemon --inspect --exec babel-node src/index.js
  2. Take a heap snapshot before I do anything在我做任何事情之前拍摄堆快照
  3. Start my front end app and hit the endpoint I think has the memory leak (the one where I upload a photo)启动我的前端应用程序并点击我认为有 memory 泄漏的端点(我上传照片的那个)
  4. Take a heap snapshot again and compare the two再次拍摄堆快照并比较两者

Is this the correct flow for finding a memory leak?这是查找 memory 泄漏的正确流程吗? Is there a better way of doing this without having to guess which endpoint it is coming from?有没有更好的方法来做到这一点,而不必猜测它来自哪个端点? Are there perhaps tools I can use online that can help me find the source of the memory leak in production without having to guess like this?是否有我可以在线使用的工具可以帮助我找到生产中 memory 泄漏的来源,而不必像这样猜测? Perhaps something like Datadog or something?也许像 Datadog 之类的东西?

Update: From Heroku's metrics, it looks like the memory usage increases every time a request is made?更新:从 Heroku 的指标来看,每次发出请求时,memory 的使用率似乎都会增加?

在此处输入图像描述

But my src/index.js file doesn't do anything special:但是我的 src/index.js 文件没有做任何特别的事情:

import { ApolloServer, gql } from "apollo-server";
import { connectDb, models } from "./models";

import schema from "./schema";
import resolvers from "./resolvers";
import contexts from "./contexts";

const server = new ApolloServer({
  typeDefs: schema,
  resolvers,
  context: async ({ req, connection }) => {
    console.log(req.body.query);
    console.log(req.body.variables);

    const { getCurrentUser } = contexts;

    const currentUser = await getCurrentUser(req);
    return { models, currentUser };
  },
});

connectDb().then(async () => {
  server.listen({ port: process.env.PORT || 4000 }).then(({ url }) => {
    console.log(`🚀  Server ready at ${url}`);
  });
});

You are on the right path.你走在正确的道路上。 The guide I'm about to link to begins by following a similar approach to the one you've taken.我将要链接到的指南首先遵循与您所采用的方法类似的方法。 I'll link to the section that talks about monitoring memory in real time, which is available when you Record allocation timeline in chrome://inspect我将链接到讨论实时监控 memory 的部分,当您在 chrome://inspect 中Record allocation timeline时可用

https://marmelab.com/blog/2018/04/03/how-to-track-and-fix-memory-leak-with-nodejs.html#watching-memory-allocation-in-real-time https://marmelab.com/blog/2018/04/03/how-to-track-and-fix-memory-leak-with-nodejs.html#watching-memory-allocation-in-real-time

You could try to use clinic in order to debug and profile the app.您可以尝试使用诊所来调试和分析应用程序。 pretty good tool for nodeJS.相当不错的nodeJS工具。

You could user node-memwatch to detect where is memory leak.您可以使用node-memwatch来检测 memory 泄漏的位置。

It also might be a known issue, here is the link with a similar issue.这也可能是一个已知问题,这里是类似问题的链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我怎样才能让`apollo server` 与 graphql schemaObject 一起工作? - How can I make `apollo server` to work with graphql schemaObject? 如何在Heroku上调试环回应用程序的内存泄漏? - How can I debug a memory leak for a loopback application on Heroku? 如何将特殊字符放入 apollo graphQL 服务器的枚举值中? - How can I put special characters in an enum value in apollo graphQL server? 如何在Apollo GraphQL服务器之上放置Express REST层? - How can I put an Express REST layer on top of an Apollo GraphQL server? 如何仅使用 Apollo Server 2 graphql 端点的快速中间件 - How can I use express middleware with only Apollo Server 2 graphql endpoints 如何解码 node.js 中 memory 数据的含义并调试 memory 泄漏? - How can I decode the meaning of memory data in node.js & debug the memory leak? 我可以在没有 Apollo Server 的情况下使用 Apollo Client 吗? 我有一个普通的 GraphQl express 服务器并且想要与前端 ReactJS 集成 - I can use Apollo Client without Apollo Server? I have a plain GraphQl express server and want to integrate with front-end ReactJS 如何使用Apollo服务器对graphQl中的数据进行排序? - How to sort data in graphQl with apollo server? 如何在Apollo graphql服务器上运行Node.js - How to run nodejs with apollo graphql server 如何使用webpack调试节点apollo服务器 - how to debug node apollo server with webpack
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM