python / python/cpython

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

Open
#122,567 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

performance type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source file or test is named. Start by running the provided reproducer on CPython 3.11 and trace the behavior of gc.collect(0), automatic collection thresholds, and generation-1 growth. Done means confirming the reported interaction and adding an appropriate regression test and fix if the behavior is verified.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.