#Preventing High RAM Usage

10 messages · Page 1 of 1 (latest)

chrome token

I have a code snippet of guildMemberAdd event and I think it is causing very high ram usage, I need suggestions in order to prevent it.

normal bridgeBOT
chrome token

if (WelcomerData?.use_image == 1) {
        number = Math.floor(Math.random() * 79);
        console.log('[guildMemberAdd] Using image number:', number);
        const card = fs.readFileSync(`./Welcome/${number}.png`);
// Read a random 2-4MB File saved locally for image background

        welcomeImageAttachment = await new canvas.Welcome()
            .setUsername(member.user.username)
            .setDiscriminator(member.user.discriminator)
            .setGuildName(member.guild.name)
            .setAvatar(member.user.displayAvatarURL({ dynamic: false, extension: 'jpg' }))
            .setMemberCount(member.guild.memberCount)
            .setBackground(card)
            .setColor('border', WelcomerData.color.slice(0,7) || '#8015EA')
            .setColor('username-box', WelcomerData.color.slice(0,7) || '#8015EA')
            .setColor('discriminator-box', WelcomerData.color.slice(0,7) || '#8015EA')
            .setColor('message-box', WelcomerData.color.slice(0,7) || '#8015EA')
            .setColor('title', WelcomerData.color.slice(0,7) || '#8015EA')
            .setColor('avatar', WelcomerData.color.slice(0,7) || '#8015EA')
            .toAttachment();
// Welcomer Image making complete

        buffer.files = [{
            attachment: welcomeImageAttachment.toBuffer(),
            name: 'Welcome.png',
        }];
        console.log('[guildMemberAdd] Welcome image prepared.');


// The 2 lines below, are they useful in preventing high ram usage?
        welcomeImageAttachment = null;
        card = null;

<channel>.send(buffer) // buffer variable contains embed, message, and image

    }

This is the code that gets executed everytime a member joins a server
It is using discord-canvas package though,

I need suggestions, like should i use garbage collector (and how), defining variables as null works or not etc
Thanks 😄

south moat

How do you know this is causing high ram usage? You should do some memory profiling if it’s an issue. However it’s likely what intents you use causing higher memory usage

Also, you should consider using fs/promises for reading files. Using the sync will block the event loop, whereas using the async version won’t

chrome token

okk i will do the things u have suggested, thankss 😄

south moat

Chances are the fs/promises won’t make a difference. But I do remember some of those libs having memory leaks

chrome token

Ok so I think it's not my code that's causing ram usage,
Its the members cache that's probably causing high ram usage cuz I have members intent 😅

Either way my problem is solved 😄