question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

(Potential) Memory Leak In insert() Call

See original GitHub issue

Environment 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).

Screen Shot 2020-05-29 at 16 12 15

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:

Screen Shot 2020-05-29 at 16 10 13

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:closed
  • Created 3 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
mortenbocommented, Jun 24, 2020

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.

0reactions
mortenbocommented, Aug 14, 2020

No, all good here. It was a bug in core Node.js that leaked memory. Thanks for taking the time to review this!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found