| LEFT | RIGHT |
| 1 /* | 1 /* |
| 2 | 2 |
| 3 Reference Cycle Garbage Collection | 3 Reference Cycle Garbage Collection |
| 4 ================================== | 4 ================================== |
| 5 | 5 |
| 6 Neil Schemenauer <nas@arctrix.com> | 6 Neil Schemenauer <nas@arctrix.com> |
| 7 | 7 |
| 8 Based on a post on the python-dev list. Ideas from Guido van Rossum, | 8 Based on a post on the python-dev list. Ideas from Guido van Rossum, |
| 9 Eric Tiedemann, and various others. | 9 Eric Tiedemann, and various others. |
| 10 | 10 |
| 11 http://www.arctrix.com/nas/python/gc/ | 11 http://www.arctrix.com/nas/python/gc/ |
| 12 http://www.python.org/pipermail/python-dev/2000-March/003869.html | 12 |
| 13 http://www.python.org/pipermail/python-dev/2000-March/004010.html | 13 The following mailing list threads provide a historical perspective on |
| 14 http://www.python.org/pipermail/python-dev/2000-March/004022.html | 14 the design of this module. Note that a fair amount of refinement has |
| 15 occurred since those discussions. |
| 16 |
| 17 http://mail.python.org/pipermail/python-dev/2000-March/002385.html |
| 18 http://mail.python.org/pipermail/python-dev/2000-March/002434.html |
| 19 http://mail.python.org/pipermail/python-dev/2000-March/002497.html |
| 15 | 20 |
| 16 For a highlevel view of the collection process, read the collect | 21 For a highlevel view of the collection process, read the collect |
| 17 function. | 22 function. |
| 18 | 23 |
| 19 */ | 24 */ |
| 20 | 25 |
| 21 #include "Python.h" | 26 #include "Python.h" |
| 22 #include "frameobject.h" /* for PyFrame_ClearFreeList */ | 27 #include "frameobject.h" /* for PyFrame_ClearFreeList */ |
| 23 | 28 |
| 24 #ifdef WITH_DTRACE | 29 #ifdef WITH_DTRACE |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 | 62 |
| 58 /* true if we are currently running the collector */ | 63 /* true if we are currently running the collector */ |
| 59 static int collecting = 0; | 64 static int collecting = 0; |
| 60 | 65 |
| 61 /* list of uncollectable objects */ | 66 /* list of uncollectable objects */ |
| 62 static PyObject *garbage = NULL; | 67 static PyObject *garbage = NULL; |
| 63 | 68 |
| 64 /* Python string to use if unhandled exception occurs */ | 69 /* Python string to use if unhandled exception occurs */ |
| 65 static PyObject *gc_str = NULL; | 70 static PyObject *gc_str = NULL; |
| 66 | 71 |
| 67 /* Python string used to look for __del__ attribute. */ | 72 /* a list of callbacks to be invoked when collection is performed */ |
| 68 static PyObject *delstr = NULL; | 73 static PyObject *callbacks = NULL; |
| 69 | 74 |
| 70 /* This is the number of objects who survived the last full collection. It | 75 /* This is the number of objects that survived the last full collection. It |
| 71 approximates the number of long lived objects tracked by the GC. | 76 approximates the number of long lived objects tracked by the GC. |
| 72 | 77 |
| 73 (by "full collection", we mean a collection of the oldest generation). | 78 (by "full collection", we mean a collection of the oldest generation). |
| 74 */ | 79 */ |
| 75 static Py_ssize_t long_lived_total = 0; | 80 static Py_ssize_t long_lived_total = 0; |
| 76 | 81 |
| 77 /* This is the number of objects who survived all "non-full" collections, | 82 /* This is the number of objects that survived all "non-full" collections, |
| 78 and are awaiting to undergo a full collection for the first time. | 83 and are awaiting to undergo a full collection for the first time. |
| 79 | 84 |
| 80 */ | 85 */ |
| 81 static Py_ssize_t long_lived_pending = 0; | 86 static Py_ssize_t long_lived_pending = 0; |
| 82 | 87 |
| 83 /* | 88 /* |
| 84 NOTE: about the counting of long-lived objects. | 89 NOTE: about the counting of long-lived objects. |
| 85 | 90 |
| 86 To limit the cost of garbage collection, there are two strategies; | 91 To limit the cost of garbage collection, there are two strategies; |
| 87 - make each collection faster, e.g. by scanning fewer objects | 92 - make each collection faster, e.g. by scanning fewer objects |
| (...skipping 20 matching lines...) Expand all Loading... |
| 108 Using the above ratio, instead, yields amortized linear performance in | 113 Using the above ratio, instead, yields amortized linear performance in |
| 109 the total number of objects (the effect of which can be summarized | 114 the total number of objects (the effect of which can be summarized |
| 110 thusly: "each full garbage collection is more and more costly as the | 115 thusly: "each full garbage collection is more and more costly as the |
| 111 number of objects grows, but we do fewer and fewer of them"). | 116 number of objects grows, but we do fewer and fewer of them"). |
| 112 | 117 |
| 113 This heuristic was suggested by Martin von Löwis on python-dev in | 118 This heuristic was suggested by Martin von Löwis on python-dev in |
| 114 June 2008. His original analysis and proposal can be found at: | 119 June 2008. His original analysis and proposal can be found at: |
| 115 http://mail.python.org/pipermail/python-dev/2008-June/080579.html | 120 http://mail.python.org/pipermail/python-dev/2008-June/080579.html |
| 116 */ | 121 */ |
| 117 | 122 |
| 123 /* |
| 124 NOTE: about untracking of mutable objects. |
| 125 |
| 126 Certain types of container cannot participate in a reference cycle, and |
| 127 so do not need to be tracked by the garbage collector. Untracking these |
| 128 objects reduces the cost of garbage collections. However, determining |
| 129 which objects may be untracked is not free, and the costs must be |
| 130 weighed against the benefits for garbage collection. |
| 131 |
| 132 There are two possible strategies for when to untrack a container: |
| 133 |
| 134 i) When the container is created. |
| 135 ii) When the container is examined by the garbage collector. |
| 136 |
| 137 Tuples containing only immutable objects (integers, strings etc, and |
| 138 recursively, tuples of immutable objects) do not need to be tracked. |
| 139 The interpreter creates a large number of tuples, many of which will |
| 140 not survive until garbage collection. It is therefore not worthwhile |
| 141 to untrack eligible tuples at creation time. |
| 142 |
| 143 Instead, all tuples except the empty tuple are tracked when created. |
| 144 During garbage collection it is determined whether any surviving tuples |
| 145 can be untracked. A tuple can be untracked if all of its contents are |
| 146 already not tracked. Tuples are examined for untracking in all garbage |
| 147 collection cycles. It may take more than one cycle to untrack a tuple. |
| 148 |
| 149 Dictionaries containing only immutable objects also do not need to be |
| 150 tracked. Dictionaries are untracked when created. If a tracked item is |
| 151 inserted into a dictionary (either as a key or value), the dictionary |
| 152 becomes tracked. During a full garbage collection (all generations), |
| 153 the collector will untrack any dictionaries whose contents are not |
| 154 tracked. |
| 155 |
| 156 The module provides the python function is_tracked(obj), which returns |
| 157 the CURRENT tracking status of the object. Subsequent garbage |
| 158 collections may change the tracking status of the object. |
| 159 |
| 160 Untracking of certain containers was introduced in issue #4688, and |
| 161 the algorithm was refined in response to issue #14775. |
| 162 */ |
| 118 | 163 |
| 119 /* set for debugging information */ | 164 /* set for debugging information */ |
| 120 #define DEBUG_STATS (1<<0) /* print collection statistics */ | 165 #define DEBUG_STATS (1<<0) /* print collection statistics */ |
| 121 #define DEBUG_COLLECTABLE (1<<1) /* print collectable objects */ | 166 #define DEBUG_COLLECTABLE (1<<1) /* print collectable objects */ |
| 122 #define DEBUG_UNCOLLECTABLE (1<<2) /* print uncollectable objects */ | 167 #define DEBUG_UNCOLLECTABLE (1<<2) /* print uncollectable objects */ |
| 123 #define DEBUG_INSTANCES (1<<3) /* print instances */ | |
| 124 #define DEBUG_OBJECTS (1<<4) /* print other objects */ | |
| 125 #define DEBUG_SAVEALL (1<<5) /* save all garbage in gc.garbage */ | 168 #define DEBUG_SAVEALL (1<<5) /* save all garbage in gc.garbage */ |
| 126 #define DEBUG_LEAK DEBUG_COLLECTABLE | \ | 169 #define DEBUG_LEAK DEBUG_COLLECTABLE | \ |
| 127 DEBUG_UNCOLLECTABLE | \ | 170 DEBUG_UNCOLLECTABLE | \ |
| 128 DEBUG_INSTANCES | \ | |
| 129 DEBUG_OBJECTS | \ | |
| 130 DEBUG_SAVEALL | 171 DEBUG_SAVEALL |
| 131 static int debug; | 172 static int debug; |
| 132 static PyObject *tmod = NULL; | 173 static PyObject *tmod = NULL; |
| 133 | 174 |
| 134 /*-------------------------------------------------------------------------- | 175 /*-------------------------------------------------------------------------- |
| 135 gc_refs values. | 176 gc_refs values. |
| 136 | 177 |
| 137 Between collections, every gc'ed object has one of two gc_refs values: | 178 Between collections, every gc'ed object has one of two gc_refs values: |
| 138 | 179 |
| 139 GC_UNTRACKED | 180 GC_UNTRACKED |
| (...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 433 traverseproc traverse = Py_TYPE(op)->tp_traverse; | 474 traverseproc traverse = Py_TYPE(op)->tp_traverse; |
| 434 assert(gc->gc.gc_refs > 0); | 475 assert(gc->gc.gc_refs > 0); |
| 435 gc->gc.gc_refs = GC_REACHABLE; | 476 gc->gc.gc_refs = GC_REACHABLE; |
| 436 (void) traverse(op, | 477 (void) traverse(op, |
| 437 (visitproc)visit_reachable, | 478 (visitproc)visit_reachable, |
| 438 (void *)young); | 479 (void *)young); |
| 439 next = gc->gc.gc_next; | 480 next = gc->gc.gc_next; |
| 440 if (PyTuple_CheckExact(op)) { | 481 if (PyTuple_CheckExact(op)) { |
| 441 _PyTuple_MaybeUntrack(op); | 482 _PyTuple_MaybeUntrack(op); |
| 442 } | 483 } |
| 443 else if (PyDict_CheckExact(op)) { | |
| 444 _PyDict_MaybeUntrack(op); | |
| 445 } | |
| 446 } | 484 } |
| 447 else { | 485 else { |
| 448 /* This *may* be unreachable. To make progress, | 486 /* This *may* be unreachable. To make progress, |
| 449 * assume it is. gc isn't directly reachable from | 487 * assume it is. gc isn't directly reachable from |
| 450 * any object we've already traversed, but may be | 488 * any object we've already traversed, but may be |
| 451 * reachable from an object we haven't gotten to yet. | 489 * reachable from an object we haven't gotten to yet. |
| 452 * visit_reachable will eventually move gc back into | 490 * visit_reachable will eventually move gc back into |
| 453 * young if that's so, and we'll see it again. | 491 * young if that's so, and we'll see it again. |
| 454 */ | 492 */ |
| 455 next = gc->gc.gc_next; | 493 next = gc->gc.gc_next; |
| 456 gc_list_move(gc, unreachable); | 494 gc_list_move(gc, unreachable); |
| 457 gc->gc.gc_refs = GC_TENTATIVELY_UNREACHABLE; | 495 gc->gc.gc_refs = GC_TENTATIVELY_UNREACHABLE; |
| 458 } | 496 } |
| 459 gc = next; | 497 gc = next; |
| 460 } | 498 } |
| 461 } | 499 } |
| 462 | 500 |
| 463 /* Return true if object has a finalization method. | 501 /* Try to untrack all currently tracked dictionaries */ |
| 464 * CAUTION: An instance of an old-style class has to be checked for a | 502 static void |
| 465 *__del__ method, and earlier versions of this used to call PyObject_HasAttr, | 503 untrack_dicts(PyGC_Head *head) |
| 466 * which in turn could call the class's __getattr__ hook (if any). That | 504 { |
| 467 * could invoke arbitrary Python code, mutating the object graph in arbitrary | 505 PyGC_Head *next, *gc = head->gc.gc_next; |
| 468 * ways, and that was the source of some excruciatingly subtle bugs. | 506 while (gc != head) { |
| 469 */ | 507 PyObject *op = FROM_GC(gc); |
| 508 next = gc->gc.gc_next; |
| 509 if (PyDict_CheckExact(op)) |
| 510 _PyDict_MaybeUntrack(op); |
| 511 gc = next; |
| 512 } |
| 513 } |
| 514 |
| 515 /* Return true if object has a finalization method. */ |
| 470 static int | 516 static int |
| 471 has_finalizer(PyObject *op) | 517 has_finalizer(PyObject *op) |
| 472 { | 518 { |
| 473 if (PyInstance_Check(op)) { | 519 if (PyGen_CheckExact(op)) |
| 474 assert(delstr != NULL); | |
| 475 return _PyInstance_Lookup(op, delstr) != NULL; | |
| 476 } | |
| 477 else if (PyType_HasFeature(op->ob_type, Py_TPFLAGS_HEAPTYPE)) | |
| 478 return op->ob_type->tp_del != NULL; | |
| 479 else if (PyGen_CheckExact(op)) | |
| 480 return PyGen_NeedsFinalizing((PyGenObject *)op); | 520 return PyGen_NeedsFinalizing((PyGenObject *)op); |
| 481 else | 521 else |
| 482 return 0; | 522 return op->ob_type->tp_del != NULL; |
| 483 } | 523 } |
| 484 | 524 |
| 485 /* Move the objects in unreachable with __del__ methods into `finalizers`. | 525 /* Move the objects in unreachable with __del__ methods into `finalizers`. |
| 486 * Objects moved into `finalizers` have gc_refs set to GC_REACHABLE; the | 526 * Objects moved into `finalizers` have gc_refs set to GC_REACHABLE; the |
| 487 * objects remaining in unreachable are left at GC_TENTATIVELY_UNREACHABLE. | 527 * objects remaining in unreachable are left at GC_TENTATIVELY_UNREACHABLE. |
| 488 */ | 528 */ |
| 489 static void | 529 static void |
| 490 move_finalizers(PyGC_Head *unreachable, PyGC_Head *finalizers) | 530 move_finalizers(PyGC_Head *unreachable, PyGC_Head *finalizers) |
| 491 { | 531 { |
| 492 PyGC_Head *gc; | 532 PyGC_Head *gc; |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 686 gc_list_move(gc, old); | 726 gc_list_move(gc, old); |
| 687 } | 727 } |
| 688 else | 728 else |
| 689 ++num_freed; | 729 ++num_freed; |
| 690 } | 730 } |
| 691 | 731 |
| 692 return num_freed; | 732 return num_freed; |
| 693 } | 733 } |
| 694 | 734 |
| 695 static void | 735 static void |
| 696 debug_instance(char *msg, PyInstanceObject *inst) | |
| 697 { | |
| 698 char *cname; | |
| 699 /* simple version of instance_repr */ | |
| 700 PyObject *classname = inst->in_class->cl_name; | |
| 701 if (classname != NULL && PyString_Check(classname)) | |
| 702 cname = PyString_AsString(classname); | |
| 703 else | |
| 704 cname = "?"; | |
| 705 PySys_WriteStderr("gc: %.100s <%.100s instance at %p>\n", | |
| 706 msg, cname, inst); | |
| 707 } | |
| 708 | |
| 709 static void | |
| 710 debug_cycle(char *msg, PyObject *op) | 736 debug_cycle(char *msg, PyObject *op) |
| 711 { | 737 { |
| 712 if ((debug & DEBUG_INSTANCES) && PyInstance_Check(op)) { | 738 PySys_FormatStderr("gc: %s <%s %p>\n", |
| 713 debug_instance(msg, (PyInstanceObject *)op); | 739 msg, Py_TYPE(op)->tp_name, op); |
| 714 } | |
| 715 else if (debug & DEBUG_OBJECTS) { | |
| 716 PySys_WriteStderr("gc: %.100s <%.100s %p>\n", | |
| 717 msg, Py_TYPE(op)->tp_name, op); | |
| 718 } | |
| 719 } | 740 } |
| 720 | 741 |
| 721 /* Handle uncollectable garbage (cycles with finalizers, and stuff reachable | 742 /* Handle uncollectable garbage (cycles with finalizers, and stuff reachable |
| 722 * only from such cycles). | 743 * only from such cycles). |
| 723 * If DEBUG_SAVEALL, all objects in finalizers are appended to the module | 744 * If DEBUG_SAVEALL, all objects in finalizers are appended to the module |
| 724 * garbage list (a Python list), else only the objects in finalizers with | 745 * garbage list (a Python list), else only the objects in finalizers with |
| 725 * __del__ methods are appended to garbage. All objects in finalizers are | 746 * __del__ methods are appended to garbage. All objects in finalizers are |
| 726 * merged into the old list regardless. | 747 * merged into the old list regardless. |
| 727 * Returns 0 if all OK, <0 on error (out of memory to grow the garbage list). | 748 * Returns 0 if all OK, <0 on error (out of memory to grow the garbage list). |
| 728 * The finalizers list is made empty on a successful return. | 749 * The finalizers list is made empty on a successful return. |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 787 * Allocated items in the free list may keep a pymalloc arena occupied. | 808 * Allocated items in the free list may keep a pymalloc arena occupied. |
| 788 * Clearing the free lists may give back memory to the OS earlier. | 809 * Clearing the free lists may give back memory to the OS earlier. |
| 789 */ | 810 */ |
| 790 static void | 811 static void |
| 791 clear_freelists(void) | 812 clear_freelists(void) |
| 792 { | 813 { |
| 793 (void)PyMethod_ClearFreeList(); | 814 (void)PyMethod_ClearFreeList(); |
| 794 (void)PyFrame_ClearFreeList(); | 815 (void)PyFrame_ClearFreeList(); |
| 795 (void)PyCFunction_ClearFreeList(); | 816 (void)PyCFunction_ClearFreeList(); |
| 796 (void)PyTuple_ClearFreeList(); | 817 (void)PyTuple_ClearFreeList(); |
| 797 #ifdef Py_USING_UNICODE | |
| 798 (void)PyUnicode_ClearFreeList(); | 818 (void)PyUnicode_ClearFreeList(); |
| 799 #endif | |
| 800 (void)PyInt_ClearFreeList(); | |
| 801 (void)PyFloat_ClearFreeList(); | 819 (void)PyFloat_ClearFreeList(); |
| 820 (void)PyList_ClearFreeList(); |
| 821 (void)PyDict_ClearFreeList(); |
| 822 (void)PySet_ClearFreeList(); |
| 802 } | 823 } |
| 803 | 824 |
| 804 static double | 825 static double |
| 805 get_time(void) | 826 get_time(void) |
| 806 { | 827 { |
| 807 double result = 0; | 828 double result = 0; |
| 808 if (tmod != NULL) { | 829 if (tmod != NULL) { |
| 809 PyObject *f = PyObject_CallMethod(tmod, "time", NULL); | 830 _Py_IDENTIFIER(time); |
| 831 |
| 832 PyObject *f = _PyObject_CallMethodId(tmod, &PyId_time, NULL); |
| 810 if (f == NULL) { | 833 if (f == NULL) { |
| 811 PyErr_Clear(); | 834 PyErr_Clear(); |
| 812 } | 835 } |
| 813 else { | 836 else { |
| 814 if (PyFloat_Check(f)) | 837 if (PyFloat_Check(f)) |
| 815 result = PyFloat_AsDouble(f); | 838 result = PyFloat_AsDouble(f); |
| 816 Py_DECREF(f); | 839 Py_DECREF(f); |
| 817 } | 840 } |
| 818 } | 841 } |
| 819 return result; | 842 return result; |
| 820 } | 843 } |
| 821 | 844 |
| 822 /* This is the main function. Read this to understand how the | 845 /* This is the main function. Read this to understand how the |
| 823 * collection process works. */ | 846 * collection process works. */ |
| 824 static Py_ssize_t | 847 static Py_ssize_t |
| 825 #ifdef WITH_DTRACE | 848 #ifdef WITH_DTRACE |
| 826 collect2 | 849 collect2 |
| 827 #else | 850 #else |
| 828 collect | 851 collect |
| 829 #endif | 852 #endif |
| 830 (int generation) | 853 (int generation, Py_ssize_t *n_collected, Py_ssize_t *n_uncollectable) |
| 831 { | 854 { |
| 832 int i; | 855 int i; |
| 833 Py_ssize_t m = 0; /* # objects collected */ | 856 Py_ssize_t m = 0; /* # objects collected */ |
| 834 Py_ssize_t n = 0; /* # unreachable objects that couldn't be collected */ | 857 Py_ssize_t n = 0; /* # unreachable objects that couldn't be collected */ |
| 835 PyGC_Head *young; /* the generation we are examining */ | 858 PyGC_Head *young; /* the generation we are examining */ |
| 836 PyGC_Head *old; /* next older generation */ | 859 PyGC_Head *old; /* next older generation */ |
| 837 PyGC_Head unreachable; /* non-problematic unreachable trash */ | 860 PyGC_Head unreachable; /* non-problematic unreachable trash */ |
| 838 PyGC_Head finalizers; /* objects with, & reachable from, __del__ */ | 861 PyGC_Head finalizers; /* objects with, & reachable from, __del__ */ |
| 839 PyGC_Head *gc; | 862 PyGC_Head *gc; |
| 840 double t1 = 0.0; | 863 double t1 = 0.0; |
| 841 | 864 |
| 842 if (delstr == NULL) { | |
| 843 delstr = PyString_InternFromString("__del__"); | |
| 844 if (delstr == NULL) | |
| 845 Py_FatalError("gc couldn't allocate \"__del__\""); | |
| 846 } | |
| 847 | |
| 848 if (debug & DEBUG_STATS) { | 865 if (debug & DEBUG_STATS) { |
| 849 PySys_WriteStderr("gc: collecting generation %d...\n", | 866 PySys_WriteStderr("gc: collecting generation %d...\n", |
| 850 generation); | 867 generation); |
| 851 PySys_WriteStderr("gc: objects in each generation:"); | 868 PySys_WriteStderr("gc: objects in each generation:"); |
| 852 for (i = 0; i < NUM_GENERATIONS; i++) | 869 for (i = 0; i < NUM_GENERATIONS; i++) |
| 853 PySys_WriteStderr(" %" PY_FORMAT_SIZE_T "d", | 870 PySys_WriteStderr(" %" PY_FORMAT_SIZE_T "d", |
| 854 gc_list_size(GEN_HEAD(i))); | 871 gc_list_size(GEN_HEAD(i))); |
| 855 t1 = get_time(); | 872 t1 = get_time(); |
| 856 PySys_WriteStderr("\n"); | 873 PySys_WriteStderr("\n"); |
| 857 } | 874 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 892 move_unreachable(young, &unreachable); | 909 move_unreachable(young, &unreachable); |
| 893 | 910 |
| 894 /* Move reachable objects to next generation. */ | 911 /* Move reachable objects to next generation. */ |
| 895 if (young != old) { | 912 if (young != old) { |
| 896 if (generation == NUM_GENERATIONS - 2) { | 913 if (generation == NUM_GENERATIONS - 2) { |
| 897 long_lived_pending += gc_list_size(young); | 914 long_lived_pending += gc_list_size(young); |
| 898 } | 915 } |
| 899 gc_list_merge(young, old); | 916 gc_list_merge(young, old); |
| 900 } | 917 } |
| 901 else { | 918 else { |
| 919 /* We only untrack dicts in full collections, to avoid quadratic |
| 920 dict build-up. See issue #14775. */ |
| 921 untrack_dicts(young); |
| 902 long_lived_pending = 0; | 922 long_lived_pending = 0; |
| 903 long_lived_total = gc_list_size(young); | 923 long_lived_total = gc_list_size(young); |
| 904 } | 924 } |
| 905 | 925 |
| 906 /* All objects in unreachable are trash, but objects reachable from | 926 /* All objects in unreachable are trash, but objects reachable from |
| 907 * finalizers can't safely be deleted. Python programmers should take | 927 * finalizers can't safely be deleted. Python programmers should take |
| 908 * care not to create such things. For Python, finalizers means | 928 * care not to create such things. For Python, finalizers means |
| 909 * instance objects with __del__ methods. Weakrefs with callbacks | 929 * instance objects with __del__ methods. Weakrefs with callbacks |
| 910 * can also call arbitrary Python code but they will be dealt with by | 930 * can also call arbitrary Python code but they will be dealt with by |
| 911 * handle_weakrefs(). | 931 * handle_weakrefs(). |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 970 (void)handle_finalizers(&finalizers, old); | 990 (void)handle_finalizers(&finalizers, old); |
| 971 | 991 |
| 972 /* Clear free list only during the collection of the highest | 992 /* Clear free list only during the collection of the highest |
| 973 * generation */ | 993 * generation */ |
| 974 if (generation == NUM_GENERATIONS-1) { | 994 if (generation == NUM_GENERATIONS-1) { |
| 975 clear_freelists(); | 995 clear_freelists(); |
| 976 } | 996 } |
| 977 | 997 |
| 978 if (PyErr_Occurred()) { | 998 if (PyErr_Occurred()) { |
| 979 if (gc_str == NULL) | 999 if (gc_str == NULL) |
| 980 gc_str = PyString_FromString("garbage collection"); | 1000 gc_str = PyUnicode_FromString("garbage collection"); |
| 981 PyErr_WriteUnraisable(gc_str); | 1001 PyErr_WriteUnraisable(gc_str); |
| 982 Py_FatalError("unexpected exception during garbage collection"); | 1002 Py_FatalError("unexpected exception during garbage collection"); |
| 983 } | 1003 } |
| 1004 |
| 1005 if (n_collected) |
| 1006 *n_collected = m; |
| 1007 if (n_uncollectable) |
| 1008 *n_uncollectable = n; |
| 984 return n+m; | 1009 return n+m; |
| 985 } | 1010 } |
| 986 | 1011 |
| 987 #ifdef WITH_DTRACE | 1012 #ifdef WITH_DTRACE |
| 988 static void | 1013 static void |
| 989 dtrace_gc_start(int collection) | 1014 dtrace_gc_start(int collection) |
| 990 { | 1015 { |
| 991 PYTHON_GC_START(collection); | 1016 PYTHON_GC_START(collection); |
| 992 | 1017 |
| 993 /* | 1018 /* |
| (...skipping 13 matching lines...) Expand all Loading... |
| 1007 /* | 1032 /* |
| 1008 * Currently a USDT tail-call will not receive the correct arguments. | 1033 * Currently a USDT tail-call will not receive the correct arguments. |
| 1009 * Disable the tail call here. | 1034 * Disable the tail call here. |
| 1010 */ | 1035 */ |
| 1011 #if defined(__sparc) | 1036 #if defined(__sparc) |
| 1012 asm("nop"); | 1037 asm("nop"); |
| 1013 #endif | 1038 #endif |
| 1014 } | 1039 } |
| 1015 | 1040 |
| 1016 static Py_ssize_t | 1041 static Py_ssize_t |
| 1017 collect(int collection) | 1042 collect(int collection, Py_ssize_t *n_collected, Py_ssize_t *n_uncollectable) |
| 1018 { | 1043 { |
| 1019 Py_ssize_t value; | 1044 Py_ssize_t value; |
| 1020 | 1045 |
| 1021 if (PYTHON_GC_START_ENABLED()) | 1046 if (PYTHON_GC_START_ENABLED()) |
| 1022 dtrace_gc_start(collection); | 1047 dtrace_gc_start(collection); |
| 1023 value = collect2(collection); | 1048 value = collect2(collection, n_collected, n_uncollectable); |
| 1024 if (PYTHON_GC_DONE_ENABLED()) | 1049 if (PYTHON_GC_DONE_ENABLED()) |
| 1025 dtrace_gc_done(value); | 1050 dtrace_gc_done(value); |
| 1026 return value; | 1051 return value; |
| 1027 } | 1052 } |
| 1028 #endif /* WITH_DTRACE */ | 1053 #endif /* WITH_DTRACE */ |
| 1054 |
| 1055 /* Invoke progress callbacks to notify clients that garbage collection |
| 1056 * is starting or stopping |
| 1057 */ |
| 1058 static void |
| 1059 invoke_gc_callback(const char *phase, int generation, |
| 1060 Py_ssize_t collected, Py_ssize_t uncollectable) |
| 1061 { |
| 1062 Py_ssize_t i; |
| 1063 PyObject *info = NULL; |
| 1064 |
| 1065 /* we may get called very early */ |
| 1066 if (callbacks == NULL) |
| 1067 return; |
| 1068 /* The local variable cannot be rebound, check it for sanity */ |
| 1069 assert(callbacks != NULL && PyList_CheckExact(callbacks)); |
| 1070 if (PyList_GET_SIZE(callbacks) != 0) { |
| 1071 info = Py_BuildValue("{sisnsn}", |
| 1072 "generation", generation, |
| 1073 "collected", collected, |
| 1074 "uncollectable", uncollectable); |
| 1075 if (info == NULL) { |
| 1076 PyErr_WriteUnraisable(NULL); |
| 1077 return; |
| 1078 } |
| 1079 } |
| 1080 for (i=0; i<PyList_GET_SIZE(callbacks); i++) { |
| 1081 PyObject *r, *cb = PyList_GET_ITEM(callbacks, i); |
| 1082 Py_INCREF(cb); /* make sure cb doesn't go away */ |
| 1083 r = PyObject_CallFunction(cb, "sO", phase, info); |
| 1084 Py_XDECREF(r); |
| 1085 if (r == NULL) |
| 1086 PyErr_WriteUnraisable(cb); |
| 1087 Py_DECREF(cb); |
| 1088 } |
| 1089 Py_XDECREF(info); |
| 1090 } |
| 1091 |
| 1092 /* Perform garbage collection of a generation and invoke |
| 1093 * progress callbacks. |
| 1094 */ |
| 1095 static Py_ssize_t |
| 1096 collect_with_callback(int generation) |
| 1097 { |
| 1098 Py_ssize_t result, collected, uncollectable; |
| 1099 invoke_gc_callback("start", generation, 0, 0); |
| 1100 result = collect(generation, &collected, &uncollectable); |
| 1101 invoke_gc_callback("stop", generation, collected, uncollectable); |
| 1102 return result; |
| 1103 } |
| 1029 | 1104 |
| 1030 static Py_ssize_t | 1105 static Py_ssize_t |
| 1031 collect_generations(void) | 1106 collect_generations(void) |
| 1032 { | 1107 { |
| 1033 int i; | 1108 int i; |
| 1034 Py_ssize_t n = 0; | 1109 Py_ssize_t n = 0; |
| 1035 | 1110 |
| 1036 /* Find the oldest generation (highest numbered) where the count | 1111 /* Find the oldest generation (highest numbered) where the count |
| 1037 * exceeds the threshold. Objects in the that generation and | 1112 * exceeds the threshold. Objects in the that generation and |
| 1038 * generations younger than it will be collected. */ | 1113 * generations younger than it will be collected. */ |
| 1039 for (i = NUM_GENERATIONS-1; i >= 0; i--) { | 1114 for (i = NUM_GENERATIONS-1; i >= 0; i--) { |
| 1040 if (generations[i].count > generations[i].threshold) { | 1115 if (generations[i].count > generations[i].threshold) { |
| 1041 /* Avoid quadratic performance degradation in number | 1116 /* Avoid quadratic performance degradation in number |
| 1042 of tracked objects. See comments at the beginning | 1117 of tracked objects. See comments at the beginning |
| 1043 of this file, and issue #4074. | 1118 of this file, and issue #4074. |
| 1044 */ | 1119 */ |
| 1045 if (i == NUM_GENERATIONS - 1 | 1120 if (i == NUM_GENERATIONS - 1 |
| 1046 && long_lived_pending < long_lived_total / 4) | 1121 && long_lived_pending < long_lived_total / 4) |
| 1047 continue; | 1122 continue; |
| 1048 n = collect(i); | 1123 n = collect_with_callback(i); |
| 1049 break; | 1124 break; |
| 1050 } | 1125 } |
| 1051 } | 1126 } |
| 1052 return n; | 1127 return n; |
| 1053 } | 1128 } |
| 1054 | 1129 |
| 1055 PyDoc_STRVAR(gc_enable__doc__, | 1130 PyDoc_STRVAR(gc_enable__doc__, |
| 1056 "enable() -> None\n" | 1131 "enable() -> None\n" |
| 1057 "\n" | 1132 "\n" |
| 1058 "Enable automatic garbage collection.\n"); | 1133 "Enable automatic garbage collection.\n"); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1109 | 1184 |
| 1110 else if (genarg < 0 || genarg >= NUM_GENERATIONS) { | 1185 else if (genarg < 0 || genarg >= NUM_GENERATIONS) { |
| 1111 PyErr_SetString(PyExc_ValueError, "invalid generation"); | 1186 PyErr_SetString(PyExc_ValueError, "invalid generation"); |
| 1112 return NULL; | 1187 return NULL; |
| 1113 } | 1188 } |
| 1114 | 1189 |
| 1115 if (collecting) | 1190 if (collecting) |
| 1116 n = 0; /* already collecting, don't do anything */ | 1191 n = 0; /* already collecting, don't do anything */ |
| 1117 else { | 1192 else { |
| 1118 collecting = 1; | 1193 collecting = 1; |
| 1119 n = collect(genarg); | 1194 n = collect_with_callback(genarg); |
| 1120 collecting = 0; | 1195 collecting = 0; |
| 1121 } | 1196 } |
| 1122 | 1197 |
| 1123 return PyInt_FromSsize_t(n); | 1198 return PyLong_FromSsize_t(n); |
| 1124 } | 1199 } |
| 1125 | 1200 |
| 1126 PyDoc_STRVAR(gc_set_debug__doc__, | 1201 PyDoc_STRVAR(gc_set_debug__doc__, |
| 1127 "set_debug(flags) -> None\n" | 1202 "set_debug(flags) -> None\n" |
| 1128 "\n" | 1203 "\n" |
| 1129 "Set the garbage collection debugging flags. Debugging information is\n" | 1204 "Set the garbage collection debugging flags. Debugging information is\n" |
| 1130 "written to sys.stderr.\n" | 1205 "written to sys.stderr.\n" |
| 1131 "\n" | 1206 "\n" |
| 1132 "flags is an integer and can have the following bits turned on:\n" | 1207 "flags is an integer and can have the following bits turned on:\n" |
| 1133 "\n" | 1208 "\n" |
| 1134 " DEBUG_STATS - Print statistics during collection.\n" | 1209 " DEBUG_STATS - Print statistics during collection.\n" |
| 1135 " DEBUG_COLLECTABLE - Print collectable objects found.\n" | 1210 " DEBUG_COLLECTABLE - Print collectable objects found.\n" |
| 1136 " DEBUG_UNCOLLECTABLE - Print unreachable but uncollectable objects found.\n" | 1211 " DEBUG_UNCOLLECTABLE - Print unreachable but uncollectable objects found.\n" |
| 1137 " DEBUG_INSTANCES - Print instance objects.\n" | |
| 1138 " DEBUG_OBJECTS - Print objects other than instances.\n" | |
| 1139 " DEBUG_SAVEALL - Save objects to gc.garbage rather than freeing them.\n" | 1212 " DEBUG_SAVEALL - Save objects to gc.garbage rather than freeing them.\n" |
| 1140 " DEBUG_LEAK - Debug leaking programs (everything but STATS).\n"); | 1213 " DEBUG_LEAK - Debug leaking programs (everything but STATS).\n"); |
| 1141 | 1214 |
| 1142 static PyObject * | 1215 static PyObject * |
| 1143 gc_set_debug(PyObject *self, PyObject *args) | 1216 gc_set_debug(PyObject *self, PyObject *args) |
| 1144 { | 1217 { |
| 1145 if (!PyArg_ParseTuple(args, "i:set_debug", &debug)) | 1218 if (!PyArg_ParseTuple(args, "i:set_debug", &debug)) |
| 1146 return NULL; | 1219 return NULL; |
| 1147 | 1220 |
| 1148 Py_INCREF(Py_None); | 1221 Py_INCREF(Py_None); |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1373 METH_VARARGS | METH_KEYWORDS, gc_collect__doc__}, | 1446 METH_VARARGS | METH_KEYWORDS, gc_collect__doc__}, |
| 1374 {"get_objects", gc_get_objects,METH_NOARGS, gc_get_objects__doc__}, | 1447 {"get_objects", gc_get_objects,METH_NOARGS, gc_get_objects__doc__}, |
| 1375 {"is_tracked", gc_is_tracked, METH_O, gc_is_tracked__doc__}, | 1448 {"is_tracked", gc_is_tracked, METH_O, gc_is_tracked__doc__}, |
| 1376 {"get_referrers", gc_get_referrers, METH_VARARGS, | 1449 {"get_referrers", gc_get_referrers, METH_VARARGS, |
| 1377 gc_get_referrers__doc__}, | 1450 gc_get_referrers__doc__}, |
| 1378 {"get_referents", gc_get_referents, METH_VARARGS, | 1451 {"get_referents", gc_get_referents, METH_VARARGS, |
| 1379 gc_get_referents__doc__}, | 1452 gc_get_referents__doc__}, |
| 1380 {NULL, NULL} /* Sentinel */ | 1453 {NULL, NULL} /* Sentinel */ |
| 1381 }; | 1454 }; |
| 1382 | 1455 |
| 1456 static struct PyModuleDef gcmodule = { |
| 1457 PyModuleDef_HEAD_INIT, |
| 1458 "gc", /* m_name */ |
| 1459 gc__doc__, /* m_doc */ |
| 1460 -1, /* m_size */ |
| 1461 GcMethods, /* m_methods */ |
| 1462 NULL, /* m_reload */ |
| 1463 NULL, /* m_traverse */ |
| 1464 NULL, /* m_clear */ |
| 1465 NULL /* m_free */ |
| 1466 }; |
| 1467 |
| 1383 PyMODINIT_FUNC | 1468 PyMODINIT_FUNC |
| 1384 initgc(void) | 1469 PyInit_gc(void) |
| 1385 { | 1470 { |
| 1386 PyObject *m; | 1471 PyObject *m; |
| 1387 | 1472 |
| 1388 m = Py_InitModule4("gc", | 1473 m = PyModule_Create(&gcmodule); |
| 1389 GcMethods, | 1474 |
| 1390 gc__doc__, | |
| 1391 NULL, | |
| 1392 PYTHON_API_VERSION); | |
| 1393 if (m == NULL) | 1475 if (m == NULL) |
| 1394 return; | 1476 return NULL; |
| 1395 | 1477 |
| 1396 if (garbage == NULL) { | 1478 if (garbage == NULL) { |
| 1397 garbage = PyList_New(0); | 1479 garbage = PyList_New(0); |
| 1398 if (garbage == NULL) | 1480 if (garbage == NULL) |
| 1399 return; | 1481 return NULL; |
| 1400 } | 1482 } |
| 1401 Py_INCREF(garbage); | 1483 Py_INCREF(garbage); |
| 1402 if (PyModule_AddObject(m, "garbage", garbage) < 0) | 1484 if (PyModule_AddObject(m, "garbage", garbage) < 0) |
| 1403 return; | 1485 return NULL; |
| 1486 |
| 1487 if (callbacks == NULL) { |
| 1488 callbacks = PyList_New(0); |
| 1489 if (callbacks == NULL) |
| 1490 return NULL; |
| 1491 } |
| 1492 Py_INCREF(callbacks); |
| 1493 if (PyModule_AddObject(m, "callbacks", callbacks) < 0) |
| 1494 return NULL; |
| 1404 | 1495 |
| 1405 /* Importing can't be done in collect() because collect() | 1496 /* Importing can't be done in collect() because collect() |
| 1406 * can be called via PyGC_Collect() in Py_Finalize(). | 1497 * can be called via PyGC_Collect() in Py_Finalize(). |
| 1407 * This wouldn't be a problem, except that <initialized> is | 1498 * This wouldn't be a problem, except that <initialized> is |
| 1408 * reset to 0 before calling collect which trips up | 1499 * reset to 0 before calling collect which trips up |
| 1409 * the import and triggers an assertion. | 1500 * the import and triggers an assertion. |
| 1410 */ | 1501 */ |
| 1411 if (tmod == NULL) { | 1502 if (tmod == NULL) { |
| 1412 tmod = PyImport_ImportModuleNoBlock("time"); | 1503 tmod = PyImport_ImportModuleNoBlock("time"); |
| 1413 if (tmod == NULL) | 1504 if (tmod == NULL) |
| 1414 PyErr_Clear(); | 1505 PyErr_Clear(); |
| 1415 } | 1506 } |
| 1416 | 1507 |
| 1417 #define ADD_INT(NAME) if (PyModule_AddIntConstant(m, #NAME, NAME) < 0) return | 1508 #define ADD_INT(NAME) if (PyModule_AddIntConstant(m, #NAME, NAME) < 0) return NU
LL |
| 1418 ADD_INT(DEBUG_STATS); | 1509 ADD_INT(DEBUG_STATS); |
| 1419 ADD_INT(DEBUG_COLLECTABLE); | 1510 ADD_INT(DEBUG_COLLECTABLE); |
| 1420 ADD_INT(DEBUG_UNCOLLECTABLE); | 1511 ADD_INT(DEBUG_UNCOLLECTABLE); |
| 1421 ADD_INT(DEBUG_INSTANCES); | |
| 1422 ADD_INT(DEBUG_OBJECTS); | |
| 1423 ADD_INT(DEBUG_SAVEALL); | 1512 ADD_INT(DEBUG_SAVEALL); |
| 1424 ADD_INT(DEBUG_LEAK); | 1513 ADD_INT(DEBUG_LEAK); |
| 1425 #undef ADD_INT | 1514 #undef ADD_INT |
| 1515 return m; |
| 1426 } | 1516 } |
| 1427 | 1517 |
| 1428 /* API to invoke gc.collect() from C */ | 1518 /* API to invoke gc.collect() from C */ |
| 1429 Py_ssize_t | 1519 Py_ssize_t |
| 1430 PyGC_Collect(void) | 1520 PyGC_Collect(void) |
| 1431 { | 1521 { |
| 1432 Py_ssize_t n; | 1522 Py_ssize_t n; |
| 1433 | 1523 |
| 1434 if (collecting) | 1524 if (collecting) |
| 1435 n = 0; /* already collecting, don't do anything */ | 1525 n = 0; /* already collecting, don't do anything */ |
| 1436 else { | 1526 else { |
| 1437 collecting = 1; | 1527 collecting = 1; |
| 1438 n = collect(NUM_GENERATIONS - 1); | 1528 n = collect_with_callback(NUM_GENERATIONS - 1); |
| 1439 collecting = 0; | 1529 collecting = 0; |
| 1440 } | 1530 } |
| 1441 | 1531 |
| 1442 return n; | 1532 return n; |
| 1533 } |
| 1534 |
| 1535 void |
| 1536 _PyGC_Fini(void) |
| 1537 { |
| 1538 if (!(debug & DEBUG_SAVEALL) |
| 1539 && garbage != NULL && PyList_GET_SIZE(garbage) > 0) { |
| 1540 char *message; |
| 1541 if (debug & DEBUG_UNCOLLECTABLE) |
| 1542 message = "gc: %zd uncollectable objects at " \ |
| 1543 "shutdown"; |
| 1544 else |
| 1545 message = "gc: %zd uncollectable objects at " \ |
| 1546 "shutdown; use gc.set_debug(gc.DEBUG_UNCOLLECTABLE) to list them
"; |
| 1547 if (PyErr_WarnFormat(PyExc_ResourceWarning, 0, message, |
| 1548 PyList_GET_SIZE(garbage)) < 0) |
| 1549 PyErr_WriteUnraisable(NULL); |
| 1550 if (debug & DEBUG_UNCOLLECTABLE) { |
| 1551 PyObject *repr = NULL, *bytes = NULL; |
| 1552 repr = PyObject_Repr(garbage); |
| 1553 if (!repr || !(bytes = PyUnicode_EncodeFSDefault(repr))) |
| 1554 PyErr_WriteUnraisable(garbage); |
| 1555 else { |
| 1556 PySys_WriteStderr( |
| 1557 " %s\n", |
| 1558 PyBytes_AS_STRING(bytes) |
| 1559 ); |
| 1560 } |
| 1561 Py_XDECREF(repr); |
| 1562 Py_XDECREF(bytes); |
| 1563 } |
| 1564 } |
| 1565 Py_CLEAR(callbacks); |
| 1443 } | 1566 } |
| 1444 | 1567 |
| 1445 /* for debugging */ | 1568 /* for debugging */ |
| 1446 void | 1569 void |
| 1447 _PyGC_Dump(PyGC_Head *g) | 1570 _PyGC_Dump(PyGC_Head *g) |
| 1448 { | 1571 { |
| 1449 _PyObject_Dump(FROM_GC(g)); | 1572 _PyObject_Dump(FROM_GC(g)); |
| 1450 } | 1573 } |
| 1451 | 1574 |
| 1452 /* extension modules might be compiled with GC support so these | 1575 /* extension modules might be compiled with GC support so these |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1551 PyObject_GC_Del(void *op) | 1674 PyObject_GC_Del(void *op) |
| 1552 { | 1675 { |
| 1553 PyGC_Head *g = AS_GC(op); | 1676 PyGC_Head *g = AS_GC(op); |
| 1554 if (IS_TRACKED(op)) | 1677 if (IS_TRACKED(op)) |
| 1555 gc_list_remove(g); | 1678 gc_list_remove(g); |
| 1556 if (generations[0].count > 0) { | 1679 if (generations[0].count > 0) { |
| 1557 generations[0].count--; | 1680 generations[0].count--; |
| 1558 } | 1681 } |
| 1559 PyObject_FREE(g); | 1682 PyObject_FREE(g); |
| 1560 } | 1683 } |
| 1561 | |
| 1562 /* for binary compatibility with 2.2 */ | |
| 1563 #undef _PyObject_GC_Del | |
| 1564 void | |
| 1565 _PyObject_GC_Del(PyObject *op) | |
| 1566 { | |
| 1567 PyObject_GC_Del(op); | |
| 1568 } | |
| LEFT | RIGHT |