Node.js memory leak error
I have this ts code that give me memory leak error every time:
after some research i found that if i deleted this part the code works fine:
can any one help me fix the error. this code is in next.js app router in the server side
type Conns = {
name: string,
platform: string,
jobs: job[]
}[];
type UserType = {
id: string;
email: string;
interval: number;
next: number;
connections: Conns;
};
const user: UserType = userData;
const allJobs: Conns[] = [];
const firstTime = user.connections.reduce((min, conn) => {
const jobDate = Math.min(...conn.jobs.map(job => job.date));
return jobDate < min ? jobDate : min;
}, Infinity);
const lastTime = user.next - user.interval;
const cachedJobs = user.connections.map(c => ({
...c,
jobs: c.jobs.sort((a, b) => a.date - b.date)
}));
const intervalCount = Math.floor((lastTime - firstTime) / user.interval);
for (let i = 1; i <= intervalCount; i++) {
const intervalTime = lastTime - (user.interval * i * 60 * 1000);
const jobsInInterval = cachedJobs.map(c => {
const jobs = c.jobs.filter(j => j.date > intervalTime);
return {...c, jobs};
});
allJobs.push(jobsInInterval);
}
type Conns = {
name: string,
platform: string,
jobs: job[]
}[];
type UserType = {
id: string;
email: string;
interval: number;
next: number;
connections: Conns;
};
const user: UserType = userData;
const allJobs: Conns[] = [];
const firstTime = user.connections.reduce((min, conn) => {
const jobDate = Math.min(...conn.jobs.map(job => job.date));
return jobDate < min ? jobDate : min;
}, Infinity);
const lastTime = user.next - user.interval;
const cachedJobs = user.connections.map(c => ({
...c,
jobs: c.jobs.sort((a, b) => a.date - b.date)
}));
const intervalCount = Math.floor((lastTime - firstTime) / user.interval);
for (let i = 1; i <= intervalCount; i++) {
const intervalTime = lastTime - (user.interval * i * 60 * 1000);
const jobsInInterval = cachedJobs.map(c => {
const jobs = c.jobs.filter(j => j.date > intervalTime);
return {...c, jobs};
});
allJobs.push(jobsInInterval);
}
for (let i = 1; i <= intervalCount; i++) {
const intervalTime = lastTime - (user.interval * i * 60 * 1000);
const jobsInInterval = cachedJobs.map(c => {
const jobs = c.jobs.filter(j => j.date > intervalTime);
return {...c, jobs};
});
allJobs.push(jobsInInterval);
}
for (let i = 1; i <= intervalCount; i++) {
const intervalTime = lastTime - (user.interval * i * 60 * 1000);
const jobsInInterval = cachedJobs.map(c => {
const jobs = c.jobs.filter(j => j.date > intervalTime);
return {...c, jobs};
});
allJobs.push(jobsInInterval);
}
1 Reply
Are you sure you're leaking memory & not just blowing the stack with too much data?