Python 對象內存占用


Python 對象內存占用

(github地址)

之前寫的腳本中,需要估計程序的內存占用,所以簡單研究下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個字節。

  • 字典

    字典的情況相對復雜很多,具體當然要參考代碼 dictobject.c, 另外 NOTES ON OPTIMIZING DICTIONARIES 非常值得仔細閱讀。

    基本情況可以參考[stackoverflow] 的問題 Python's underlying hash data structure for dictionaries 中的一些回答:

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

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

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

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM