[Python] Why does sys.getrefcount() returns a huge number in circular reference?

Discussão em 'Python' iniciado por Stack, Fevereiro 6, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to understand why reference counting technique of garbage collection cannot handle circular references. So I used this code to verify that the reference count doesn't go to zero.

    import sys

    # Function to count references to an object in the program
    def count_references(obj):
    return sys.getrefcount(obj) - 1


    # Example objects
    l = []
    l.append(l)

    l = None

    # Count references before deleting any reference
    print("References to 'l' before deletion:", count_references(l))


    this was the output

    References to 'l' before deletion: 4413


    I would be grateful if someone could explain the reference between these large numbers.

    Thanks in advance.

    Continue reading...

Compartilhe esta Página