Python论坛  - 讨论区

标题:[python-chinese] 如何测量一个变量或者常量在内存中的大小

2004年08月10日 星期二 16:38

gentoo.cn gentoo.cn at 126.com
Tue Aug 10 16:38:23 HKT 2004

一般来说,使用python, java这样有gc的程序时不用关心内存分配(memory killer)
但是,有的应用场合可能你需要关心object的大小
比如:类似于Zope这样的Object容器里的Cache, 太大的Object不要放到Cache里来

抑或,有个叫pythonOS的东东里可能也需要哦。

limodou wrote:

>Zoom.Quiet,您好!
>
>	也不至于要细到要知道内存大小。
>
>======= 2004-08-10 15:54:28 您在来信中写道:=======
>
>  
>
>>Hollo limodou:
>>
>> 不能说不关心哪?!
>>Python 在嵌入设置中也有移植的!性能的追求是一切语言不能忽略的是也乎,不过 Python 以另外的方式来完成,当然也有提高的余地,有忐者可以开始探险哪!?
>>
>>
>>/******** [2004-08-10]15:52:50 ; limodou wrote:
>>
>>limodou> tocer,您好!
>>
>>limodou> 	因为使用Python根本不关心这个。不要总把C/C++里的概念在Python中使用,因为它们根本就不一样。
>>
>>limodou> ======= 2004-08-10 15:33:00 您在来信中写道:=======
>>
>>    
>>
>>>>没想到这么难解决
>>>>
>>>>----- Original Message ----- 
>>>>From: "gentoo.cn" <gentoo.cn at 126.com>
>>>>To: <python-chinese at lists.python.cn>
>>>>Sent: Tuesday, August 10, 2004 12:53 PM
>>>>Subject: Re: [python-chinese] 如何测量一个变量或者常量在内存中的大小
>>>>
>>>>
>>>>        
>>>>
>>>>>找到一个相关的讨论
>>>>>http://www.egenix.com/mailman-archives/egenix-users/2002-November/000156.html
>>>>>
>>>>>gentoo.cn wrote:
>>>>>
>>>>>          
>>>>>
>>>>>>我的意思是:是不是可以通过计算一个Object的Struct来猜测这个Object的大小?
>>>>>>比如c='a', string="abcd"
>>>>>>那么c的类型是1c, string的类型是4c
>>>>>>calcsize('c')=1
>>>>>>calcsize('L')=4
>>>>>>也就是说一个数值型的对象占用4Bytes内存空间,一个字符型对象占用1Bytes内存
>>>>>>空间?
>>>>>>对于string="abcd"这个对象,类型是4c所以占用4Bytes内存空间?
>>>>>>不知道Python如何考虑对齐的(也许他最小的单位是1Bytes所以都是8bit的倍数,
>>>>>>这样就对齐了?)
>>>>>>
>>>>>>大家都来讨论讨论啊。
>>>>>>
>>>>>>
>>>>>>
>>>>>>limodou wrote:
>>>>>>
>>>>>> 
>>>>>>
>>>>>>            
>>>>>>
>>>>>>>gentoo.cn,您好!
>>>>>>>
>>>>>>>
>>>>>>>              
>>>>>>>
>>>>>那个不是内存的大小吧。而是对应的struct的大小。因为在python中都是对象,而不仅仅是数据结构。不知道:你想知道内存大小有什么用?
>>>>>          
>>>>>
>>>>>>>======= 2004-08-10 11:05:42 您在来信中写道:=======
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>   
>>>>>>>
>>>>>>>              
>>>>>>>
>>>>>>>>import struct
>>>>>>>>struct.pack
>>>>>>>>struct.calcsize ?
>>>>>>>>
>>>>>>>>难道使用这个方法吗?
>>>>>>>>我也想知道有什么方法来计算对象的size
>>>>>>>>
>>>>>>>>*calcsize*( fmt)
>>>>>>>>
>>>>>>>> Return the size of the struct (and hence of the string)
>>>>>>>> corresponding to the given format. 
>>>>>>>>
>>>>>>>>Format characters have the following meaning; the conversion between C
>>>>>>>>and Python values should be obvious given their types:
>>>>>>>>
>>>>>>>>*Format* *C Type* *Python* *Notes*
>>>>>>>>x pad byte no value 
>>>>>>>>c char string of length 1 
>>>>>>>>b signed char integer 
>>>>>>>>B unsigned char integer 
>>>>>>>>h short integer 
>>>>>>>>H unsigned short integer 
>>>>>>>>i int integer 
>>>>>>>>I unsigned int long 
>>>>>>>>l long integer 
>>>>>>>>L unsigned long long 
>>>>>>>>q long long long (1)
>>>>>>>>Q unsigned long long long (1)
>>>>>>>>f float float 
>>>>>>>>d double float 
>>>>>>>>s char[] string 
>>>>>>>>p char[] string 
>>>>>>>>P void * integer 
>>>>>>>>
>>>>>>>>
>>>>>>>>tocer wrote:
>>>>>>>>
>>>>>>>>  
>>>>>>>>
>>>>>>>>     
>>>>>>>>
>>>>>>>>                
>>>>>>>>
>>>>>>>>>比如:
>>>>>>>>>a=[1,2,3]
>>>>>>>>>测试 a 在内存中的所占的字节数
>>>>>>>>>
>>>>>>>>>------------------------------------------------------------------------
>>>>>>>>>
>>>>>>>>>_______________________________________________
>>>>>>>>>python-chinese list
>>>>>>>>>python-chinese at lists.python.cn
>>>>>>>>>http://python.cn/mailman/listinfo/python-chinese
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>    
>>>>>>>>>
>>>>>>>>>       
>>>>>>>>>
>>>>>>>>>                  
>>>>>>>>>
>>>>>>>>_______________________________________________
>>>>>>>>python-chinese list
>>>>>>>>python-chinese at lists.python.cn
>>>>>>>>http://python.cn/mailman/listinfo/python-chinese
>>>>>>>>
>>>>>>>>  
>>>>>>>>
>>>>>>>>     
>>>>>>>>
>>>>>>>>                
>>>>>>>>
>>>>>>>= = = = = = = = = = = = = = = = = = = =
>>>>>>>
>>>>>>>
>>>>>>>        致
>>>>>>>礼!
>>>>>>>
>>>>>>>
>>>>>>>        limodou
>>>>>>>        chatme at 263.net
>>>>>>>          2004-08-10
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>------------------------------------------------------------------------
>>>>>>>
>>>>>>>_______________________________________________
>>>>>>>python-chinese list
>>>>>>>python-chinese at lists.python.cn
>>>>>>>http://python.cn/mailman/listinfo/python-chinese
>>>>>>>
>>>>>>>
>>>>>>>   
>>>>>>>
>>>>>>>              
>>>>>>>
>>>>>>_______________________________________________
>>>>>>python-chinese list
>>>>>>python-chinese at lists.python.cn
>>>>>>http://python.cn/mailman/listinfo/python-chinese
>>>>>>
>>>>>>
>>>>>> 
>>>>>>
>>>>>>            
>>>>>>
>>>>>_______________________________________________
>>>>>python-chinese list
>>>>>python-chinese at lists.python.cn
>>>>>http://python.cn/mailman/listinfo/python-chinese
>>>>>_______________________________________________
>>>>>          
>>>>>
>>>>python-chinese list
>>>>python-chinese at lists.python.cn
>>>>http://python.cn/mailman/listinfo/python-chinese
>>>>
>>>>        
>>>>
>>limodou> = = = = = = = = = = = = = = = = = = = =
>>			
>>
>>limodou>         致
>>limodou> 礼!
>>
>>				 
>>limodou>         limodou
>>limodou>         chatme at 263.net
>>limodou>           2004-08-10
>>
>>
>>
>>********************************************/
>>
>>-- 
>>Free as in Freedom
>>
>>Zoom.Quiet                           
>>
>>#=========================================#
>>]Time is unimportant, only life important![
>>#=========================================#
>>
>>sender is the Bat!2.12.00
>>
>>_______________________________________________
>>python-chinese list
>>python-chinese at lists.python.cn
>>http://python.cn/mailman/listinfo/python-chinese
>>
>>    
>>
>
>= = = = = = = = = = = = = = = = = = = =
>			
>
>        致
>礼!
> 
>				 
>        limodou
>        chatme at 263.net
>          2004-08-10
>
>  
>
>------------------------------------------------------------------------
>
>_______________________________________________
>python-chinese list
>python-chinese at lists.python.cn
>http://python.cn/mailman/listinfo/python-chinese
>  
>



[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2004年08月10日 星期二 16:40

hoxide hoxide_dirac at yahoo.com.cn
Tue Aug 10 16:40:49 HKT 2004

gentoo.cn,您好!

	如果把python嵌入到便携设备,比如手机之类的,可能就需要严格控制内存了.
好像有人在做这个,或者是用jython吧.我不太清楚.另外用python做deamon也很需要严格监控内存.

======= 2004-08-10 16:38:23 您在来信中写道:=======

>一般来说,使用python, java这样有gc的程序时不用关心内存分配(memory killer)
>但是,有的应用场合可能你需要关心object的大小
>比如:类似于Zope这样的Object容器里的Cache, 太大的Object不要放到Cache里来
>
>抑或,有个叫pythonOS的东东里可能也需要哦。
>
>limodou wrote:
>
>>Zoom.Quiet,您好!
>>
>>	也不至于要细到要知道内存大小。
>>
>>======= 2004-08-10 15:54:28 您在来信中写道:=======
>>
>>  
>>
>>>Hollo limodou:
>>>
>>> 不能说不关心哪?!
>>>Python 在嵌入设置中也有移植的!性能的追求是一切语言不能忽略的是也乎,不过 Python 以另外的方式来完成,当然也有提高的余地,有忐者可以开始探险哪!?
>>>
>>>
>>>/******** [2004-08-10]15:52:50 ; limodou wrote:
>>>
>>>limodou> tocer,您好!
>>>
>>>limodou> 	因为使用Python根本不关心这个。不要总把C/C++里的概念在Python中使用,因为它们根本就不一样。
>>>
>>>limodou> ======= 2004-08-10 15:33:00 您在来信中写道:=======
>>>
>>>    
>>>
>>>>>没想到这么难解决
>>>>>
>>>>>----- Original Message ----- 
>>>>>From: "gentoo.cn" <gentoo.cn at 126.com>
>>>>>To: <python-chinese at lists.python.cn>
>>>>>Sent: Tuesday, August 10, 2004 12:53 PM
>>>>>Subject: Re: [python-chinese] 如何测量一个变量或者常量在内存中的大小
>>>>>
>>>>>
>>>>>        
>>>>>
>>>>>>找到一个相关的讨论
>>>>>>http://www.egenix.com/mailman-archives/egenix-users/2002-November/000156.html
>>>>>>
>>>>>>gentoo.cn wrote:
>>>>>>
>>>>>>          
>>>>>>
>>>>>>>我的意思是:是不是可以通过计算一个Object的Struct来猜测这个Object的大小?
>>>>>>>比如c='a', string="abcd"
>>>>>>>那么c的类型是1c, string的类型是4c
>>>>>>>calcsize('c')=1
>>>>>>>calcsize('L')=4
>>>>>>>也就是说一个数值型的对象占用4Bytes内存空间,一个字符型对象占用1Bytes内存
>>>>>>>空间?
>>>>>>>对于string="abcd"这个对象,类型是4c所以占用4Bytes内存空间?
>>>>>>>不知道Python如何考虑对齐的(也许他最小的单位是1Bytes所以都是8bit的倍数,
>>>>>>>这样就对齐了?)
>>>>>>>
>>>>>>>大家都来讨论讨论啊。
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>limodou wrote:
>>>>>>>
>>>>>>> 
>>>>>>>
>>>>>>>            
>>>>>>>
>>>>>>>>gentoo.cn,您好!
>>>>>>>>
>>>>>>>>
>>>>>>>>              
>>>>>>>>
>>>>>>那个不是内存的大小吧。而是对应的struct的大小。因为在python中都是对象,而不仅仅是数据结构。不知道:你想知道内存大小有什么用?
>>>>>>          
>>>>>>
>>>>>>>>======= 2004-08-10 11:05:42 您在来信中写道:=======
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>   
>>>>>>>>
>>>>>>>>              
>>>>>>>>
>>>>>>>>>import struct
>>>>>>>>>struct.pack
>>>>>>>>>struct.calcsize ?
>>>>>>>>>
>>>>>>>>>难道使用这个方法吗?
>>>>>>>>>我也想知道有什么方法来计算对象的size
>>>>>>>>>
>>>>>>>>>*calcsize*( fmt)
>>>>>>>>>
>>>>>>>>> Return the size of the struct (and hence of the string)
>>>>>>>>> corresponding to the given format. 
>>>>>>>>>
>>>>>>>>>Format characters have the following meaning; the conversion between C
>>>>>>>>>and Python values should be obvious given their types:
>>>>>>>>>
>>>>>>>>>*Format* *C Type* *Python* *Notes*
>>>>>>>>>x pad byte no value 
>>>>>>>>>c char string of length 1 
>>>>>>>>>b signed char integer 
>>>>>>>>>B unsigned char integer 
>>>>>>>>>h short integer 
>>>>>>>>>H unsigned short integer 
>>>>>>>>>i int integer 
>>>>>>>>>I unsigned int long 
>>>>>>>>>l long integer 
>>>>>>>>>L unsigned long long 
>>>>>>>>>q long long long (1)
>>>>>>>>>Q unsigned long long long (1)
>>>>>>>>>f float float 
>>>>>>>>>d double float 
>>>>>>>>>s char[] string 
>>>>>>>>>p char[] string 
>>>>>>>>>P void * integer 
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>tocer wrote:
>>>>>>>>>
>>>>>>>>>  
>>>>>>>>>
>>>>>>>>>     
>>>>>>>>>
>>>>>>>>>                
>>>>>>>>>
>>>>>>>>>>比如:
>>>>>>>>>>a=[1,2,3]
>>>>>>>>>>测试 a 在内存中的所占的字节数
>>>>>>>>>>
>>>>>>>>>>------------------------------------------------------------------------
>>>>>>>>>>
>>>>>>>>>>_______________________________________________
>>>>>>>>>>python-chinese list
>>>>>>>>>>python-chinese at lists.python.cn
>>>>>>>>>>http://python.cn/mailman/listinfo/python-chinese
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>    
>>>>>>>>>>
>>>>>>>>>>       
>>>>>>>>>>
>>>>>>>>>>                  
>>>>>>>>>>
>>>>>>>>>_______________________________________________
>>>>>>>>>python-chinese list
>>>>>>>>>python-chinese at lists.python.cn
>>>>>>>>>http://python.cn/mailman/listinfo/python-chinese
>>>>>>>>>
>>>>>>>>>  
>>>>>>>>>
>>>>>>>>>     
>>>>>>>>>
>>>>>>>>>                
>>>>>>>>>
>>>>>>>>= = = = = = = = = = = = = = = = = = = =
>>>>>>>>
>>>>>>>>
>>>>>>>>        致
>>>>>>>>礼!
>>>>>>>>
>>>>>>>>
>>>>>>>>        limodou
>>>>>>>>        chatme at 263.net
>>>>>>>>          2004-08-10
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>------------------------------------------------------------------------
>>>>>>>>
>>>>>>>>_______________________________________________
>>>>>>>>python-chinese list
>>>>>>>>python-chinese at lists.python.cn
>>>>>>>>http://python.cn/mailman/listinfo/python-chinese
>>>>>>>>
>>>>>>>>
>>>>>>>>   
>>>>>>>>
>>>>>>>>              
>>>>>>>>
>>>>>>>_______________________________________________
>>>>>>>python-chinese list
>>>>>>>python-chinese at lists.python.cn
>>>>>>>http://python.cn/mailman/listinfo/python-chinese
>>>>>>>
>>>>>>>
>>>>>>> 
>>>>>>>
>>>>>>>            
>>>>>>>
>>>>>>_______________________________________________
>>>>>>python-chinese list
>>>>>>python-chinese at lists.python.cn
>>>>>>http://python.cn/mailman/listinfo/python-chinese
>>>>>>_______________________________________________
>>>>>>          
>>>>>>
>>>>>python-chinese list
>>>>>python-chinese at lists.python.cn
>>>>>http://python.cn/mailman/listinfo/python-chinese
>>>>>
>>>>>        
>>>>>
>>>limodou> = = = = = = = = = = = = = = = = = = = =
>>>			
>>>
>>>limodou>         致
>>>limodou> 礼!
>>>
>>>				 
>>>limodou>         limodou
>>>limodou>         chatme at 263.net
>>>limodou>           2004-08-10
>>>
>>>
>>>
>>>********************************************/
>>>
>>>-- 
>>>Free as in Freedom
>>>
>>>Zoom.Quiet                           
>>>
>>>#=========================================#
>>>]Time is unimportant, only life important![
>>>#=========================================#
>>>
>>>sender is the Bat!2.12.00
>>>
>>>_______________________________________________
>>>python-chinese list
>>>python-chinese at lists.python.cn
>>>http://python.cn/mailman/listinfo/python-chinese
>>>
>>>    
>>>
>>
>>= = = = = = = = = = = = = = = = = = = =
>>			
>>
>>        致
>>礼!
>> 
>>				 
>>        limodou
>>        chatme at 263.net
>>          2004-08-10
>>
>>  
>>
>>------------------------------------------------------------------------
>>
>>_______________________________________________
>>python-chinese list
>>python-chinese at lists.python.cn
>>http://python.cn/mailman/listinfo/python-chinese
>>  
>>
>
>_______________________________________________
>python-chinese list
>python-chinese at lists.python.cn
>http://python.cn/mailman/listinfo/python-chinese

= = = = = = = = = = = = = = = = = = = =
			

        致
礼!
 
				 
        hoxide
        hoxide_dirac at yahoo.com.cn
          2004-08-10


[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号