Call a function periodically in Javascript (with memory leak analysis)
November 17, 2019 ·
8 mins read
Use cases
- Taking backups of the database.
1. Use cases
2. Possible Solutions
3. Periodic task in Vanilla JavaScript
4. Update regarding Memory leak
5. Checking memory leak using Node’s inspect flag
6. Checking memory leak using memwatch-next module
7. How does a leaky function look like?
If you get a lot of users you can take this backup multiple times a day.
AWS takes automatic backups so you don’t have to worry about it.
2. Possible Solutions
3. Periodic task in Vanilla JavaScript
4. Update regarding Memory leak
5. Checking memory leak using Node’s inspect flag
6. Checking memory leak using memwatch-next module
7. How does a leaky function look like?
- Sending newsletters to your customers.
Possible Solutions
Crontab
Most of the organizations tend to use crontab for handling any such requests for running periodic tasks. Each and every language has its own implementation of handling periodic tasks.Periodic task in Vanilla JavaScript
Let’s write a simple function that can log the current time after every minute. Save this code in the filetime_tell.js
.
const tellTime = async function () {
console.log(new Date());
}
const minutes = 0.5;
const interval = minutes * 60 * 1000;
setInterval(function() {
// catch all the errors.
tellTime().catch(console.log);
}, interval);
async
function tellTime
which logs the time whenever it is called.
It is important to declare this function async
so that we can run .then
or .catch
after this function.
setInterval
is the inbuilt function that can run any statements after every given interval.
It takes two arguments, first is the definition of the function being run itself and next is the interval after which we want to run the function again( In milliseconds).
In this example, we have set the interval to one minute.
Run the file using the command,
node time_tell.js
Here is how the output is going to look like,
2019-11-18T18:40:27.286Z
2019-11-18T18:40:57.293Z
...
Update regarding Memory leak
When I posted the same on dev.to, some of the programmers reached out in the comment section asking about the potential memory leak in the program. While building the program, I was not thinking about the memory leak because I was going to run it for a few hours only for a particular use case. I searched online to find a solution for finding ways to discover any memory leaks in the program. This nearform article was very helpful in finding more about it. The article suggested three ways to find out the memory leaks. I have only tried two of them. Let me know if you have any other way of finding it out as well.Checking memory leak using Node’s inspect flag
Node’s--inpect
flag attaches a debugger with the program and allows you to run all types of analysis on the program running in the chrome.
Try running the following command.
node --inspect time_tell.js
This will open a new tab in the chrome with the attached debugger,
Debugger listening on ws://127.0.0.1:9229/2ee7271d-04e2-4711-9df5-99777bdb7ca4
For help see https://nodejs.org/en/docs/inspector
Debugger attached.
memory
tab of the devtool in the chrome and click on the take snapshot button. You will be able to see the memory being used when you take the snapshot.
Take another snapshot after 10 seconds of the initial snapshot. If the memory size is increasing you will know that there is a function in your code which is leaking memory.
Checking memory leak using memwatch-next module
Thememwatch-next
module is used to find the memory leaks in a program.
Let’s install it using,
npm install memwatch-next
Now change the program accordingly,
const memwatch = require('memwatch-next');
const tellTime = async function () {
console.log(new Date());
}
const minutes = 0.01;
const interval = minutes * 60 * 1000;
setInterval(function() {
// catch all the errors.
tellTime().catch(console.log);
}, interval);
memwatch.on('leak', (info) => {
console.error('Memory leak detected:\n', info);
});
How does a leaky function look like?
const http = require("http");
const requestUrls = [];
const server = http.createServer((req, res) => {
requestUrls.push({ url: req.url });
res.end(JSON.stringify(requestUrls));
});
server.listen(3000);
console.log("Server listening to port 3000.");
requestUrls
.
This is a basic type of leaky function. Now we will see how memory leak function will raise a Warning
in memwatch-next.
Just add memwatch-next
to your existing code.
const http = require("http");
const memwatch = require('memwatch-next');
const requestUrls = [];
const server = http.createServer((req, res) => {
requestUrls.push({ url: req.url });
res.end(JSON.stringify(requestUrls));
memwatch.on('leak', (info) => {
console.error('Memory leak detected:\n', info);
});
});
server.listen(3000);
console.log("Server listening to port 3000.");
node server_file.js
memcache-next
will raise an error like this memory leak error.
Please share your Feedback:
Did you enjoy reading or think it can be improved? Don’t forget to leave your thoughts in the comments section below! If you liked this article, please share it with your friends, and read a few more!