File tree 4 files changed +32
-1
lines changed
4 files changed +32
-1
lines changed Original file line number Diff line number Diff line change
1
+ import gc
1
2
import numpy as np
2
3
import pandas .util .testing as tm
3
4
from pandas import (
@@ -225,4 +226,21 @@ def time_intersection_both_duplicate(self, N):
225
226
self .intv .intersection (self .intv2 )
226
227
227
228
229
+ class GC :
230
+ params = [1 , 2 , 5 ]
231
+
232
+ def create_use_drop (self ):
233
+ idx = Index (list (range (1000 * 1000 )))
234
+ idx ._engine
235
+
236
+ def peakmem_gc_instances (self , N ):
237
+ try :
238
+ gc .disable ()
239
+
240
+ for _ in range (N ):
241
+ self .create_use_drop ()
242
+ finally :
243
+ gc .enable ()
244
+
245
+
228
246
from .pandas_vb_common import setup # noqa: F401
Original file line number Diff line number Diff line change @@ -83,6 +83,7 @@ Indexing
83
83
^^^^^^^^
84
84
85
85
- Bug in partial-string indexing returning a NumPy array rather than a ``Series `` when indexing with a scalar like ``.loc['2015'] `` (:issue: `27516 `)
86
+ - Break reference cycle involving :class: `Index ` to allow garbage collection of :class: `Index ` objects without running the GC. (:issue: `27585 `)
86
87
-
87
88
-
88
89
Original file line number Diff line number Diff line change @@ -691,7 +691,11 @@ def _cleanup(self):
691
691
@cache_readonly
692
692
def _engine (self ):
693
693
# property, for now, slow to look up
694
- return self ._engine_type (lambda : self ._ndarray_values , len (self ))
694
+
695
+ # to avoid a refernce cycle, bind `_ndarray_values` to a local variable, so
696
+ # `self` is not passed into the lambda.
697
+ _ndarray_values = self ._ndarray_values
698
+ return self ._engine_type (lambda : _ndarray_values , len (self ))
695
699
696
700
# --------------------------------------------------------------------
697
701
# Array-Like Methods
Original file line number Diff line number Diff line change 1
1
from collections import defaultdict
2
2
from datetime import datetime , timedelta
3
+ import gc
3
4
from io import StringIO
4
5
import math
5
6
import operator
@@ -2424,6 +2425,13 @@ def test_deprecated_contains(self):
2424
2425
with tm .assert_produces_warning (FutureWarning ):
2425
2426
index .contains (1 )
2426
2427
2428
+ def test_engine_reference_cycle (self ):
2429
+ # https://github.com/pandas-dev/pandas/issues/27585
2430
+ index = pd .Index ([1 , 2 , 3 ])
2431
+ nrefs_pre = len (gc .get_referrers (index ))
2432
+ index ._engine
2433
+ assert len (gc .get_referrers (index )) == nrefs_pre
2434
+
2427
2435
2428
2436
class TestMixedIntIndex (Base ):
2429
2437
# Mostly the tests from common.py for which the results differ
You can’t perform that action at this time.
0 commit comments