I have been finding memory leak in our production code for 2 weaks. At the end I reproduced the memory leak with simple nodejs server and gcloud logging api.
Nodejs server is the same as inside Nodejs Getting Started Guide
'use strict';
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
module.exports = server;
I write every request headers to gcloud logging:
'use strict';
const app = require('./app.js');
const gcloud = require('gcloud')({
keyFilename: 'my-key-file',
projectId: 'my-project-id'
});
const logging = gcloud.logging();
const logs = logging.log('log-bucket');
const resource = {
type: 'logging_log',
labels: {
name: 'clear_node_logs',
}
};
app.on('request', function onServerRequest(req, res) {
const entry = logs.entry(resource, req.headers);
logs.info(entry, logHandler);
});
function logHandler(err) {
if (err) {
console.log(err);
}
}
If I make 300k requests to this server I get the following memory usage
As you can see the memory leaks. At the end I get OutOfMemory exception.
If I log the same amount of data inside setInterval
function the memory doesn't leak.
What could be the source of that leak in such a simple application?
Our dependency, gRPC, had a memory leak which was originally reported here: https://github.com/GoogleCloudPlatform/google-cloud-node/issues/1414
A fix was recently released in gRPC, and we will be releasing versions of google-cloud and affected submodules today.