Use cases

Taking backups of the database.

Sending newsletters to your customers.

Possible Solutions

Crontab

Periodic task in Vanilla JavaScript

time_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

tellTime

async

.then

.catch

setInterval

node time_tell.js

2019 - 11 - 18 T18 : 40 : 27.286 Z 2019 - 11 - 18 T18 : 40 : 57.293 Z ...

Checking memory leak using Node’s inspect flag

--inpect

node --inspect time_tell.js

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

Checking memory leak using memwatch-next module

memwatch-next

npm install memwatch-next

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:

' , 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

Warning

memwatch-next

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:

' , info ); }); }); server . listen ( 3000 ); console . log ( " Server listening to port 3000. " );

node server_file.js

memcache-next

In a production system, we run a lot of scripts periodically either to sync data from some third-party source or spit data to some source. Although there are a lot of solutions to achieve such periodic behaviour, in this post we are going to discuss one such solution using vanilla JavaScript.Taking a backup of the database can be done once or twice a day depending upon the use case.If you get a lot of users you can take this backup multiple times a day.takes automatic backups so you don’t have to worry about it.Sending newsletters to customers can also be one of the use cases of running things periodically.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.Let’s write a simple function that can log the current time after every minute. Save this code in the fileFirst, we declare anfunctionwhich logs the time whenever it is called. It is important to declare this functionso that we can runorafter this function.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 this example, we have set the interval to one minute. Run the file using the command,Here is how the output is going to look like,Although this is a pretty straight forward example you can extend this to do anything that you want. 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.Node’sflag 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.This will open a new tab in the chrome with the attached debugger,Go to thetab 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.Since the memory used in the given program was consistent, I believe there was no memory leak in the program.Themodule is used to find the memory leaks in a program. Let’s install it using,Now change the program accordingly,Run the program simply, if you see the message memory leak being printed, you will know that there is some memory leak.The only purpose of this server is to save url data to the variable. This is a basic type of leaky function. Now we will see how memory leak function will raise ain. Just addto your existing code.Now I am using postman’s collection runner to run send 200 requests to the newly created server.Now run the server using the simple node command.Send request to the server on a random URL using the postman’s collection runner.will raise an error like this memory leak error.Hopefully, this will help you find memory leaks in your program in the future as well. Thanks for stopping by and do consider subscribing to the weekly newsletter.