memory management - How long are immutable objects stored in Python? - Stack Overflow

As far as I understand Python‘s memory management, when initialising e.g. some floatk = 1.0Python crea

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
  • 1 @OldBoy I thought Python released memory immediately in almost all cases, and the garbage collector really only was required in unusual situations? – Mark Ransom Commented Mar 25 at 14:21
  • @MarkRansom Not according to (my reading of) the documentation. – OldBoy Commented Mar 25 at 14:37
  • "What is the conceptual advantage of such memory management; meaning why not release the memory immediately?" youtube/watch?v=c32zXYAK7CI TLDR: It's faster – jabaa Commented Mar 25 at 14:46
  • 1 @OldBoy basically correct, but note, the 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
  • @jabaa but python does release the memory immediately (back to the privately managed heap) in most cases – juanpa.arrivillaga Commented Mar 25 at 15:26
 |  Show 4 more comments

1 Answer 1

Reset to default 1

What 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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信