obj = something() bytes_used = sizeof(obj) (显然没有内置函数sizeof ... 等等,让我检查......不,不是内置的) 我已经阅读了gc和pdb的文档而且没有任何内容 给我做这样的事情。 - 史蒂文。 我想调查一下 Python对象使用了多少内存。我的动机是98%纯粹的知识分子好奇心和2%的优化。 我相信这是PySizer项目的目的(其中一个) ; Google Summer of Code项目,最近在这个小组宣布了 (我确定任何搜索引擎都能指引你,无论如何)。 我没有检查过,因为我的目的不同 - 我的是 根本不是与Python相关的泄漏,只是一个在C代码中泄漏(其中恰巧是Python扩展模块)。 Alex 如果您正在使用Unix,可以使用getrusage(2)吗? 我的gentoo上有零amd64盒子。不知道为什么。我想也许它是b 是Python,但是C给出了相同的结果。 另一种可能性是调用sbrk(0),它应该返回 堆。然后,您可以返回此值并进行检查。它需要一个很小的C模块,但它应该很容易并适用于大多数unix。您可以通过将它与id(0)进行比较来确定方向堆的增长,这应该在解释器生命的早期就已经分配了 。 我意识到这并不完美,因为内存变得支离破碎,但可能会工作。从2.3及以后使用pymalloc,碎片可能不是很多问题。由于内存分配在一个大块中,然后必须发布为 。 这些技术可能适用于Windows,但有一些注意事项。如果您对Windows感兴趣,请参阅: http://msdn.microsoft.com/library/de...l/UCMGch09.asp 但是不能想到任何万无一失的事。 HTH, n Having fixed a memory leak (not the leak of a Python reference, someother stuff I wasn''t properly freeing in certain cases) in a C-codedextension I maintain, I need a way to test that the leak is indeedfixed. Being in a hurry, I originally used a q&d hack...:if sys.platform in (''linux2'', ''darwin''):def _memsize():""" this function tries to return a measurement of how much memorythis process is consuming, in some arbitrary unit (if it doesn''tmanage to, it returns 0)."""gc.collect()try:x = int(os.popen(''ps -p %d -o vsz|tail -1'' % os.getpid()).read())except:x = 0return xelse:def _memsize():return 0Having a _memsize() function available, the test then does:before = _memsize()# a lot of repeated executions of code that should not consume# any net memory, but used to when the leak was thereafter = _memsize()and checks that after==before.However, that _memsize is just too much of a hack, and I really want toclean it up. It''s also not cross-platform enough. Besides, I got a bugreport from a user on a Linux platform different from those I had testedmyself, and it boils down to the fact that once in a while on hismachine it turns our that after is before+4 (for any large number ofrepetitions of the code in the above comment) -- I''m not sure what theunit of measure is supposed to be (maybe blocks of 512 byte, with a pagesize of 2048? whatever...), but clearly an extra page is getting usedsomewhere.So, I thought I''d turn to the "wisdom of crowds"... how would YOU guysgo about adding to your automated regression tests one that checks thata certain memory leak has not recurred, as cross-platform as feasible?In particular, how would you code _memsize() "cross-platformly"? (I caneasily use C rather than Python if needed, adding it as an auxiliaryfunction for testing purposes to my existing extension).TIA,Alex 解决方案I would like to investigate how much memory is used byPython objects. My motive is 98% pure intellectualcuriosity and 2% optimization.I wonder whether I can do something like this:obj = something()bytes_used = sizeof(obj)(obviously there is no built-in function sizeof...wait, let me check... nope, not a built-in)I''ve read the docs for gc and pdb and nothing standsout to me as doing anything like this.--Steven. I would like to investigate how much memory is used by Python objects. My motive is 98% pure intellectual curiosity and 2% optimization.I believe that''s the purpose of the PySizer project (one of the "GoogleSummer of Code" projects), which was recently announced on this group(I''m sure any search engine will be able to direct you to it, anyway).I have not checked it out, because my purpose is different -- mine isnot a Python-related leak at all, just a leak within C code (whichhappens coincidentally to be a Python extension module).AlexIf you are doing Unix, can you use getrusage(2)?I get zeroes on my gentoo amd64 box. Not sure why. I thought maybe itwas Python, but C gives the same results.Another possibiity is to call sbrk(0) which should return the top ofthe heap. You could then return this value and check it. It requiresa tiny C module, but should be easy and work on most unixes. You candetermine direction heap grows by comparing it with id(0) which shouldhave been allocated early in the interpreters life.I realize this isn''t perfect as memory becomes fragmented, but mightwork. Since 2.3 and beyond use pymalloc, fragmentation may not be muchof an issue. As memory is allocated in a big hunk, then doled out asnecessary.These techniques could apply to Windows with some caveats. If you areinterested in Windows, see: http://msdn.microsoft.com/library/de...l/UCMGch09.aspCan''t think of anything fool-proof though.HTH,n 这篇关于发现此进程当前内存使用情况的最佳方式,跨平台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-19 14:44
查看更多