lxcypp / pydermonkey

Automatically exported from code.google.com/p/pydermonkey
0 stars 0 forks source link

Add support for cross-language cyclic GC #2

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
As noted in the "Challenges" section of the current revision of the
documentation, detecting reference cycles between Python and JS-land isn't
currently supported, and as a result it's easy to introduce memory leaks
into a program that uses Pydermonkey.

Original issue reported on code.google.com by var...@gmail.com on 5 Sep 2009 at 4:57

GoogleCodeExporter commented 9 years ago
Since Python already has a cycle detector, it'd be nice if we could leverage 
this. 
One way to do it might be:

Whenever Python's cyclic GC starts to run, make it call our callback (if this 
is even
possible--if it's not possible, we'll have to manually call this function and 
invoke
Python GC ourselves). When our callback is called, do the following:

  1. Make a list of what JS runtimes are inactive.
  2. Trace through each runtime, creating a table of how connected their
Python-exposed JS objects are to each other.
  3. Temporarily increase the reference counts of each Python-exposed JS object to
reflect the other Python-exposed JS objects it's connected to. Let these
Python-exposed JS objects have access to the list of objects they're connected 
to, so
that their tp_traverse callback can properly direct Python's cyclic GC to these 
objects.
  4. Allow Python's cyclic GC to proceed.
  5. If a Python-exposed JS object's tp_clear callback is called, then it should
decrease its reference counts to all objects it refers to.
  6. When Python's cyclic GC is done, "undo" any temporary incrementing done in #3
that has not yet been undone.

Original comment by var...@gmail.com on 5 Sep 2009 at 5:12

GoogleCodeExporter commented 9 years ago
Useful notes from XPCOM's cycle collector:

  https://developer.mozilla.org/en/Interfacing_with_the_XPCOM_cycle_collector
  http://mxr.mozilla.org/mozilla-central/source/js/src/xpconnect/src/nsXPConnect.cpp#414
  http://mxr.mozilla.org/mozilla-central/source/xpcom/base/nsCycleCollector.cpp#40

Original comment by var...@gmail.com on 6 Sep 2009 at 12:02