设为首页 收藏本站
查看: 838|回复: 0

[经验分享] python 对象内存分析

[复制链接]

尚未签到

发表于 2017-4-26 12:15:52 | 显示全部楼层 |阅读模式
python对象内存分析

  一、python内建对象
  python内建对象占用内存的情况又分为定长对象与非定长对象(变长)
  1.1 定长对象,对象在内存中所占大小不会变化的对象
  包括int,float,long,bool,complex和dict
  测试程序如下:

#!/usr/bin/env python
#-*- coding:utf-8 -*-
import sys
print "value\t\ttype\t\tmemsize"
#int test
alist=[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 test
blist=[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 test
clist=[0l,1l,2l,-1111l,45445l]
for i in clist:
print "%d\t\t%s\t\t%s"%(i,type(i),sys.getsizeof(i))
#bool test
dlist=[True,False]
for i in dlist:
print "%s\t\t%s\t\t%s"%(i,type(i),sys.getsizeof(i))
#complex test
elist=[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 test
flist=[{},{'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))

  运行结果如下:

valuetypememsize
0<type 'int'>24
1<type 'int'>24
10<type 'int'>24
-1<type 'int'>24
-444<type 'int'>24
12313<type 'int'>24
0.000000<type 'float'>24
1.000000<type 'float'>24
111.100000<type 'float'>24
2323.220000<type 'float'>24
-1.100000<type 'float'>24
0<type 'long'>24
1<type 'long'>28
2<type 'long'>28
-1111<type 'long'>28
45445<type 'long'>28
True<type 'bool'>24
False<type 'bool'>24
0j <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 test
print "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 test
alist=[[],['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 test
print "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-lengthtypememsize
0<type 'str'>37
1<type 'str'>38
2<type 'str'>39
6<type 'str'>43
4<type 'str'>41
4<type 'str'>41
4<type 'str'>41
list-lengthtypememsize
0<type 'list'>72
2<type 'list'>88
2<type 'list'>88
3<type 'list'>96
4<type 'list'>104
tuple-lentypememsize
0<type 'tuple'>56
1<type 'tuple'>64
2<type 'tuple'>72
3<type 'tuple'>80
4<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)
...
872
872
872
872
872
872
872
872
872
872
  类型对象也是定长的为872
  基类对象object所占内存也为872
  二、自建对象
  测试程序:

#!/usr/bin/env python
import sys
class A:
def __init__(self):
self.value=2
def test(self):
print self.value
class B(object):
def test(self):
print "test"
class C(float):
def __init__(self):
self.value=1
def test(self):
print self.value
class D(object):
pass
class E(A):
pass
print "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'>104
A():<type 'instance'>72
B  :<type 'type'>904
B():<class '__main__.B'>64
C  :<type 'type'>904
C():<class '__main__.C'>72
D  :<type 'type'>904
D():<class '__main__.D'>64
E  :<type 'classobj'>104
E():<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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-369545-1-1.html 上篇帖子: Python代码规范与pylint 下篇帖子: Python中的默认参数值
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表