牛骨文教育服务平台(让学习变的简单)
博文笔记

python对象内存分析

创建时间:2014-03-24 投稿人: 浏览次数:4853

python对象内存分析

一、python内建对象

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

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

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

测试程序如下:

#!/usr/bin/env python
#-*- coding:utf-8 -*-
import sys
print "value		type		memsize"
#int test
alist=[0,1,10,-1,-444,12313]
for i in alist:
        print "%d		%s		%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		%s		%s"%(i,type(i),sys.getsizeof(i))
#long test
clist=[0l,1l,2l,-1111l,45445l]
for i in clist:
        print "%d		%s		%s"%(i,type(i),sys.getsizeof(i))
#bool test
dlist=[True,False]
for i in dlist:
        print "%s		%s		%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,"		%s		%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,"		%s		%s"%(type(i),sys.getsizeof(i))

?运行结果如下:

value		type		memsize
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": "xe4xbdxa0xe5xa5xbd"} 		<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	type	memsize"
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	%s	%s"%(len(s),type(s),sys.getsizeof(s))

print "list-length	type	memsize"
#list test
alist=[[],["a","b"],["abc","你好"],[11,12,"eee"]]
for li in alist:
        print "%d	%s	%s"%(len(li),type(li),sys.getsizeof(li))

print "%d	%s	%s"%(len(alist),type(alist),sys.getsizeof(alist))

#tuple test
print "tuple-len	type	memsize"
alist=((),("a",),("abc","你好"),(11,12,"eeee"))
for tp in alist:
        print "%d	%s	%s"%(len(tp),type(tp),sys.getsizeof(tp))

print "%d	%s	%s"%(len(alist),type(alist),sys.getsizeof(alist))

?结果:

str-length	type	memsize
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-length	type	memsize
0	<type "list">	72
2	<type "list">	88
2	<type "list">	88
3	<type "list">	96
4	<type "list">	104
tuple-len	type	memsize
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中维护字符串的机制是一样的,即在末尾加"",这个占了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	%s"%(type(A),sys.getsizeof(A))
print "A():%s	%s"%(type(A()),sys.getsizeof(A()))
print "B  :%s	%s"%(type(B),sys.getsizeof(B))
print "B():%s	%s"%(type(B()),sys.getsizeof(B()))
print "C  :%s	%s"%(type(C),sys.getsizeof(C))
print "C():%s	%s"%(type(C()),sys.getsizeof(C()))
print "D  :%s	%s"%(type(D),sys.getsizeof(D))
print "D():%s	%s"%(type(D()),sys.getsizeof(D()))
print "E  :%s	%s"%(type(E),sys.getsizeof(E))
print "E():%s	%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

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。