Potential memory leak.
- Node.js Version: v8.9.0
- OS: Win7, AWS
- Scope (install, code, runtime, meta, other?): other
- Module (and version) (if relevant):
I having been running nodejs in my server for quite a while but I just happened to notice a memory leak that started a week or two ago. I tried using the --inspect flag as well as the memwatch-next package but I havent found anything other than a memory leak. The heap snapshots did not return me (at least to my eyes) anything out of the ordinary. While it happens ion the server, I haven't been able to reproduce it on Windows. The process at first runs at 130mb and after a day or two is at 200 (though at times it drops ~30mb sometimes and re-climbs but I guess that's because of the GC.). I cache a few things but for certain not that much.
Aside from native Node.js I use MySQL, express and native HTTP(s) server. I initially decided to try to completely remove express to see if that changes anything but it didn't. I also checked the HTTP server and separated it to another process entirely which didn't seem to leak either.
That led me to believe, since the memory leak is something big, and not something strictly limited to a portion of the process, I am either using wrong async/await/promises/callbacks or I have a leaky module.
My callbacks (yes I still have some, I plan to change them to promises.) are pretty straight forward.
function some_fun(param)
{
another_func(param, function(res){
//...
});
}
function another_func(param, callback)
{
connection.query('..', function(error, row) {
if(error) throw error;
var data = row;
//do a few things with data
return callback(data);
});
}
When I use await I simply do it like:
async function another_funct(param)
{
var response = await some_promise(params).catch(err => handle_error(err));
if(!response) return;
}
I know with few to none source code, you wont be able to tell, but if you have any advice as to how to narrow it down I'd appreciate it.
now that you have simplified the program, one of the natural next level of isolation is to see if the growth is in proportion to the data you are fetching from the db. For this, you could issue multiple queries that retrieve large data items of known volume, produce heapdumps and perform simple math to see if the growth matches with the aggregation of transported data.
If the process doesn't terminate with an out-of-memory error, then there is probably nothing to worry about.
If you want to cap memory usage, pass --max_old_space_size=512 (for example) to node. The default is ~1500 and node will in general prefer to use that memory if the alternative is frequent gc'ing.
Apropos your await example, is there a reason you call .catch() instead of wrapping the statement in a try/catch? Seems like a more natural way to write it.
As has been noted above, what it sounds like you have observed is a memory usage increase, but because the heap memory is managed under garbage collection there is the potential for accumulating objects in memory that are no longer in use in your application and this memory can be freed in the next GC cycle. For there to be an actual memory leak, then there needs to be an accumulation of objects that remain 'in use' in the application and are ineligible for collection by the garbage collector. Thus, the classic profile of a memory leak requires there is a persistent upward trending sawtooth profile for the used heap in verbose GC data (or similar). The sawtooth shape is created by the generational GC. The issue is not diagnosable just plotting memory usage of allocated objects alone, but requires extrapolating and analysis of the memory of in-use objects after a few GC cycles.
After much search I finally found that it was thanks to an update made in a module. Though truth to be told I am kinda thankful due to the fact that I learned a lot in the process and I feel more experienced and ready to tackle similar situations. Thank you all for your feedback, while none of these turned out to be the leak, they helped me learn and understand better the GC and memory in general. I appreciate it!
issue resolved, closing.