As far as I understand Python‘s memory management, when initialising e.g. some float
k = 1.0
Python creates an immutable object. Even when redefining the name „k“
k = 1.1
the memory space is not released, but rather a second one allocated for the „new k“.
I was wondering, how long this immutable object is saved. Since there exists no name that references to it, it could as be well deleted immediately. But I have read that this is not the case. Am I right in assuming that with this behaviour, some „simple“ routine like
from datetime import datetime
while True:
k = datetime.now()
would eventually crash as all memory has been littered with unnamed immutable objects. What is the conceptual advantage of such memory management; meaning why not release the memory immediately?
As far as I understand Python‘s memory management, when initialising e.g. some float
k = 1.0
Python creates an immutable object. Even when redefining the name „k“
k = 1.1
the memory space is not released, but rather a second one allocated for the „new k“.
I was wondering, how long this immutable object is saved. Since there exists no name that references to it, it could as be well deleted immediately. But I have read that this is not the case. Am I right in assuming that with this behaviour, some „simple“ routine like
from datetime import datetime
while True:
k = datetime.now()
would eventually crash as all memory has been littered with unnamed immutable objects. What is the conceptual advantage of such memory management; meaning why not release the memory immediately?
Share Improve this question asked Mar 25 at 13:57 OctaviusOctavius 1091 bronze badge 9 | Show 4 more comments1 Answer
Reset to default 1What is the conceptual advantage of such memory management; meaning why not release the memory immediately?
The purpose of the garbage collector is not to free memory. The purpose of the garbage collector is to simulate a computer with infinite memory. It achieves that by freeing memory that is unreachable, at a time that is convenient.
If the program can run to completion without exceeding available memory, then the GC need not run. If a program can run for a while, and then spend a small amount of time copying the live objects somewhere else, then it can deallocate a huge amount of memory in one go. This can be significantly faster than freeing memory immediately.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744191989a4562475.html
gc
garbage collector is an auxiliary garbage collector, it handles reference cycles that become unreachable, in CPython, objects are immediately reclaimed when their reference count reaches zero. This is an implementation detail, of course. In the docs, this is kinda elided, but it notes "This module provides an interface to the optional garbage collector" and "Since the collector supplements the reference counting already used in Python, you can disable the collector if you are sure your program does not create reference cycles." – juanpa.arrivillaga Commented Mar 25 at 15:24