最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin4浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论