python / python/cpython

GC - Possible to indefinitely prevent collection of generation-1 objects by calling gc.collect(0) often

Ouverte
#122,567 1 commentaire 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

performance type-bug
Langage dominant
Python
Étoiles
77.2k
Forks
35.9k
Métriques de merge des PR
Métriques de PR en attente

Description

Bug report

Bug description:

The example below will die from memory exhaustion, despite nearly all objects being eligible for collection and despite garbage collection not being disabled. See inline comments for an explanation of what I think is happening:

import gc
from typing import Any

class Temp:
  def __init__(self):
    # Cyclic reference to prevent deletion by ref count dropping to 0.
    self._x = self
    # Allocate a bunch of memory so that the process will die if
    # collection doesn't happen.
    self.memory = bytearray(1024*1024*512)

# Set some low thresholds so that collection should occur often.
gc.set_threshold(100, 3, 3)

while True:
  ref = Temp()
  # Promote the new Temp() object to generation-1. I think this is also resetting
  # the GCs view of how many objects were allocated at the "last collection". If
  # that's true, it will effectively stop the GC from ever being invoked
  # automatically, even though we're accumulating more and more objects,
  # because `threshold0` will never be exceeded.
  gc.collect(0)
  del ref
  # Temp() is now in generation-1 and eligible for collection, but gc.collect(1)
  # is never triggered, because there are no automatically invoked GCs at all.
  # Here we log the number of generation-1 objects, which continues to increase
  # until the process dies.
  print("Number of generation 1 objects: ", len(gc.get_objects(generation=1)))

Commenting out only the gc.collect(0) allows the Temp objects to be collected, and the process to run indefinitely. The fact that removing an explicit GC actually enables more GC to happen seems broken.

If what I think is happening is what's happening, then perhaps manually invoked GCs should not reset the GC's view on how many objects were allocated at the "last collection", since that would be what's effectively disabling the automatically triggered GCs (and hence the GCs at higher generations) from running.

CPython versions tested on:

3.11

Operating systems tested on:

macOS

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Aucun fichier source ni test n’est nommé. Commencez par exécuter le reproducteur fourni sur CPython 3.11 et suivez le comportement de gc.collect(0), des seuils de collecte automatique et de la croissance de la génération 1. Le travail est considéré comme terminé lorsque l’interaction signalée est confirmée et que, si le comportement est vérifié, un test de régression approprié ainsi qu’un correctif sont ajoutés.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
python
Domaine
backend
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.