Attachment Memory Leak

Hi, I m experiencing a memory leak. Here is the command answer, this cause a memory leak of 5mb. In the second snippet there is no more issue, so I think its the node canvas librairy. Any idea of what Im doing wrong ? I already tried : - using canvacord - using sharpjs - sending outside of embed (without setImage) - all types of files Same problem eveytime MEMORY LEAK
callback: async (client, interaction) => {
await interaction.deferReply();

const canvas = Canvas.createCanvas(736, 882);
const context = canvas.getContext("2d");
const img = await Canvas.loadImage(`src/assets/cards/${cardId}.jpg`);
context.drawImage(img, 0, 0);

const attachment = new AttachmentBuilder(await canvas.encode("png"), {
name: "profile-image.png",
});
const embed = new EmbedBuilder().setImage(
"attachment://profile-image.png"
);

await interaction.editReply({
embeds: [embed],
files: [attachment],
ephemeral: false,
});
},
callback: async (client, interaction) => {
await interaction.deferReply();

const canvas = Canvas.createCanvas(736, 882);
const context = canvas.getContext("2d");
const img = await Canvas.loadImage(`src/assets/cards/${cardId}.jpg`);
context.drawImage(img, 0, 0);

const attachment = new AttachmentBuilder(await canvas.encode("png"), {
name: "profile-image.png",
});
const embed = new EmbedBuilder().setImage(
"attachment://profile-image.png"
);

await interaction.editReply({
embeds: [embed],
files: [attachment],
ephemeral: false,
});
},
NO MEMORY LEAK
callback: async (client, interaction) => {
await interaction.deferReply();
const attachment = new AttachmentBuilder(
`src/assets/cards/${cardId}.jpg`,
{
name: "profile-image.png",
}
);
const embed = new EmbedBuilder().setImage(
"attachment://profile-image.png"
);

await interaction.editReply({
embeds: [embed],
files: [attachment],
ephemeral: false,
});
},
callback: async (client, interaction) => {
await interaction.deferReply();
const attachment = new AttachmentBuilder(
`src/assets/cards/${cardId}.jpg`,
{
name: "profile-image.png",
}
);
const embed = new EmbedBuilder().setImage(
"attachment://profile-image.png"
);

await interaction.editReply({
embeds: [embed],
files: [attachment],
ephemeral: false,
});
},
USING :
"@napi-rs/canvas": "^0.1.44",
"discord.js": "^14.14.1",
"@napi-rs/canvas": "^0.1.44",
"discord.js": "^14.14.1",
And node : v21.2.0
13 Replies
d.js toolkit
d.js toolkit•10mo 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!
7te3ep
7te3ep•10mo ago
I can provide any information, test, code snippet.... Feel free to ask
Kevinnnn
Kevinnnn•10mo ago
judging by the code you provided, the image you creating using canvas is still stored in memory, therefore you have a "memory leak"
7te3ep
7te3ep•10mo ago
Yeah, but how can I remove it ? I already tried to
const img = ''
const img = ''
In order to "clear it", but it doesn't work
Kevinnnn
Kevinnnn•10mo ago
which canvas package are you using?
7te3ep
7te3ep•10mo ago
"@napi-rs/canvas": "^0.1.44", But same error for canvacord, and sharpjs, and chartjs *same problem, there is no error until my server run out of ram
Kevinnnn
Kevinnnn•10mo ago
best way to go about it is to use context.clearRect(0, 0, canvas.width, canvas.height) I don't know if it'll be any better over the long term, but that should basically remove the bulk of the memory reference
7te3ep
7te3ep•10mo ago
So creating one single instance of canvas, and clearing it before re using it again? Because this doesnt work... Any ideas ? It restart. I think its a host thing to make it automatically restart. So if I create only one instance of canvas, and clearrect each time, the issue is still here, each image generated by canvas adds 50mb to ram No, but I asked my host and its due to the ram being full
7te3ep
7te3ep•10mo ago
No description
7te3ep
7te3ep•10mo ago
I can see it myself, its all on the server panel in real time its a grid of 3x2, for 400*400 img in jpg
7te3ep
7te3ep•10mo ago
No description
7te3ep
7te3ep•10mo ago
here it is this is for one "generation" this add 50mb to ram, its not a problem, but it stays in ram, like the memory usage doest go back down Its possible,but im calling manually the gc, and I tried to let the bot at 200mb for like 15 hours, nothing changed You guessed it, I use the flag --expose-gc and then gc() Maybe its not exactly like the native gc, but its something similar bc its not an external librairy, its into node
ThePedroo
ThePedroo•10mo ago
global.gc behaves the same way as it would without it, but it will early activate gc Also, for debugging forgotten memory, you will probably need to use heap snapshots And look for addition of data of global variables, as they never get cleaned by GC
Want results from more Discord servers?
Add your server