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
|
|