python对象内存分析

来源:互联网 发布:淘宝网卖什么比较好 编辑:程序博客网 时间:2024/06/05 11:12

python对象内存分析

一、python内建对象

python内建对象占用内存的情况又分为定长对象与非定长对象(变长)

1.1 定长对象,对象在内存中所占大小不会变化的对象

包括int,float,long,bool,complex和dict

测试程序如下:

#!/usr/bin/env python#-*- coding:utf-8 -*-import sysprint "value\t\ttype\t\tmemsize"#int testalist=[0,1,10,-1,-444,12313]for i in alist:        print "%d\t\t%s\t\t%s"%(i,type(i),sys.getsizeof(i))#float testblist=[0.0,1.0,111.1,2323.22,-1.1]for i in blist:        print "%f\t\t%s\t\t%s"%(i,type(i),sys.getsizeof(i))#long testclist=[0l,1l,2l,-1111l,45445l]for i in clist:        print "%d\t\t%s\t\t%s"%(i,type(i),sys.getsizeof(i))#bool testdlist=[True,False]for i in dlist:        print "%s\t\t%s\t\t%s"%(i,type(i),sys.getsizeof(i))#complex testelist=[0j,1+0j,1+1j,1000-23j,-100+5j]for i in elist:        print i,"\t\t%s\t\t%s"%(type(i),sys.getsizeof(i))#dict testflist=[{},{'a':'b'},{'a':'b','c':1},{'a':'b','c':1,'d':'你好'}]for i in flist:        print i,"\t\t%s\t\t%s"%(type(i),sys.getsizeof(i))

?运行结果如下:

valuetypememsize0<type 'int'>241<type 'int'>2410<type 'int'>24-1<type 'int'>24-444<type 'int'>2412313<type 'int'>240.000000<type 'float'>241.000000<type 'float'>24111.100000<type 'float'>242323.220000<type 'float'>24-1.100000<type 'float'>240<type 'long'>241<type 'long'>282<type 'long'>28-1111<type 'long'>2845445<type 'long'>28True<type 'bool'>24False<type 'bool'>240j <type 'complex'>32(1+0j) <type 'complex'>32(1+1j) <type 'complex'>32(1000-23j) <type 'complex'>32(-100+5j) <type 'complex'>32{} <type 'dict'>280{'a': 'b'} <type 'dict'>280{'a': 'b', 'c': 1} <type 'dict'>280{'a': 'b', 'c': 1, 'd': '\xe4\xbd\xa0\xe5\xa5\xbd'} <type 'dict'>280

?有运行结果可以看出各个定长对象所占的内存:

int和float:24

long:这个有点特殊,对于0l,python识别为long type,但是所占内存是24,除了0l所占内存为24以外,其他的都为28

complex(复数):32

dict(字典):280

1.2 变成对象,会随着对象变化所占用的内存会变化

包括:list,tuple,str

测试代码:

#/usr/bin/env python#-*- coding: utf-8 -*-import sys#str testprint "str-length\ttype\tmemsize"ua='你好'ga=ua.decode('utf-8').encode('gbk')ba=ua.decode('utf-8').encode('big5')ga1=ua.decode('utf-8').encode('gb2312')alist=['','a','ab',ua,ga,ba,ga1]for s in alist:        print "%d\t%s\t%s"%(len(s),type(s),sys.getsizeof(s))print "list-length\ttype\tmemsize"#list testalist=[[],['a','b'],['abc','你好'],[11,12,'eee']]for li in alist:        print "%d\t%s\t%s"%(len(li),type(li),sys.getsizeof(li))print "%d\t%s\t%s"%(len(alist),type(alist),sys.getsizeof(alist))#tuple testprint "tuple-len\ttype\tmemsize"alist=((),('a',),('abc','你好'),(11,12,'eeee'))for tp in alist:        print "%d\t%s\t%s"%(len(tp),type(tp),sys.getsizeof(tp))print "%d\t%s\t%s"%(len(alist),type(alist),sys.getsizeof(alist))

?结果:

str-lengthtypememsize0<type 'str'>371<type 'str'>382<type 'str'>396<type 'str'>434<type 'str'>414<type 'str'>414<type 'str'>41list-lengthtypememsize0<type 'list'>722<type 'list'>882<type 'list'>883<type 'list'>964<type 'list'>104tuple-lentypememsize0<type 'tuple'>561<type 'tuple'>642<type 'tuple'>723<type 'tuple'>804<type 'tuple'>88

分析结果可知:

str:空str所占内存为37,若str长度每加1,则内存所占大小相应加1

list:空列表所占内存为72,长度每增加1,则所占内存加8

tuple:空元组所占内存为56,长度每加1,所占了内存加8

空字符串为什么是37,而不是36或38,因为这里介绍所有的对像内存都为偶数,python内部维护字符串的机制和C中维护字符串的机制是一样的,即在末尾加'\0',这个占了1个字节,所以内存大小表现为36+1=37

补充:

python中还有一个比较特殊的对象,就是类型对像

>>> tlist=(int,float,long,str,complex,dict,list,tuple,bool,type)>>> for i in tlist:...     print sys.getsizeof(i)... 872872872872872872872872872872

类型对象也是定长的为872

基类对象object所占内存也为872

二、自建对象

测试程序:

#!/usr/bin/env pythonimport sysclass A:        def __init__(self):                self.value=2        def test(self):                print self.valueclass B(object):        def test(self):                print "test"class C(float):        def __init__(self):                self.value=1        def test(self):                print self.valueclass D(object):        passclass E(A):        passprint "A  :%s\t%s"%(type(A),sys.getsizeof(A))print "A():%s\t%s"%(type(A()),sys.getsizeof(A()))print "B  :%s\t%s"%(type(B),sys.getsizeof(B))print "B():%s\t%s"%(type(B()),sys.getsizeof(B()))print "C  :%s\t%s"%(type(C),sys.getsizeof(C))print "C():%s\t%s"%(type(C()),sys.getsizeof(C()))print "D  :%s\t%s"%(type(D),sys.getsizeof(D))print "D():%s\t%s"%(type(D()),sys.getsizeof(D()))print "E  :%s\t%s"%(type(E),sys.getsizeof(E))print "E():%s\t%s"%(type(E()),sys.getsizeof(E()))

结果:

A  :<type 'classobj'>104A():<type 'instance'>72B  :<type 'type'>904B():<class '__main__.B'>64C  :<type 'type'>904C():<class '__main__.C'>72D  :<type 'type'>904D():<class '__main__.D'>64E  :<type 'classobj'>104E():<type 'instance'>72

?有结果可以看出:

A和E对象没有继承类型对象,未申明基类的情况下,类型python解释为’classobj',所占内存为104,实例化后类型为instance 内存为72

BD对象都是继承自基类object,类型为type,所占内存为904,实例化后类型为class,所占内存为64

C对象继承自类型对象 float,类型为type,所占内存为904,实例化后类型为class,所占内存为72

PS:object是所有对象的基类,python中所有对象都继承自object

1 0
原创粉丝点击