Python memory leaks

I have a long-running script which, if let to run long enough, will consume all the memory on my system.

Without going into details about the script, I have two questions:

  • Are there any "Best Practices" to follow, which will help prevent leaks from occurring?
  • What techniques are there to debug memory leaks in Python?

  • Have a look at this article: Tracing python memory leaks

    Also, note that the garbage collection module actually can have debug flags set. Look at the set_debug function. Additionally, look at this code by Gnibbler for determining the types of objects that have been created after a call.


    I tried out most options mentioned previously but found this small and intuitive package to be the best: pympler

    It's quite straight forward to trace objects that were not garbage-collected, check this small example:

    install package via pip install pympler

    from pympler.tracker import SummaryTracker
    tracker = SummaryTracker()
    
    # ... some code you want to investigate ...
    
    tracker.print_diff()
    

    The output shows you all the objects that have been added, plus the memory they consumed.

    Sample output:

                                     types |   # objects |   total size
    ====================================== | =========== | ============
                                      list |        1095 |    160.78 KB
                                       str |        1093 |     66.33 KB
                                       int |         120 |      2.81 KB
                                      dict |           3 |       840 B
          frame (codename: create_summary) |           1 |       560 B
              frame (codename: print_diff) |           1 |       480 B
    

    This package provides a number of more features. Check pympler's documentation, in particular the section Identifying memory leaks.


    Let me recommend mem_top tool,
    that helped me to solve a similar issue.

    It just instantly shows top suspects for memory leaks in a Python program.

    链接地址: http://www.djcxy.com/p/18386.html

    上一篇: 如何检查dbexpress组件是否存在内存泄漏?

    下一篇: Python内存泄漏