簡體   English   中英

如何使用pympler跟蹤/修復tornado-redis中的內存泄漏?

[英]How can I track/fix memory leak in tornado-redis using pympler?

我一直在嘗試使用龍卷風Redis的 (這基本上是一個叉brükva略作修改與tornado.gen接口而不是adisp工作),以便通過使用提供事件的Redis'發布訂閱

所以我寫了一個小腳本來測試這個例子的靈感。

import os

from tornado import ioloop, gen
import tornadoredis


print os.getpid()

def on_message(msg):
    print msg

@gen.engine
def listen():
    c = tornadoredis.Client()
    c.connect()
    yield gen.Task(c.subscribe, 'channel')
    c.listen(on_message)

listen()

ioloop.IOLoop.instance().start()

不幸的是,當我通過redis-cli PUBLISH內存使用率一直在上升。

為了描述內存使用情況,我首先嘗試使用guppy-pe,但它不能在python 2.7下運行(是的,甚至嘗試過后備箱),所以我又回到了pympler

import os

from pympler import tracker
from tornado import ioloop, gen
import tornadoredis


print os.getpid()

class MessageHandler(object):

    def __init__(self):
        self.memory_tracker = tracker.SummaryTracker()

    def on_message(self, msg):
        self.memory_tracker.print_diff()

@gen.engine
def listen():
    c = tornadoredis.Client()
    c.connect()
    yield gen.Task(c.subscribe, 'channel')
    c.listen(MessageHandler().on_message)

listen()

ioloop.IOLoop.instance().start()

現在,每次我PUBLISH我都能看到一些對象從未發布過:

                                            types |   # objects |   total size
===================================================== | =========== | ============
                                                 dict |          32 |     14.75 KB
                                                tuple |          41 |      3.66 KB
                                                  set |           8 |      1.81 KB
                                       instancemethod |          16 |      1.25 KB
                                                 cell |          22 |      1.20 KB
                          function (handle_exception) |           8 |    960     B
                                     function (inner) |           7 |    840     B
                                            generator |           8 |    640     B
                             <class 'tornado.gen.Task |           8 |    512     B
                           <class 'tornado.gen.Runner |           8 |    512     B
  <class 'tornado.stack_context.ExceptionStackContext |           8 |    512     B
                                                 list |           3 |    504     B
                                                  str |           7 |    353     B
                                                  int |           7 |    168     B
                           builtin_function_or_method |           2 |    144     B
                                                types |   # objects |   total size
===================================================== | =========== | ============
                                                 dict |          32 |     14.75 KB
                                                tuple |          42 |      4.23 KB
                                                  set |           8 |      1.81 KB
                                                 cell |          24 |      1.31 KB
                                       instancemethod |          16 |      1.25 KB
                          function (handle_exception) |           8 |    960     B
                                     function (inner) |           8 |    960     B
                                            generator |           8 |    640     B
                             <class 'tornado.gen.Task |           8 |    512     B
                           <class 'tornado.gen.Runner |           8 |    512     B
  <class 'tornado.stack_context.ExceptionStackContext |           8 |    512     B
                                               object |           8 |    128     B
                                                  str |           2 |    116     B
                                                  int |           1 |     24     B
                                                types |   # objects |   total size
===================================================== | =========== | ============
                                                 dict |          32 |     14.75 KB
                                                tuple |          42 |      4.73 KB
                                                  set |           8 |      1.81 KB
                                                 cell |          24 |      1.31 KB
                                       instancemethod |          16 |      1.25 KB
                          function (handle_exception) |           8 |    960     B
                                     function (inner) |           8 |    960     B
                                            generator |           8 |    640     B
                             <class 'tornado.gen.Task |           8 |    512     B
                           <class 'tornado.gen.Runner |           8 |    512     B
  <class 'tornado.stack_context.ExceptionStackContext |           8 |    512     B
                                                 list |           0 |    240     B
                                               object |           8 |    128     B
                                                  int |          -1 |    -24     B
                                                  str |           0 |    -34     B

現在我知道確實存在內存泄漏,如何跟蹤這些對象的創建位置? 我想我應該從這里開始?

將Tornado升級到版本2.3應該可以解決這個問題。

我遇到了ExceptionStackContext很快泄漏的問題。 它與此錯誤報告有關: https//github.com/facebook/tornado/issues/507並在此提交中修復: https//github.com/facebook/tornado/commit/57a3f83fc6b6fa4d9c207dc078a337260863ff99 升級到2.3為我解決了這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM