Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak #13

Closed
pyed opened this issue Aug 9, 2015 · 7 comments
Closed

Memory leak #13

pyed opened this issue Aug 9, 2015 · 7 comments

Comments

@pyed
Copy link
Contributor

pyed commented Aug 9, 2015

Sending large files with SendDocument or SendVideo will result in high memory usage, and after the file done sending, this memory won't get freed

consider the following code

package main

import (
    "log"

    "github.com/Syfaro/telegram-bot-api"
)

func main() {
    bot, err := tgbotapi.NewBotAPI("MyAwesomeToken")
    if err != nil {
        log.Panic(err)
    }

    // bot.Debug = true

    log.Printf("Authorized on account %s", bot.Self.UserName)

    u := tgbotapi.NewUpdate(0)
    u.Timeout = 60

    err = bot.UpdatesChan(u)
    if err != nil {
        log.Panic(err)
    }

    filename := "file.dat" // ~33Mb, should be fairly a big file to notice
    for update := range bot.Updates {
        log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)

        msg := tgbotapi.NewMessage(update.Message.Chat.ID, "the file on its way")
        msg.ReplyToMessageID = update.Message.MessageID
        bot.SendMessage(msg)

        newDoc := tgbotapi.NewDocumentUpload(update.Message.Chat.ID, filename)
        if _, err := bot.SendDocument(newDoc); err != nil {
            log.Panic(err)
        }

    }
}

Now running this bot on OS X will start with 8.2 MB of Real Mem "according to Activity Monitor"
after sending the first message, the bot will start sending file.dat, Real Mem will jump to 107.6 MB it will finish sending but it will not free the memory, requesting the file again will rise Real Mem again to 180 MB ... etc

I took a look at the code and I figured it has to be in the UploadFile method, and I found that a usage to os.Open() bond to a variable called f hasn't get closed, I tried closing it, but it did not solve the problem.

@Syfaro
Copy link
Member

Syfaro commented Aug 9, 2015

Hmm... you're definitely right that there's a memory leak somewhere.

I think it has something to do with the form, but I'm not entirely sure. I'll work on figuring out what's happening.

@Syfaro
Copy link
Member

Syfaro commented Aug 9, 2015

Well, this is what's using the most memory.

(pprof) top
26184.78kB of 26184.78kB total (  100%)
Dropped 33 nodes (cum <= 130.92kB)
      flat  flat%   sum%        cum   cum%
   24648kB 94.13% 94.13%    24648kB 94.13%  bytes.makeSlice

report

@Syfaro
Copy link
Member

Syfaro commented Aug 9, 2015

This might be a limitation in Go.

moby/moby#1922 (comment)

@pyed
Copy link
Contributor Author

pyed commented Aug 10, 2015

so nothing we can do about it ?

I can confirm that github.com/rockneurotiko/go-tgbot has the same issue

@Syfaro
Copy link
Member

Syfaro commented Aug 10, 2015

Well, this commit will reduce memory usage, a 30MiB test file increases my memory usage by ~2MB.

That upload function is pretty much identical to mine, so I don't know what the difference could be.

@pyed
Copy link
Contributor Author

pyed commented Aug 10, 2015

I just tried it out, it is a great improvement.

@Syfaro
Copy link
Member

Syfaro commented Aug 10, 2015

If in the future anyone comes up with a better solution, I'd be happy to reopen this. Until then, I'm going to close it.

@Syfaro Syfaro closed this as completed Aug 10, 2015
ilpy20 pushed a commit to ilpy20/telegram-bot-api that referenced this issue Feb 18, 2024
Instructions on how to use `OvyFlash/telegram-bot-api` by replacing the original dependency
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants