Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #89822

Re: Inner workings of this Python feature: Can a Python data structure reference itself?

Date 2015-05-02 20:57 -0500
From Tim Chase <python.list@tim.thechases.com>
Subject Re: Inner workings of this Python feature: Can a Python data structure reference itself?
References <387ac520-9da3-411a-a3b8-b326e1e8a2c0@googlegroups.com> <mailman.26.1430597813.12865.python-list@python.org> <87a8xmn0yp.fsf@Equus.decebal.nl>
Newsgroups comp.lang.python
Message-ID <mailman.41.1430619381.12865.python-list@python.org> (permalink)

Show all headers | View raw


On 2015-05-02 23:06, Cecil Westerhof wrote:
> Op Saturday 2 May 2015 22:17 CEST schreef Tim Chase:
>> This creates a cycle, then makes it unreachable, but the list is
>> still referenced by itself, so the reference count never drops to
>> zero (where it would get GC'd), and thus that item lingers around
>> in memory.
> 
> Maybe look at Java? If my memory is correct, the JVM gc reclaims
> those kind of things also.

The two most common ways of doing garbage collection are
reference-counting and mark-and-sweep. They're not mutually
exclusive, and can be combined, but each has advantages to counter
disadvantages of the other.  In reference-counting, you have the
overhead of incrementing/decrementing a counter and the
above-mentioned unreachability issue.  With mark-and-sweep, you can
end up with pauses as the VM rejiggers memory.

It's also my understanding that Jython uses the JVM and thus gets the
JVM's generational mark-and-sweep memory management instead of the
reference-counting that the CPython implementation does.  The GC
technique isn't part of the Python spec (AFAIK), so it's up to the
individual implementation which one to use.  MRAB's post-with-link
regarding CPython's implementation suggests that CPython does
indeed provide optional mark-and-sweep style collection:

    """
    CPython implementation detail: CPython currently uses a
    reference-counting scheme with (optional) delayed detection
    of cyclically linked garbage, which collects most objects as
    soon as they become unreachable, but is not guaranteed to
    collect garbage containing circular references. See the
    documentation of the gc module for information on controlling
    the collection of cyclic garbage. Other implementations act
    differently and CPython may change. Do not depend on
    immediate finalization of objects when they become
    unreachable (so you should always close files explicitly).
    """

So it sounds like you have to request such a mark-and-sweep from
the gc module.

-tkc


Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Inner workings of this Python feature: Can a Python data structure reference itself? vasudevram <vasudevram@gmail.com> - 2015-05-02 13:02 -0700
  Re: Inner workings of this Python feature: Can a Python data structure reference itself? Tim Chase <python.list@tim.thechases.com> - 2015-05-02 15:17 -0500
    Re: Inner workings of this Python feature: Can a Python data structure reference itself? vasudevram <vasudevram@gmail.com> - 2015-05-02 14:07 -0700
    Re: Inner workings of this Python feature: Can a Python data structure reference itself? Cecil Westerhof <Cecil@decebal.nl> - 2015-05-02 23:06 +0200
      Re: Inner workings of this Python feature: Can a Python data structure reference itself? MRAB <python@mrabarnett.plus.com> - 2015-05-03 00:07 +0100
      Re: Inner workings of this Python feature: Can a Python data structure reference itself? Tim Chase <python.list@tim.thechases.com> - 2015-05-02 20:57 -0500
      Re: Inner workings of this Python feature: Can a Python data structure reference itself? Ian Kelly <ian.g.kelly@gmail.com> - 2015-05-02 22:29 -0600
  Re: Inner workings of this Python feature: Can a Python data structure reference itself? Tim Chase <python.list@tim.thechases.com> - 2015-05-02 15:10 -0500
  Re: Inner workings of this Python feature: Can a Python data structure reference itself? Terry Reedy <tjreedy@udel.edu> - 2015-05-02 19:17 -0400
    Re: Inner workings of this Python feature: Can a Python data structure reference itself? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-05-03 21:10 +1000
    Re: Inner workings of this Python feature: Can a Python data structure reference itself? vasudevram <vasudevram@gmail.com> - 2015-05-03 05:59 -0700
      Re: Inner workings of this Python feature: Can a Python data structure reference itself? vasudevram <vasudevram@gmail.com> - 2015-05-03 06:05 -0700
      Re: Inner workings of this Python feature: Can a Python data structure reference itself? Chris Angelico <rosuav@gmail.com> - 2015-05-03 23:08 +1000
        Re: Inner workings of this Python feature: Can a Python data structure reference itself? vasudevram <vasudevram@gmail.com> - 2015-05-03 08:46 -0700
  Re: Inner workings of this Python feature: Can a Python data structure reference itself? Ian Kelly <ian.g.kelly@gmail.com> - 2015-05-02 22:43 -0600
  Re: Inner workings of this Python feature: Can a Python data structure reference itself? Chris Angelico <rosuav@gmail.com> - 2015-05-03 15:46 +1000
  Re: Inner workings of this Python feature: Can a Python data structure reference itself? vasudevram <vasudevram@gmail.com> - 2015-05-03 03:52 -0700

csiph-web