Memory

hey so, im using chokidar + function to send a message to a channel but everytime i do. Im assuming it caches the attachment sent because, it jumps +8mb of memory everytime the function is called and holds that memory and keeps adding to it when the function is called another time. i have messages cache set to 0 so it cant really cache messages, so why is it using 8mb of memory to send a video and holding the memory hostage 💔 it calls the function every 6 minutes and every 6minutes it adds +8mb to the memory which is an L.
async function sendMedia(receivedFile, mediaChannel) {
try {
const fileName = receivedFile;
const filePath = `${fileName}`;

const embedData = (an embed)

const attachmentToSend = new AttachmentBuilder(filePath);
const content = await mediaChannel.send({
files: [attachmentToSend],
embeds: [embedData],
});
await content.crosspost().catch((error) => console.error(error));

fs.unlink(filePath).catch((error) => console.error(error));
} catch (error) {
console.error(error);
}
}
async function sendMedia(receivedFile, mediaChannel) {
try {
const fileName = receivedFile;
const filePath = `${fileName}`;

const embedData = (an embed)

const attachmentToSend = new AttachmentBuilder(filePath);
const content = await mediaChannel.send({
files: [attachmentToSend],
embeds: [embedData],
});
await content.crosspost().catch((error) => console.error(error));

fs.unlink(filePath).catch((error) => console.error(error));
} catch (error) {
console.error(error);
}
}
4 Replies
d.js toolkit
d.js toolkit•3mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - ✅ Marked as resolved by OP
Lawa
Lawa•3mo ago
function setupChokidarWatchers() {
const watcherImage = chokidar.watch(imageFolder, {
ignored: /(^|[\/\\])\../,
persistent: true,
ignoreInitial: true,
awaitWriteFinish: {
stabilityThreshold: 2000,
pollInterval: 100,
},
});

const watcherVideo = chokidar.watch(videoFolder, {
ignored: /(^|[\/\\])\../,
persistent: true,
ignoreInitial: true,
awaitWriteFinish: {
stabilityThreshold: 2000,
pollInterval: 100,
},
});

if (watcherImage && watcherVideo) {
watcherVideo.on("add", (fileName) => {
sendMedia(fileName, vidchannel);
});

watcherImage.on("add", (fileName) => {
sendMedia(fileName, imgchannel);
});
}
}
function setupChokidarWatchers() {
const watcherImage = chokidar.watch(imageFolder, {
ignored: /(^|[\/\\])\../,
persistent: true,
ignoreInitial: true,
awaitWriteFinish: {
stabilityThreshold: 2000,
pollInterval: 100,
},
});

const watcherVideo = chokidar.watch(videoFolder, {
ignored: /(^|[\/\\])\../,
persistent: true,
ignoreInitial: true,
awaitWriteFinish: {
stabilityThreshold: 2000,
pollInterval: 100,
},
});

if (watcherImage && watcherVideo) {
watcherVideo.on("add", (fileName) => {
sendMedia(fileName, vidchannel);
});

watcherImage.on("add", (fileName) => {
sendMedia(fileName, imgchannel);
});
}
}
thats my chokidar, its well optimised and it do run 24/7, i need it to i dont think the issue is with chokidar, i use the same in another process and it doesnt leak any memory so im hoping that djs is caching its own attachment it sent and keeping a hold of it?
const bot = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildWebhooks],
makeCache: Options.cacheWithLimits({
...Options.DefaultMakeCacheSettings,
MessageManager: 0,
ReactionManager: 0,
VoiceStateManager: 0,
UserManager: 0,
ThreadMemberManager: 0,
ThreadManager: 0,
StageInstanceManager: 0,
ReactionUserManager: 0,
PresenceManager: 0,
GuildStickerManager: 0,
GuildScheduledEventManager: 0,
GuildInviteManager: 0,
GuildBanManager: 0,
GuildMemberManager: 0,
GuildEmojiManager: 0,
BaseGuildEmojiManager: 0,
AutoModerationRuleManager: 0,
ApplicationCommandManager: 0,
}),
});
const bot = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildWebhooks],
makeCache: Options.cacheWithLimits({
...Options.DefaultMakeCacheSettings,
MessageManager: 0,
ReactionManager: 0,
VoiceStateManager: 0,
UserManager: 0,
ThreadMemberManager: 0,
ThreadManager: 0,
StageInstanceManager: 0,
ReactionUserManager: 0,
PresenceManager: 0,
GuildStickerManager: 0,
GuildScheduledEventManager: 0,
GuildInviteManager: 0,
GuildBanManager: 0,
GuildMemberManager: 0,
GuildEmojiManager: 0,
BaseGuildEmojiManager: 0,
AutoModerationRuleManager: 0,
ApplicationCommandManager: 0,
}),
});
and finally thats my cache settings nodejs 18 update not a djs issue, i used to download with "stream" and write the file with write:
async function downloadFile(url, directory) {
try {
const response = await axios.get(url, { responseType: "stream" });
const fileName = `attachment.${url.split(".").pop().split("?")[0]}`;
const filePath = path.join(directory, fileName);
const writer = fs.createWriteStream(filePath);
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on("finish", () => resolve(fileName));
writer.on("error", reject);
});
} catch (error) {
console.error(`Error downloading file from ${url}:`, error);
return null;
}
}
async function downloadFile(url, directory) {
try {
const response = await axios.get(url, { responseType: "stream" });
const fileName = `attachment.${url.split(".").pop().split("?")[0]}`;
const filePath = path.join(directory, fileName);
const writer = fs.createWriteStream(filePath);
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on("finish", () => resolve(fileName));
writer.on("error", reject);
});
} catch (error) {
console.error(`Error downloading file from ${url}:`, error);
return null;
}
}
updated to:
async function downloadFile(url, directory) {
try {
const response = await axios.get(url, { responseType: "arraybuffer" }); // Change responseType to arraybuffer
const fileName = `attachment.${url.split(".").pop().split("?")[0]}`;
const filePath = path.join(directory, fileName);
await fs.promises.writeFile(filePath, response.data); // Write response data synchronously
return fileName;
} catch (error) {
console.error(`Error downloading file from ${url}:`, error);
}
}
async function downloadFile(url, directory) {
try {
const response = await axios.get(url, { responseType: "arraybuffer" }); // Change responseType to arraybuffer
const fileName = `attachment.${url.split(".").pop().split("?")[0]}`;
const filePath = path.join(directory, fileName);
await fs.promises.writeFile(filePath, response.data); // Write response data synchronously
return fileName;
} catch (error) {
console.error(`Error downloading file from ${url}:`, error);
}
}
which uses less memory now like 30mb less, but it still increases non stop everytime i download and write a file to my machine. should i open a question in #other-js-ts ? yes djs downloads it for me? oh thats cool ill do that
const content = await mediaChannel.send({
files: ["https://discord.com/image.png"],
embeds: [embedData],
});
const content = await mediaChannel.send({
files: ["https://discord.com/image.png"],
embeds: [embedData],
});
just like that? yep
manikin
manikin•3mo ago
ok so that wouldnt work bc i need to download the file to rename it. so i have to use writeFile and axios. sorry again replying from anothwr account i cant reach my computer thats a question idk how to answer 💔 could the problem we with axios or write file its hard to debug it but yeah i monitored it and it literally holds the attachment in the memory after downloading it so idk.
Lawa
Lawa•3mo ago
i also seek to download it bc i want to rename the file too, i cant let djs handle it from the url oh okay using fetch the memory dropped around 20mb and stopped memory leaking, ig it was axios so my issue is resolved