JavaScript - Persistent memory retention issues

I've noticed persistent memory leaks in my TypeScript application (3PG), leading me to believe there's an issue with memory management.

Comparison of Applications:

Here is a sample code snippet from the class of 3PG that heavily uses intervals and might be causing the problem: https://pastebin.com/Z6K8a2vK

private schedule(uuid: string, savedGuild: GuildDocument, interval: number) {
        const task = this.findTask(uuid, savedGuild.id);
        if (!task.timer) return;
 
        task.status = 'ACTIVE';
        task.timeout = setInterval(
            async() => await this.sendTimer(task, savedGuild), interval);
}

I'm questioning whether this piece of code could be the root cause of the issue, and if so, what practices I should avoid in JavaScript that may lead to increased memory usage. Appreciate any insights.


Update: The problem was identified as discord.js triggering the ready event multiple times, gradually consuming more memory. Acknowledging my oversight in not providing sufficient information for a precise solution.

Answer №1

The issue may arise from the multiple setInterval calls setting up new executions. To address this, there are two steps you should take:

  1. Clear Intervals for any tasks that have already completed and are no longer necessary. Keep a reference to the interval objects and clear them when they are no longer needed.
  2. Depending on the specific requirements of your code, consider whether setTimeout might be a more suitable option for the task at hand.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to send and download files using Express.js and React on the client side

Currently, my backend is built with Express JS and the frontend with React JS. In certain routes, I need to send a file in response to requests. Here's how I'm currently handling it: return res.status(200).sendFile(path.resolve(`files/${product. ...

After an error occurs, the Node.js Restify code will be executed

Within a Restify route, I have set up a router handler that calls a custom module for error checking. If the code encounters an error condition, it returns next(err) and displays the error message in the browser. However, the code does not stop executing a ...

Is it possible to make a Javascript AJAX request without using server-side code?

I am currently working on a JavaScript file where I am attempting to make an AJAX call in order to retrieve JSON data. $.ajax({ type: "POST", url: "er.js", // this is the same file data: { action: 'analyzePost&ap ...

Error message encountered: "Node.js Object [object Object] does not support the method"

My application uses Express.js and mongodb, including the mongodb Native driver. I have created a model with two functions for retrieving posts and comments: // Retrieve single Post exports.posts = function(id,callback){ MongoClient.connect(url, fun ...

NPM is complaining about the absence of a start script

Apologies for any language barriers in my English. I'm currently facing an issue while trying to deploy an app on Heroku using the heroku local web command in the terminal. The error message ERR! missing script: start keeps popping up, even though the ...

Sorting tables with jQuery UI sortable() and rowspan功能

Can jQuery UI's sortable() be configured to sort rows based on their "containing" element only? I have a table with rowspanned cells that should only be sorted within their respective spanned columns. var $sortable = $('.nested-sortable tbody&ap ...

Typescript error encountered while attempting to fetch repository from Redis OM

I'm currently working on developing a shopping cart system with the use of expressjs, typescript, and redis OM for the in-memory database. As I created a schema using Redis OM, I encountered an error The error message says: 'Property 'fetc ...

Steer clear of using ng-repeat while also maintaining two-way data binding

My web application is quite large, and I frequently utilize the ng-repeat directive to display lists of data. However, this has resulted in slow performance at times, which can be very frustrating. While I am aware of different methods to avoid using ng- ...

Display the initial JSON data object without the need to choose a link with the help of AngularJS

I have successfully built a webpage that displays JSON data based on the selected link. However, I am facing an issue where I need to show the first JSON data object before selecting any link (initially). Check out the Plunker demo here: http://embed.plnk ...

Module not found in Node.js Express

Having trouble locating a module in Node.js Express Sample code provided below const express = require('express') const app = express() const port = 3000 app.get('/', (req, res) => { res.send('Hello World!') }) app.lis ...

Viewing Server Logs in a Discord Channel

My gaming server stores logs in .txt format, and I'm looking for a way to automatically send updates to a Discord channel whenever there is new log data. I am currently developing a Discord bot using Node.js. Does anyone have any suggestions on how I ...

Working with AJAX PHP variables within JavaScript

The code below was previously functioning correctly: var xmlHttp var layername var url function update(layer, url) { var xmlHttp=GetXmlHttpObject(); //you have this defined elsewhere if(xmlHttp==null) { alert("Your browser is not supporte ...

Using jQuery to find the index of an element with a particular child

I am working with three different div containers. <div class="box"></div> <div class="box"> <span class="item">my item</span> </div> <div class="box"></div> Within these containers, there is an item pla ...

In a scenario where a specific element is disabled, how can I link its value to another related element?

Setting: Ionic version: 6.20.1 Angular CLI version: 10.0.8 In the process of developing a mobile expense management application, I am working on implementing a feature that calculates the recommended spending for different categories (e.g., home expense ...

Utilizing Laravel5 to create captivating Highmaps visuals

Good day! I am currently setting up my database to work with HighMaps using Laravel5. I have received JSON data from Laravel in the following format: [{"hc-key":"es-vi"},{"hc-key":"es-cs"},{"hc-key":"es-lo"},{"hc-key":"es-z"}] Highmaps requires the data ...

Integrate a service component into another service component by utilizing module exports

After diving into the nestjs docs and exploring hierarchical injection, I found myself struggling to properly implement it within my project. Currently, I have two crucial modules at play. AuthModule is responsible for importing the UserModule, which conta ...

Update the navigation bar from displaying "LOGIN" to "LOGOUT

I am facing a challenge in updating the navbar login link to a logout link once the user logs in. I have attempted the following: header.ejs: <ul class="nav navbar-nav navbar-right"> <li id="home"><a href="/ ...

When you click on a list of links, a stylish image gallery will appear using fancybox

Can anyone lend a hand with this? I would greatly appreciate any assistance CSS <a id="fancybox" href="javascript:;">Gallery 1</a> <br /> <a id="fancybox" href="javascript:;">Gallery 2</a> <br /> <a id="fancybox" hr ...

Exploring the application of Redux in various contexts

Apologies for the vague question, but I'm a bit confused. I've recently started learning redux and now I need to integrate it into a fully-functioning project: https://github.com/CodeNinja1395/Test-task-for-inCode. There is a branch for the redux ...

Running tests to check for next(err) functionality using supertest and Express JS

When using Express in a route or middleware, you can halt the callback chain by calling next(err) with any object as err. This feature is well-documented and simple to understand. However, I encountered an issue when testing this behavior with SuperTest. ...