(Potential) Memory Leak In insert() Call
See original GitHub issueEnvironment details
- OS: Linux Alpine, macOS
- Node.js version: v14.3.0
- npm version: 6.14.5
@google-cloud/bigquery
version: ^4.7.0
Steps to Reproduce
const {BigQuery} = require('@google-cloud/bigquery');
(async () => {
const client = new BigQuery({
keyFilename: './google-creds.json',
projectId: 'myproductid'
});
const run = async () => {
await client
.dataset('events')
.table('analytics')
.insert([{
insertId: '1',
json: {
test: 1
}
}, {
insertId: '2',
json: {
test: 2
}
}], {raw: true});
};
setInterval(async () => {
await run();
}, 5000);
})();
Here’s a production screenshot that shows external memory (top), total heap (middle) and used heap (bottom).

As you can see external memory slowly goes up. If I remove my BigQuery insert statements in my endpoint external memory releases and doesn’t gradually increase.
Here’s an isolated external memory chart for one of the pods:

I’ve found out it’s (maybe) related to teeny-request being called in node_modules/@google-cloud/common/build/src/util.js
. Specifically this function:
/**
* Make a request through the `retryRequest` module with built-in error
* handling and exponential back off.
*
* @param {object} reqOpts - Request options in the format `request` expects.
* @param {object=} config - Configuration object.
* @param {boolean=} config.autoRetry - Automatically retry requests if the
* response is related to rate limits or certain intermittent server
* errors. We will exponentially backoff subsequent requests by default.
* (default: true)
* @param {number=} config.maxRetries - Maximum number of automatic retries
* attempted before returning the error. (default: 3)
* @param {object=} config.request - HTTP module for request calls.
* @param {function} callback - The callback function.
*/
makeRequest(reqOpts, config, callback) {
const options = {
request: teeny_request_1.teenyRequest.defaults(requestDefaults),
retries: config.autoRetry !== false ? config.maxRetries || 3 : 0,
shouldRetryFn(httpRespMessage) {
const err = util.parseHttpRespMessage(httpRespMessage).err;
return err && util.shouldRetryRequest(err);
},
};
if (typeof reqOpts.maxRetries === 'number') {
options.retries = reqOpts.maxRetries;
}
if (!config.stream) {
return retryRequest(reqOpts, options, (err, response, body) => {
util.handleResp(err, response, body, callback);
});
}
If I return before return retryRequest
external memory doesn’t go up. Maybe I’m wrong but this is as far as I got before concluding I need some help from the authors.
I hope you can help! Thanks!
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Memory leak in insert in sorted linked list - Stack Overflow
However, when I analyze the program with valgrind, it is showing that I am leaking memory in function sorted_insert , and to be...
Read more >Memory Leak with Inserting at the End of a Singly Linked List
Hello, I'm creating a very basic singly linked list as a refresher on manual memory management. Here is a gist that contains the...
Read more >Understanding Memory Leaks in Java - Baeldung
A Memory Leak is a situation where there are objects present in the heap that are no longer used, but the garbage collector...
Read more >Potential Memory Leak and Other Errors in EDQ — oracle-tech
We have recently migrated from a development environment with EDQ that was using PostgreSQL to a testing environment using Oracle DB.
Read more >Memory leak - Wikipedia
In computer science, a memory leak is a type of resource leak that occurs when a computer program incorrectly manages memory allocations in...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Update: It seems the memory leak is specific to certain versions of Node.js: https://github.com/nodejs/node/issues/33468
The application/json bug is still present in your code.
No, all good here. It was a bug in core Node.js that leaked memory. Thanks for taking the time to review this!