How to find the largest objects in memory?

前端 未结 1 1011
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-09 19:32

I want to have a table of 10 largest objects in memory, with size.

Equivalent function in R: Tricks to manage the available memory in an R session

lsos()         


        
1条回答
  •  梦毁少年i
    2021-02-09 20:21

    The simplest is to use Pympler:

    from operator import itemgetter
    
    from pympler import tracker
    
    mem = tracker.SummaryTracker()
    print(sorted(mem.create_summary(), reverse=True, key=itemgetter(2))[:10])
    

    Output:

    [["

    Of course, you can also create a pandas dataframe and work with this:

    memory = pd.DataFrame(mem.create_summary(), columns=['object', 'number_of_objects', 'memory'])
    memory['mem_per_object'] = memory['memory'] / memory['number_of_objects']
    print(memory.sort_values('memory', ascending=False).head(10))
    print(memory.sort_values('mem_per_object', ascending=False).head(10))
    

    Output:

                                               object  number_of_objects     memory  mem_per_object
    11                                  .Laz...                  2    66000    33000.000000
    10637                  .La...                  2     9248     4624.000000
    1594                               

    0 讨论(0)
提交回复
热议问题