vysheng / tg

telegram-cli
GNU General Public License v2.0
6.51k stars 1.54k forks source link

Memory leak #558

Open ghost opened 9 years ago

ghost commented 9 years ago

After having a telegram daemon running for a few days it needs more and more memory. Like 200mb after 4 days. I don't know when this was intorduced. Probably a long time ago, because I used an old version for quite a while and hoped this was fixed by now. Unfortunately it is not. Since I am running tg on a Raspberry Pi I cannot provide 200mb of memory for one application. For now I am using the following script to restart tg every day via crontab:

#!/bin/bash
if [ -e /home/pi/tg/telegram.pid ]; then
    kill $(cat /home/pi/tg/telegram.pid)
    kill $(cat /home/pi/tg/telegram.pid)
    rm /home/pi/tg/telegram.pid
fi
/home/pi/tg/bin/telegram-cli -k /home/pi/tg/tg-server.pub -WR -s /home/pi/tg/bot.lua -d&
echo $! > /home/pi/tg/telegram.pid

btw: Why do I have to kill telegram-cli twice to kill it? Is that intended? I didn't feel like that is worth posting an issue

Thank you

LukeLR commented 9 years ago

My telegram daemon is running all day and all night since a couple of weeks now since the last restart, and It still uses not more than a few MB. BTW, it's a Raspberry Pi for me as well ;) Maybe it's some kind of configurational issue? Did you try re-downloading / re-building / re-configuring? Besides that, I have to confirm your issue in quitting telegram - On my Raspberry Pi the process only exits after sending the second kill to it.

ghost commented 9 years ago

Hi Luke, are you using a LUA script, too?

LukeLR commented 9 years ago

Yes, of course. Mine is checking the message contents of incoming messages for specific keywords, and then executing commands for different cases. Therefore it's not much to be done in the script, but it's definitively running fine for several hundreds of hours now. Maybe your lua script is the memory leak? You should check if the memory leak occurs even when launching the tg-cli without the script, and if this fixes the problem, you should clean your script as much as possible, checking for possible points that could eat memory. If I were you, I'd start with a clean lua script and then add functionalities one after another, to see at which point the amount of memory explodes, and then fix that point.

On 27 May 2015, at 1:21 am, andreaskoelsch notifications@github.com wrote:

Hi Luke, are you using a LUA script, too?

— Reply to this email directly or view it on GitHub.

vysheng commented 9 years ago

telegram-cli tries to end gracefully on receiving SIGTERM. I means it wait until current function is finished and process is back in main cycle and stops only then. So it catches SIGTERM signal and sets flag to stop. But if sets handler to default, so you can allways kill it with subsequent SIGTERM if it is necessary (for example if there is infinite loop somewhere in the code).

Telegram-cli keeps in memory information about all peers it sent message to and all messages it sent/received since last start. So in case your bot is heavy-loaded it can start to use more and more RAM

ghost commented 9 years ago

In 15h without sending a single message to my bot it got twice the size. Is that normal? :-/

27/05/15 02:00:02 PID RSS %MEM CMD 24667 5192 1.1 /home/pi/tg/bin/telegram-cli -k /home/pi/tg/tg-server.pub -WR -s /home/pi/tg/bot.lua -d

27/05/15 03:00:01 PID RSS %MEM CMD 24667 5560 1.2 /home/pi/tg/bin/telegram-cli -k /home/pi/tg/tg-server.pub -WR -s /home/pi/tg/bot.lua -d

27/05/15 04:00:01 PID RSS %MEM CMD 24667 5932 1.3 /home/pi/tg/bin/telegram-cli -k /home/pi/tg/tg-server.pub -WR -s /home/pi/tg/bot.lua -d

27/05/15 05:00:01 PID RSS %MEM CMD 24667 6304 1.4 /home/pi/tg/bin/telegram-cli -k /home/pi/tg/tg-server.pub -WR -s /home/pi/tg/bot.lua -d

27/05/15 06:00:01 PID RSS %MEM CMD 24667 6668 1.4 /home/pi/tg/bin/telegram-cli -k /home/pi/tg/tg-server.pub -WR -s /home/pi/tg/bot.lua -d

27/05/15 07:00:02 PID RSS %MEM CMD 24667 7036 1.5 /home/pi/tg/bin/telegram-cli -k /home/pi/tg/tg-server.pub -WR -s /home/pi/tg/bot.lua -d

27/05/15 08:00:02 PID RSS %MEM CMD 24667 7408 1.6 /home/pi/tg/bin/telegram-cli -k /home/pi/tg/tg-server.pub -WR -s /home/pi/tg/bot.lua -d

27/05/15 09:00:01 PID RSS %MEM CMD 24667 7784 1.7 /home/pi/tg/bin/telegram-cli -k /home/pi/tg/tg-server.pub -WR -s /home/pi/tg/bot.lua -d

27/05/15 10:00:01 PID RSS %MEM CMD 24667 8168 1.8 /home/pi/tg/bin/telegram-cli -k /home/pi/tg/tg-server.pub -WR -s /home/pi/tg/bot.lua -d

27/05/15 11:00:01 PID RSS %MEM CMD 24667 8548 1.9 /home/pi/tg/bin/telegram-cli -k /home/pi/tg/tg-server.pub -WR -s /home/pi/tg/bot.lua -d

27/05/15 12:00:01 PID RSS %MEM CMD 24667 8920 2.0 /home/pi/tg/bin/telegram-cli -k /home/pi/tg/tg-server.pub -WR -s /home/pi/tg/bot.lua -d

27/05/15 13:00:01 PID RSS %MEM CMD 24667 9300 2.0 /home/pi/tg/bin/telegram-cli -k /home/pi/tg/tg-server.pub -WR -s /home/pi/tg/bot.lua -d

27/05/15 14:00:02 PID RSS %MEM CMD 24667 9672 2.1 /home/pi/tg/bin/telegram-cli -k /home/pi/tg/tg-server.pub -WR -s /home/pi/tg/bot.lua -d

27/05/15 15:00:01 PID RSS %MEM CMD 24667 10040 2.2 /home/pi/tg/bin/telegram-cli -k /home/pi/tg/tg-server.pub -WR -s /home/pi/tg/bot.lua -d

27/05/15 16:00:01 PID RSS %MEM CMD 24667 10408 2.3 /home/pi/tg/bin/telegram-cli -k /home/pi/tg/tg-server.pub -WR -s /home/pi/tg/bot.lua -d

27/05/15 17:00:01 PID RSS %MEM CMD 24667 10780 2.4 /home/pi/tg/bin/telegram-cli -k /home/pi/tg/tg-server.pub -WR -s /home/pi/tg/bot.lua -d

ghost commented 9 years ago

I now had telegram-cli running for a few days without lua script and it did not need much more ram than when I started it. Then I tried with the given test.lua which I changed so it works for me. The file now looks like the following

started = 0
our_id = 0

function vardump(value, depth, key)
  local linePrefix = ""
  local spaces = ""

  if key ~= nil then
    linePrefix = "["..key.."] = "
  end

  if depth == nil then
    depth = 0
  else
    depth = depth + 1
    for i=1, depth do spaces = spaces .. "  " end
  end

  if type(value) == 'table' then
    mTable = getmetatable(value)
    if mTable == nil then
      print(spaces ..linePrefix.."(table) ")
    else
      print(spaces .."(metatable) ")
        value = mTable
    end     
    for tableKey, tableValue in pairs(value) do
      vardump(tableValue, depth, tableKey)
    end
  elseif type(value)    == 'function' or 
      type(value)   == 'thread' or 
      type(value)   == 'userdata' or
      value     == nil
  then
    print(spaces..tostring(value))
  else
    print(spaces..linePrefix.."("..type(value)..") "..tostring(value))
  end
end

print ("HI, this is lua script")

function ok_cb(extra, success, result)
end

function on_msg_receive (msg)
  if started == 0 then
    return
  end
  if msg.out then
    return
  end

  if (msg.text == 'ping') then
    if (msg.to.id == our_id) then
      send_msg (msg.from.print_name, 'pong', ok_cb, false)
    else
      send_msg (msg.to.print_name, 'pong', ok_cb, false)
    end
    return
  end
  if (msg.text == 'PING') then
    if (msg.to.id == our_id) then
      fwd_msg (msg.from.print_name, msg.id, ok_cb, false)
    else
      fwd_msg (msg.to.print_name, msg.id, ok_cb, false)
    end
    return
  end
end

function on_our_id (id)
  our_id = id
end

function on_user_update (user, what)
  --vardump (user)
end

function on_chat_update (chat, what)
  --vardump (chat)
end

function on_secret_chat_update (schat, what)
  --vardump (schat)
end

function on_get_difference_end ()
end

function cron()
  -- do something
  postpone (cron, false, 1.0)
end

function on_binlog_replay_end ()
  started = 1
  postpone (cron, false, 1.0)
end

So I basically just deleted the Notification section. Even with this minimal script I get the memory leak. Thus I assume it is not my script producing the leak, but a bug in telegram.

LukeLR commented 9 years ago

Just noticed I am experiencing a similar memory leak, although not as huge as your's… I didn't notice before, because I'm using a script that gathers different system stats (such as logged in users, processes, CPU-, Memory-, Bus-Frequencies (and so on), CPU-Temperature and CPU-Voltage over 2 minutes, etc) and it seems to calculate the percentage of the used memory incorrectly, therefore telegram never used much memory ;) But now, as I looked manually on the pi's terminal, I saw that there's a memory leak for me, too. See a screenshot from my second Pi attached: image It isn't bothering me much right now, because my B+-Model has enough RAM for this and I'm not running anything else on my Pi, but I think it should be fixed ;)

ghost commented 9 years ago

Well, 150M is quite a lot considering it needs ~5M when you start it....

LukeLR commented 9 years ago

Of course it is…

On 08 Jun 2015, at 10:39 am, andreaskoelsch notifications@github.com wrote:

Well, 150M is quite a lot considering it needs ~5M when you start it....

— Reply to this email directly or view it on GitHub.

gammy commented 8 years ago

150-200m? That's nothing. It's sucking up an excess of 4G after a few days here, haha.

fbt commented 8 years ago

I've left Telegram running through the weekends on my work machine (8G + 8G SWAP) and it went well into swap. After leaving it running during a vacation, I've determined that OOM killer killed the damn thing after just 3 days.

Granted, I have a lot of chats, but this is probably not the desired behaviour :D

mxmlnkn commented 7 years ago

This still is a problem in the latest master. Currently mine needs 1GB. I'm not even doing anything with it. I just started it for telegram-history-dump and then forgot to close it. It might have been at 1GB since the dump, though, I'm not sure.