diff --git a/Include/internal/pycore_gc.h b/Include/internal/pycore_gc.h index a6519aa086309d..99fb7719ac3cbf 100644 --- a/Include/internal/pycore_gc.h +++ b/Include/internal/pycore_gc.h @@ -240,6 +240,7 @@ static inline void _PyObject_GC_TRACK( PyInterpreterState *interp = _PyInterpreterState_GET(); PyGC_Head *generation0 = &interp->gc.young.head; + interp->gc.young.count++; /* number of tracked GC objects */ PyGC_Head *last = (PyGC_Head*)(generation0->_gc_prev); _PyGCHead_SET_NEXT(last, gc); _PyGCHead_SET_PREV(gc, last); @@ -280,6 +281,10 @@ static inline void _PyObject_GC_UNTRACK( _PyGCHead_SET_PREV(next, prev); gc->_gc_next = 0; gc->_gc_prev &= _PyGC_PREV_MASK_FINALIZED; + PyInterpreterState *interp = _PyInterpreterState_GET(); + if (interp->gc.young.count > 0) { + interp->gc.young.count--; + } #endif } diff --git a/Python/gc.c b/Python/gc.c index 79c7476f4a9a74..ad4edcc27a8114 100644 --- a/Python/gc.c +++ b/Python/gc.c @@ -2296,7 +2296,6 @@ _PyObject_GC_Link(PyObject *op) GCState *gcstate = &tstate->interp->gc; gc->_gc_next = 0; gc->_gc_prev = 0; - gcstate->young.count++; /* number of allocated GC objects */ gcstate->heap_size++; if (gcstate->young.count > gcstate->young.threshold && gcstate->enabled && @@ -2428,9 +2427,6 @@ PyObject_GC_Del(void *op) #endif } GCState *gcstate = get_gc_state(); - if (gcstate->young.count > 0) { - gcstate->young.count--; - } gcstate->heap_size--; PyObject_Free(((char *)op)-presize); }