Free Listsの GC に関する Python ドキュメント:
特定の実装、特に int と float が原因で、一部のフリー リストのすべてのアイテムが解放されるわけではありません。
したがって、ジェネレーターを介して無数の無限の整数を割り当てると、すべてのメモリが (メモリ エラーを受け取るまで) 消費されますが、上記の定義に従ってすべてのアイテムが解放されるわけではありません。
しかし、その記憶は永久に失われていますか?いいえ。環境は、コードで再利用できるように保持します。ガベージ コレクションは「効率的」であるからといって、オブジェクトがスコープを離れた瞬間に回収されるわけではありません。また、「使用したばかりのメモリを保持しましょう。このばかげたコードが再び使用する可能性がある」という意味もあります。
または、Effbotが教えてくれるように:
特定のアロケーターに返されたメモリは、システムに返されなくても、そのアロケーターによって再利用されます。
GC コレクションを強制することはでき ますが、その理由がわからず、強制する非常に正当な理由がない限り、実際にはパフォーマンスが低下する可能性があります。
gc.collect([generation])
With no arguments, run a full collection. The optional argument generation may be an integer specifying which generation to collect (from 0 to 2). A ValueError is raised if the generation number is invalid. The number of unreachable objects found is returned.
Changed in version 2.5: The optional generation argument was added.
Changed in version 2.6: The free lists maintained for a number of built-in types are cleared whenever a full collection or collection of the highest generation (2) is run. Not all items in some free lists may be freed due to the particular implementation, in particular int and float.