Garbage collector strategy

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Drautzburg

    Garbage collector strategy

    Just for curiosity: does python use a mark-and-sweep garbage collector
    or simple reference counting? In the latter case it would not garbage
    collect circular references, right ?

    For mark-and-sweep, I assume there must be a toplevel Object from
    which all other objects can be accessed or they will be GCed (called
    "Smalltalk" in Smalltalk). Is there such a thing?
  • Stephen Kellett

    #2
    Re: Garbage collector strategy

    In message <87brcmk76f.fsf @web.de>, Martin Drautzburg
    <martin.drautzb urg@web.de> writes[color=blue]
    >Just for curiosity: does python use a mark-and-sweep garbage collector
    >or simple reference counting? In the latter case it would not garbage
    >collect circular references, right ?[/color]

    gcmodule.c in the python sources shows the implementation, plus the code
    for this is well documented.

    Stephen
    --
    Stephen Kellett
    Object Media Limited http://www.objmedia.demon.co.uk
    RSI Information: http://www.objmedia.demon.co.uk/rsi.html

    Comment

    • Fredrik Lundh

      #3
      Re: Garbage collector strategy

      Martin Drautzburg wrote:
      [color=blue]
      > Just for curiosity: does python use a mark-and-sweep garbage collector
      > or simple reference counting?[/color]

      python the language doesn't specify this, but I assume you meant the CPython
      interpreter.

      both, sort of: it uses reference counting, and a separate "cycle breaking" collector
      that runs when needed. but unlike an ordinary m&s-collector, python's collector
      looks for unreachable objects, not reachable objects.
      [color=blue]
      > For mark-and-sweep, I assume there must be a toplevel Object from
      > which all other objects can be accessed or they will be GCed (called
      > "Smalltalk" in Smalltalk). Is there such a thing?[/color]

      nope. instead, the allocator keeps track of container objects that support the GC
      protocol. non-containers, such as integers and strings, are not tracked. for an
      overview, see:



      for the details, see Modules/gcmodule.c.

      other implementations are free to use other GC strategies. Jython, for example, uses
      the Java GC to handle Jython garbage.

      </F>



      Comment

      • Martin v. Löwis

        #4
        Re: Garbage collector strategy

        Martin Drautzburg wrote:[color=blue]
        > Just for curiosity: does python use a mark-and-sweep garbage collector
        > or simple reference counting?[/color]

        I can't resist: Yes, it does.
        [color=blue]
        > In the latter case it would not garbage
        > collect circular references, right ?[/color]

        Python uses refcounting for all objects, and a generational collector
        for container objects.
        [color=blue]
        > For mark-and-sweep, I assume there must be a toplevel Object from
        > which all other objects can be accessed or they will be GCed (called
        > "Smalltalk" in Smalltalk). Is there such a thing?[/color]

        No. There are no gc roots. Instead, there are global lists of all
        container objects, sorted by generation. Garbage is what is not
        referenced from outside these lists, everything else must be rooted
        somewhere (either in a true root, a local variable of some stack
        frame, or an older generation)

        Regards,
        Martin

        Comment

        Working...