1. 程式人生 > >Python 物件記憶體佔用

Python 物件記憶體佔用

轉自:http://www.cnblogs.com/Lvkun/archive/2012/03/01/python_object_memory_usage.html

之前寫的指令碼中,需要估計程式的記憶體佔用,所以簡單研究下Python各種物件在記憶體中佔用大小。

本人對 Python 一直處在使用的階段,沒有進行深入研究。所以有什麼錯誤還請指出,歡迎交流。

一切皆是物件

在 Python 一切皆是物件,包括所有型別的常量與變數,整型,布林型,甚至函式。 參見stackoverflow上的一個問題 Is everything an object in python like ruby

程式碼中即可以驗證:

 # everythin in python is object def fuction(): return print isinstance(True, object) print isinstance(0, object) print isinstance('a', object) print isinstance(fuction, object) 

如何計算

Python 在 sys 模組中提供函式 getsizeof 來計算 Python 物件的大小。

sys.getsizeof(object[, default])

以位元組(byte)為單位返回物件大小。 這個物件可以是任何型別的物件。 所以內建物件都能返回正確的結果 但不保證對第三方擴充套件有效,因為和具體實現相關。

......

getsizeof() 呼叫物件的 __sizeof__ 方法, 如果物件由垃圾收集器管理, 則會加上額外的垃圾收集器開銷。

當然,物件記憶體佔用與 Python 版本以及作業系統版本關係密切, 本文的程式碼和測試結果都是基於 windows7 32位作業系統。

 import sys print sys.version 

2.7.2 (default, Jun 24 2011, 12:21:10) [MSC v.1500 32 bit (Intel)]

基本型別

  • 布林型

    print 'size of True: %d' % (sys.getsizeof(True)) print
    'size of False: %d' % (sys.getsizeof(False))

    輸出:

    size of True: 12 size of False: 12 
  • 整型

    # normal integer print 'size of integer: %d' % (sys.getsizeof(1)) # long print 'size of long integer: %d' % (sys.getsizeof(1L)) print 'size of big long integer: %d' % (sys.getsizeof(100000L)) 

    輸出:

    size of integer: 12x size of long integer 1L: 14 size of long integer 100000L: 16 

    可以看出整型佔用12位元組,長整型最少佔用14位元組,且佔用空間會隨著位數的增多而變大。 在2.x版本,如果整型型別的值超出sys.maxint,則自動會擴充套件為長整型。而 Python 3.0 之後,整型和長整型統一為一種型別。

  • 浮點型

    print 'size of float: %d' % (sys.getsizeof(1.0)) 

    輸出:

    size of float: 16 

    浮點型佔用16個位元組。超過一定精度後會四捨五入。參考如下程式碼:

    print 1.00000000003 print 1.000000000005 

    輸出:

    1.00000000003 1.00000000001 
  • 字串

    # size of string type print '\r\n'.join(["size of string with %d chars: %d" % (len(elem), sys.getsizeof(elem)) for elem in ["", "a", "ab"]]) # size of unicode string print '\r\n'.join(["size of unicode string with %d chars: %d" % (len(elem), sys.getsizeof(elem)) for elem in [u"", u"a", u"ab"]]) 

    輸出:

    size of string with 0 chars: 21 size of string with 1 chars: 22 size of string with 2 chars: 23 size of unicode string with 0 chars: 26 size of unicode string with 1 chars: 28 size of unicode string with 2 chars: 30 

    普通空字串佔21個位元組,每增加一個字元,多佔用1個位元組。Unicode字串最少佔用26個位元組,每增加一個字元,多佔用2個位元組。

集合型別

  • 列表

    # size of list type print '\r\n'.join(["size of list with %d elements: %d" % (len(elem), sys.getsizeof(elem)) for elem in [[], [0], [0,2], [0,1,2]]]) 

    輸出:

    size of list with 0 elements: 36 size of list with 1 elements: 40 size of list with 2 elements: 44 size of list with 3 elements: 48 

    可見列表最少佔用36個位元組,每增加一個元素,增加4個位元組。但要注意,sys.getsizeof 函式並不計算容器型別的元素大小。比如:

    print 'size of list with 3 integers %d' % (sys.getsizeof([0,1,2])) print 'size of list with 3 strings %d' % (sys.getsizeof(['0','1','2'])) 

    輸出:

    size of list with 3 integers 48 size of list with 3 strings 48 

    容器中儲存的應該是對元素的引用。如果要準確計算容器,可以參考recursive sizeof recipe 。使用其給出的 total_size 函式:

    print 'total size of list with 3 integers %d' % (total_size([0,1,2])) print 'total size of list with 3 strings %d' % (total_size(['0','1','2'])) 

    輸出為:

    total size of list with 3 integers 84 total size of list with 3 strings 114 

    可以看出列表的空間佔用為 基本空間 36 + (物件引用 4 + 物件大小) * 元素個數。

    另外還需注意如果宣告一個列表變數,則其會預先分配一些空間,以便新增元素時增加效率:

    li = [] for i in range(0, 101): print 'list with %d integers size: %d, total_size: %d' % (i, getsizeof(li), total_size(li)) li.append(i) 
  • 元組

    基本與列表類似,但其最少佔用為28個位元組。

  • 字典

    1. 字典最小擁有8個條目的空間(PyDict_MINSIZE);
    2. 條目數小於50,000時,每次增長4倍;
    3. 條目數大於50,000時,每次增長2倍;
    4. 鍵的hash值快取在字典中,字典調整大小後不會重新計算;

    每接近2/3時,字典會調整大小。

    其中一個回答的留言也很有意思: Python如此依賴字典且字典廣泛地影響這門語言的效能, 我敢打賭他們的實現很難超越。

    暫時寫這些,今後有時間會進一步研究字典的實現。