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.

Describe the bug

I have a huge json file which I read using streams. I read this json file using JSONStream module. I do it like so

image

When JSONStream emits a json object axios posts this object to a webservice. There is no infinite loop anywhere, but axios is not releasing memory after finishing the request. In the image below you can see that after few minutes running, my node app reaches 12gb of memory! What is the problem?

image

To Reproduce

You can use the following code snippet to verify this issue

const fs = require("fs");
const path = require("path");
const JSONStream = require('JSONStream');
const axios = require('axios');
const axiosRetry = require('axios-retry');
const axiosRateLimit = require('axios-rate-limit');
const es = require('event-stream');
const winston = require('winston');

const logger = winston.createLogger({
	format: winston.format.combine(
		winston.format.timestamp({
            format: 'YYYY-MM-DD HH:mm:ss',
        }),
		winston.format.printf((info) =>
            JSON.stringify({
                timestamp: info.timestamp,
                message: info.message
            })
        )
    ),
    transports: [
		new winston.transports.File({filename: path.resolve(__dirname, 'errors.log'), level: 'error'}),
		new winston.transports.File({filename: path.resolve(__dirname, 'infos.log'), level:'info'})
    ]
});


axiosRetry(axios, { retries: 3, retryDelay: axiosRetry.exponentialDelay });
const http = axiosRateLimit(axios, { maxRequests: 20, perMilliseconds: 200 })

const postEvent = (uri, event, apiKey) => {
    return http.post(uri, event, {
        headers: {
            ...(apiKey && { 'api-key': apiKey }),
            'Content-Type': 'application/json'
        },
		maxRedirects: 0
    });
};

async function main(){

	const getStream = () => {
		const stream = fs.createReadStream(path.resolve('.', 'events.json'), { encoding: "utf8" });
		const parser = JSONStream.parse("*");
		return stream.pipe(parser);
	};

	getStream()
	.pipe(es.mapSync(async function (data){
		logger.info(data._id.$oid);
		try{
			await postEvent(data);
		}catch(e){
			logger.error(e)
		}
	}))
}

main();

Expected behavior

Memory should be released after request promise has been fullfiled.

Environment

  • Axios Version 0.27.2
  • Node.js Version 16
  • OS: Ubuntu 20
  • JSONStream: 1.3.5

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:10

github_iconTop GitHub Comments

1reaction
einhauscommented, Oct 3, 2022

FYI I had this same issue, and the only solution was to remove axios-retry. It seems it was hanging on to the response for every request regardless of whether the request failed or not.

Read more comments on GitHub >

github_iconTop Results From Across the Web

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 >
What is Memory Leak? How can we avoid? - GeeksforGeeks
Memory leak occurs when programmers create a memory in heap and forget to delete it. The consequences of memory leak is that it...
Read more >
Definition of memory leak - PCMag
When memory is allocated, but not deallocated, a memory leak occurs (the memory has leaked out of the computer). If too many memory...
Read more >
Memory Leaks and Garbage Collection | Computerworld
DEFINITION A memory leak is the gradual deterioration of system performance that occurs over time as the result of the fragmentation of a...
Read more >
What Is a Memory Leak and How Do They Happen?
A memory leak is a portion of an application that uses memory from RAM without finally freeing it. The result is that an...
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