由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - python gc question
相关主题
A problem on string parsing (using either grep or perl)gvim diff question
如果python command line positional arguments 里有些是运算,那这种argument 该怎么处理?how to assign new value to loop variables?
问个PYTHON烂问题哪有关于python调用c的tutorial啊
Python: 有一个混合了子list和string的list, 如何判断元素是list还是string?我知道为啥ruby这些framework没有用v8引擎原理的原因了
阅读scala中有一点我不同意公孙大神说的
How to concatenate two .tar.gz filespython Global Lock
大牛 in Perl and SED?Python程序员请进
[合集] 如何能让这个程序快一点呢?太慢了求教python问题,在线等 (转载)
相关话题的讨论汇总
话题: r0话题: memory话题: r1话题: python话题: free
进入Programming版参与讨论
1 (共1页)
w****i
发帖数: 964
1
Could someone tell me how to free memory of huge strings in python?
I have two huge strings R0 and R1 in python, why "R0 = R1" doesn't free the
memory of previous R0?
for example, this frees the memory of R0:
>>> R0 = open(file1).readlines()
>>> del R0
But the following doesn't free memory
>>> R0 = open(file1).readlines()
>>> R1 = open(file2).readlines()
>>> R0 = R1 # this doesn't free memory of file1.
>>> del R0
>>> del R1 # this doesn't free the memory either
what is the best way to free
r****t
发帖数: 10904
2
R1 and R2 should be big lists, not strings, aren't they?
del obj 只是让 reference count minus 1,if reference count == 0, interpreter
will get around to call obj.__del__() and destroy the object. Even after the
obj is destroyed, 虽然它不存在了,但是它占的内存也不一定还给 OS 了。
big string obj > 256k uses malloc,这些内存 python interpreter 私藏起来 in case it will need that space again, instead of giving back to OS. This is not leak, because a next big string can reuse this memory again. Python interpreter keep space for 80 delet
w****i
发帖数: 964
3
right they should be lists. So even these lists take a huge ammount of
memory, they will be kept in memory for potential future reuse? This seems
not optimal since the probability that a huge list (consist of huge strings)
will be reused should be very low. In my case they actually eat up huge
ammount of memory. Is there any way to explicitly free the memory of these
lists?
thanks
r****t
发帖数: 10904
4
It is the strings that're referenced by the list that are taking up the
memory, not the lists themselves.
我给你 copy 一段:http://effbot.org/pyfaq/why-doesnt-python-release-the-memory-when-i-delete-a-large-object.htm
“Exactly if and when Python’s allocator returns memory to the C runtime,
and when the C runtime returns memory to the operating system, depends on a
lot of parameters, including Python and library versions, your application’
s object allocation patterns, and so on. For example, CPython 2

【在 w****i 的大作中提到】
: right they should be lists. So even these lists take a huge ammount of
: memory, they will be kept in memory for potential future reuse? This seems
: not optimal since the probability that a huge list (consist of huge strings)
: will be reused should be very low. In my case they actually eat up huge
: ammount of memory. Is there any way to explicitly free the memory of these
: lists?
: thanks

1 (共1页)
进入Programming版参与讨论
相关主题
求教python问题,在线等 (转载)阅读scala中
大家工作中一般如何用python 调用C或C++How to concatenate two .tar.gz files
cyphon,cpython,numba,julia,pypy,为什么都不是主流大牛 in Perl and SED?
最近在看《python源码剖析》[合集] 如何能让这个程序快一点呢?太慢了
A problem on string parsing (using either grep or perl)gvim diff question
如果python command line positional arguments 里有些是运算,那这种argument 该怎么处理?how to assign new value to loop variables?
问个PYTHON烂问题哪有关于python调用c的tutorial啊
Python: 有一个混合了子list和string的list, 如何判断元素是list还是string?我知道为啥ruby这些framework没有用v8引擎原理的原因了
相关话题的讨论汇总
话题: r0话题: memory话题: r1话题: python话题: free