My deployed app has a function that makes a call to a database whenever the app.get("/")
route is called, or when the user adds or deletes a file to the database. My service is being limited by my web host when this function is called, because it exceeds my memory allotment, causing a failure in one of my npm packages (sharp) not having the processor threads available to process an image.
I've studied the metrics provided by my web host, but aside from telling me a fault has occurred, it does not show how large the memory usage is, or where. Using the Chrome DevTools task manager I can see that the live javascript memory does in fact gain slowly over the runtime of the app, but not more than other webpages I've looked at.
I have no mentors in the field, and after some exhaustive research on memory leaks, what causes them, and how to troubleshoot them, I'm left lost and confused, but I do believe I've narrowed it down to the culprit of the problem. I just don't know what to do from here.
Here is the function in question:
let artWorks = [];
async function listFiles() {
artWorks = []; //clears the array if it has content
const [files] = await storage.bucket(bucketName).getFiles(); //call for url names of files in DB
files.forEach(file => { //loops through all files and pushes them to the array, separating the URL string into usable data
function splitStr(str) {
var string = str.split("/");
artWorks.push(
{
imgCategory: string[0],
subCat: string[1].replace(/-/g, ' '),
imgSrc: bucket + file.name,
thumbnail: thumbBucket + file.name.split('.')[0] + "-thumb.jpg",
alt: string[2]
});
}
var str = file.name;
splitStr(str);
});
console.log("files listed");
}
The artWorks
array is the crux of this whole app. Every route uses the contents of this array to populate the ejs template pages. Once a user adds or deletes a file, the listFiles()
function is called again to repopulate the array.
I have not noticed any slow down in performance, and if it wasn't for my web host capping my memory, I never would have known there was a leak. Please offer me guidance, hopefully not getting too involved in terminology, but in terms a new developer like myself can understand.