Skip to content

Commit 3fb76b5

Browse files
committed
Reset the GC state when finalize
It's save for initialize python again, be it will cause a larger memory leak.
1 parent 7f1bcda commit 3fb76b5

5 files changed

Lines changed: 44 additions & 0 deletions

File tree

Include/object.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,7 @@ PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
759759
PyAPI_FUNC(void) _Py_PrintReferences(FILE *);
760760
PyAPI_FUNC(void) _Py_PrintReferenceAddresses(FILE *);
761761
PyAPI_FUNC(void) _Py_AddToAllObjects(PyObject *, int force);
762+
PyAPI_FUNC(void) _Py_ResetRefChain(void);
762763

763764
#else
764765
/* Without Py_TRACE_REFS, there's little enough to do that we expand code

Include/pylifecycle.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ PyAPI_FUNC(void) PyFloat_Fini(void);
104104
PyAPI_FUNC(void) PyOS_FiniInterrupts(void);
105105
PyAPI_FUNC(void) _PyGC_DumpShutdownStats(void);
106106
PyAPI_FUNC(void) _PyGC_Fini(void);
107+
PyAPI_FUNC(void) _PyGC_Clear(void);
107108
PyAPI_FUNC(void) PySlice_Fini(void);
108109
PyAPI_FUNC(void) _PyType_Fini(void);
109110
PyAPI_FUNC(void) _PyRandom_Fini(void);

Modules/gcmodule.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1671,6 +1671,16 @@ _PyGC_Fini(void)
16711671
Py_CLEAR(callbacks);
16721672
}
16731673

1674+
void
1675+
_PyGC_Clear(void)
1676+
{
1677+
for (int i = 0; i < NUM_GENERATIONS; ++i) {
1678+
PyGC_Head *head = GEN_HEAD(i);
1679+
head->gc.gc_next = head;
1680+
head->gc.gc_prev = head;
1681+
}
1682+
}
1683+
16741684
/* for debugging */
16751685
void
16761686
_PyGC_Dump(PyGC_Head *g)
@@ -1798,3 +1808,22 @@ PyObject_GC_Del(void *op)
17981808
}
17991809
PyObject_FREE(g);
18001810
}
1811+
1812+
void gc_init_gneration()
1813+
{
1814+
for (int i = 0; i < NUM_GENERATIONS; i++) {
1815+
PyGC_Head* head = GEN_HEAD(i);
1816+
if (head->gc.gc_next != head) {
1817+
PyGC_Head* dummy_head = head;
1818+
Py_ssize_t len = 0;
1819+
do {
1820+
dummy_head = dummy_head->gc.gc_next;
1821+
++len;
1822+
} while (dummy_head != head);
1823+
printf("gen[%d]garbage len :%"PY_FORMAT_SIZE_T"d\n", i, len);
1824+
}
1825+
//head->gc.gc_next = head;
1826+
//head->gc.gc_prev = head;
1827+
}
1828+
}
1829+

Objects/object.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ _Py_AddToAllObjects(PyObject *op, int force)
8585
refchain._ob_next = op;
8686
}
8787
}
88+
89+
void
90+
_Py_ResetRefChain(void)
91+
{
92+
refchain._ob_next = &refchain;
93+
refchain._ob_prev = &refchain;
94+
}
8895
#endif /* Py_TRACE_REFS */
8996

9097
#ifdef COUNT_ALLOCS

Python/pylifecycle.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,12 @@ Py_FinalizeEx(void)
735735
}
736736
#endif
737737

738+
_PyGC_Clear();
739+
740+
#ifdef Py_TRACE_REFS
741+
_Py_ResetRefChain();
742+
#endif
743+
738744
call_ll_exitfuncs();
739745
return status;
740746
}

0 commit comments

Comments
 (0)