WhiskeySockets / Baileys

Lightweight full-featured typescript/javascript WhatsApp Web API
https://baileys.whiskeysockets.io/
MIT License
3.1k stars 1.07k forks source link

Send Media Message Memory Leak #745

Open mikelx99 opened 2 months ago

mikelx99 commented 2 months ago

Hello, my Baileys bot has a high memory usage problem. When the bot sends an image, the RAM usage increases. If the bot sends a lot of images, the memory usage keeps increasing and it does not release that memory. This continues until the system memory limit is reached and the system kills my bot.

Even after the bot reaches 800MB memory usage, leaving it idle for 1 days does not release that memory.

However, when I send a document message (500MB document), the memory is released after the message sent successfully. Currently, this issue occurs when sending images. Please help me fix this issue. Thank you

Connection Code

const {
  makeWASocket,
  DisconnectReason,
  useMultiFileAuthState,
  fetchLatestBaileysVersion,
  makeCacheableSignalKeyStore,
} = require("@whiskeysockets/baileys");
const { Boom } = require("@hapi/boom");
const pino = require("pino");
const messageHandler = require("../handlers/messageHandler");
require("dotenv").config();

//setup logger
const logger = pino();
logger.level = "fatal";

//node cache
// const msgRetryCounterCache = new NodeCache();

async function connectToWhatsApp() {
  const { state, saveCreds } = await useMultiFileAuthState("auth_tvinfo");
  const { version, isLatest } = await fetchLatestBaileysVersion();
  console.log(`v${version.join(".")}, isLatest: ${isLatest}`);
  const sock = makeWASocket({
    // can provide additional config here
    printQRInTerminal: true,
    auth: {
      creds: state.creds,
      /** caching makes the store faster to send/recv messages */
      keys: makeCacheableSignalKeyStore(state.keys, logger),
    },
    logger,
    // msgRetryCounterCache,
  });

  sock.ev.process(async (events) => {
    if (events["connection.update"]) {
      const update = events["connection.update"];
      const { connection, lastDisconnect } = update;
      if (connection === "close") {
        // reconnect if not logged out
        if (
          lastDisconnect?.error instanceof Boom &&
          lastDisconnect.error.output.statusCode !== DisconnectReason.loggedOut
        ) {
          connectToWhatsApp();
        } else {
          console.log(`Connection closed. You are logged out.`);
        }
      } else if (connection === "open") {
        console.log("Connected 🙃");
      }
    }
    if (events["creds.update"]) {
      await saveCreds();
    }
  });

  // Handle Messages
  messageHandler(sock);
}

Handle Image Code

const messageHandler = async function (sock) {
  sock.ev.on("messages.upsert", async (m) => {

    const msg = m.messages[0];

    const msgText = getText(msg);
    const imageUrl = getImgUrl(msgText);

    const reply = await sock.sendMessage(
    msg.key.remoteJid,
    {
      image: {
        url: imageUrl,
      },
      caption: msgText,
    },
    { quoted: msg }
  );

}}

image

mikelx99 commented 2 months ago

JS Memory Log

0|app | rss: 387 MB 0|app | heapTotal: 38 MB 0|app | heapUsed: 35 MB 0|app | external: 203 MB 0|app | arrayBuffers: 199 MB 0|app | . 0|app | . 0|app | rss: 396 MB 0|app | heapTotal: 38 MB 0|app | heapUsed: 35 MB 0|app | external: 212 MB 0|app | arrayBuffers: 209 MB 0|app | . 0|app | . 0|app | rss: 403 MB 0|app | heapTotal: 38 MB 0|app | heapUsed: 35 MB 0|app | external: 218 MB 0|app | arrayBuffers: 215 MB 0|app | . 0|app | . 0|app | rss: 412 MB 0|app | heapTotal: 38 MB 0|app | heapUsed: 35 MB 0|app | external: 229 MB 0|app | arrayBuffers: 225 MB 0|app | . 0|app | . 0|app | rss: 420 MB 0|app | heapTotal: 38 MB 0|app | heapUsed: 35 MB 0|app | external: 234 MB 0|app | arrayBuffers: 231 MB 0|app | . 0|app | . 0|app | rss: 428 MB 0|app | heapTotal: 38 MB 0|app | heapUsed: 36 MB 0|app | external: 245 MB 0|app | arrayBuffers: 242 MB 0|app | . 0|app | . 0|app | rss: 436 MB 0|app | heapTotal: 38 MB 0|app | heapUsed: 36 MB 0|app | external: 252 MB 0|app | arrayBuffers: 248 MB 0|app | . 0|app | . 0|app | rss: 442 MB 0|app | heapTotal: 38 MB 0|app | heapUsed: 35 MB 0|app | external: 257 MB 0|app | arrayBuffers: 253 MB

samuelrac commented 1 month ago

Did you manage to find a solution to this problem?

Alien-Alfa commented 1 month ago

const messageHandler = async function (sock) { sock.ev.on("messages.upsert", async (m) => {

const msg = m.messages[0];

const msgText = getText(msg);
const imageUrl = getImgUrl(msgText);

return await sock.sendMessage(
msg.key.remoteJid,
{
  image: {
    url: imageUrl,
  },
  caption: msgText,
},
{ quoted: msg }

);

}) }

try return