New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
memory leak problem #398
Comments
Thanks for reporting this. I have a few follow up questions to help us get to the bottom of the leak:
|
Hi, @watson
|
Thanks for the details. Which version of Node.js are you running and do you mind sharing the entire |
@watson node10 and node 8.9 {
"dependencies": {
"@types/lodash": "^4.14.106",
"aws-sdk": "^2.130.0",
"bluebird": "^3.5.1",
"cache-manager": "^2.6.0",
"cache-manager-store-redis": "^0.2.0",
"config": "^1.26.2",
"debug": "^2.6.9",
"deepcopy": "^0.6.3",
"elastic-apm-node": "^1.6.0",
"kafka-node": "^2.2.3",
"kcors": "^2.2.1",
"koa": "^2.3.0",
"koa-bodyparser": "^3.2.0",
"koa-json-body": "^5.3.0",
"koa-morgan": "^1.0.1",
"koa-router": "^7.2.1",
"koa2-newrelic": "^2.2.0",
"lodash": "^4.17.4",
"mongoose": "^4.12.1",
"newrelic": "^1.40.0",
"pow-mongodb-fixtures": "^0.14.0",
"raven": "^2.2.1",
"redis": "^2.8.0",
"request": "^2.83.0",
"request-promise": "^4.2.2",
"sa-sdk-node": "^1.1.1"
},
"devDependencies": {
"ava": "^0.16.0",
"eslint": "^3.4.0",
"eslint-config-airbnb-base": "^10.0.1",
"eslint-plugin-import": "^2.7.0",
"nock": "^8.0.0",
"nodemon": "^1.12.1",
"rewire": "^2.5.2",
"sinon": "^1.17.5",
"sinon-as-promised": "^4.0.2",
"supertest": "^2.0.0"
},
} |
Hello, We are tracking a memory leak in our code too for a while. After some investigations, it seems that it could come from this agent. |
@joway I can see you're running New Relic at the same time as Elastic APM. We have seen situations where running multiple APM agents in the same app will generate memory leaks, and it's as such not something advice doing. Would it be possible for you to disable New Relic for a short time to see if that removes the memory leak? |
@watson I have remove newrelic in my code , but not uninstall it in package.json . |
@watson I had remove all newrelic things both in code and package.json , and the problem is still existed . |
By the way , does the apm-agent-nodejs will write local files ? We use kubernetes , and when it does not mount volume it will write file in memory . |
Ok, thank you so much for helping us debug this problem. It's super hard to reproduce these issues, so it helps a lot. Approximately how many requests per minute do your application get where you see this memory leak?
No the agent never writes anything to disk. It does use an in-memory queue, but that will be flushed and sent to the APM Server when it reaches a 100 transactions (configurable using |
I've so far been unable to reproduce this leak with mongoose and bluebird under either express or koa. I also did some testing around request-promise and got nothing from that either. I suspect it's likely related to context loss, but it's impossible to verify without a reproducible test case. The most likely candidates to investigate next I think would be aws-sdk, kafka-node and cache-manager. Can you share a bit about how you are using those? I'm especially interested in aws-sdk, since it contains quite a bit. |
In my case, I am using a custom Transaction to handle some udp traffic. @joway On which kind of Transaction / Span are you experiencing leak ? |
@Thetortuga Thanks for the details. It might be two different memory leaks that you're experiencing. When you say that the memory leak disappears when you disable @joway I know it might sound silly, but I just want to make sure that the memory leak you're experiencing is actually related to Elastic APM. Does it disappear if you disable the agent by setting |
@watson yes , I'm sure it caused by the apm agent . And I'm try to create a demo code to reproduce it . https://github.com/joway/elastic-apm-demo Now , the first thing I found it's that when I enable But in my production application, I just use ES5 javascript and no I doubt that if I have a package which use the |
@watson Yes, this is what I begin to suspect. Do you want that I open an new issued about it ? |
Hi, We are experiencing the same problem. We have a NodeJS/Express REST API (node 8.11.3) hosted in Heroku. We use the APM agent in version 1.6.0, with default parameters. Thanks. |
@mcolmant Thanks for letting us know. To narrow down what's going on it's best to try these things first:
Let me know if and at which point the memory leak disappears. |
Finally, I was wrong and the |
Hey ! I'm one of the colleagues x) With 1.8.2 :
We're trying to reproduce with a small environnement, we'll let you know |
Thanks @PestoP. Yes it would be super useful for us to have a small app where the problem can be reproduced 👍 |
Hi, we are having the exact same issue in our app. We've run into it in our Production ENV as well as during load testing (with devtools Heap Timelines showing the memory increase). After some testing we've found that:
But We're running Node 8.9.* with |
@fejalish thanks for reporting this. Is it possible for you to either share an example app where the leak occurs or take some heap snapshots and compare them in the Chrome Developer tools to possibly get an idea of which objects that are leaking? |
@watson I was going through the heap snapshots to see if I could figure out what was leaking, and saw lots of Would you like me to send you the snapshots somehow at all? I have snapshots with and without the leaks. |
@fejalish I would not recommend sharing these heap snapshots if they are from a production environment as they might contain personally identifiable information - so an example app would be better. But if you're sure they do not, can you upload them to Dropbox or something similar and share them with me by sending a link to watson at elastic.co? |
Little Offtopic: I had a similar experience with a "memory leak", but for me it was that I have long running transactions, which collect a lot of Spans. So far it looks like setting transactionMaxSpans to e.g. 100 (anything is better than unlimited ;)) solved this for me. |
I also ran into this issue in production and setting Here is how it affacted the memory usage: The first drop is an emegency restart due to the memory leak - the second drop is when deployed with I suspect the issue is in apm-agent-nodejs or one of it's dependencies. Just to give you the full context: The follwoing code allows any library to overwrite prepareStackTrace any way they like while at the same time ensuring that the error-callsites code which is used by apm-agent-nodejs is always executed. type prepTrace = (error, structuredStackTrace) => void
type Wrapper = prepTrace & { impl?: prepTrace }
let prepareStackTraceImpl = undefined
Object.defineProperty(Error, 'prepareStackTrace', {
set: (v) => {
if (v.impl) {
prepareStackTraceImpl = v.impl
return
}
prepareStackTraceImpl = v
},
get() {
if (!prepareStackTraceImpl) {
return
}
const wrapper: Wrapper = (err, callsites) => {
// the following call is copied from error-callsites package
Object.defineProperty(err, '__error_callsites', {
enumerable: false,
configurable: true,
writable: false,
value: callsites
})
return wrapper.impl.call(Error, err, callsites)
}
wrapper.impl = prepareStackTraceImpl
return wrapper
}
})
const apm = require('elastic-apm-node').start() |
Is there any fix for this issue yet? |
Unfortunately, no. We haven't yet been able to reproduce the issue ourselves, and no reproduction case has been given. If you are encountering memory issues like this it would be a huge help to us if you could please share your package.json dependencies, agent settings, Node.js version and, if possible, code for a simplified app that reproduces this issue. |
Hello, Like joway, we use these packages : Also using these: bluebird override Promise: node version 8.12.0. Agent is loaded with default configuration. After less than 24h of uptime: heap was 328mb After analysing some heap dump of the process, From what I understand, some PROMISE hooks type are init in the async-hook.js but are not destroyed, this might be link to promise cache used in dataloader package (storing a Promise in a map or a variable) or / and in local Promise cache, that store resolved Promise for reused OR PromiseWrap. A way to release these PROMISE might be to use the promiseResolve callback of async hooks (tried it on a local solution and seems to do the job but can't really tell as it's not in production) ? and this might move the leak somewhere else. Note that, for now I can't reproduce on a test project, so this might also be linked in wrong use of apm client in our production software. Hope this can help. |
The problem with using I'm working on trying to reproduce the issue more consistently in a reduced test case. If we can identify more specifically what interaction leads to this leak so we can hopefully figure out a way to work around it. It would be nice if we could figure out a way to map async contexts to the current transaction through a weakmap, but async_hooks is based off numeric context ids and weakmaps don't support numeric keys. 🤔 For now, you can try disabling async_hooks by using |
is any progress about this issue? |
Thanks for being patient with us on this. It's been a complicated process to debug and narrow down the culprit. But we now think that we have a fix. We just released version 1.14.3 of the agent which fixes the memory leak that we identified. Please try it out and let us know if it also fixed it for you 😃 |
@watson Test in my production env, it seems be fixed :) |
Great to hear! I'll close this issue for now. If it turns out to persist, please leave a comment here and we'll reopen the issue. |
Hi. It appears to be ok for us too ! Thanks ! |
@watson I'm using 2.0.6 version, I'm still facing same issue. I'm using all default configurations. |
@ambika93 I believe that what caused this particular issue have been fixed. You might be experiencing another similar issue though. Could I get you to open a new GitHub issue instead so that we don't "spam" the people in this thread while we try to work out what's going on? |
We have add the apm agent with express , our codes is:
But we find the memory usage is increasing until hit the memory limit .
We also put the same code in Koa applications , but the problem is still existed .
We find the issues #243 But @Qard said that it has been fixed ?
We also try to change our config like :
But it's not work .
Our dependencies is :
and the applications run in kubernets .
The text was updated successfully, but these errors were encountered: