From 0d97c7db960aa058b7cb1c60aea839dbc3d161e9 Mon Sep 17 00:00:00 2001 From: Simon King Date: Sun, 20 Sep 2015 09:05:00 +0200 Subject: [PATCH 1/3] Make garbage collection deterministic during a test against a memory leak --- src/sage/categories/fields.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/sage/categories/fields.py b/src/sage/categories/fields.py index 054b1d194d6..795f5275b0a 100644 --- a/src/sage/categories/fields.py +++ b/src/sage/categories/fields.py @@ -91,15 +91,20 @@ def __contains__(self, x): sage: GR in Fields() True - The following tests against a memory leak fixed in :trac:`13370`:: + The following tests against a memory leak fixed in :trac:`13370`. In order + to prevent non-deterministic deallocation of fields that have been created + in other doctests, :trac:`19244` disables garbage collection until we really + want it to happen. :: sage: import gc sage: _ = gc.collect() + sage: gc.disable() sage: n = len([X for X in gc.get_objects() if isinstance(X, sage.rings.finite_rings.integer_mod_ring.IntegerModRing_generic)]) sage: for i in prime_range(100): ....: R = ZZ.quotient(i) ....: t = R in Fields() sage: del R + sage: gc.enable() sage: _ = gc.collect() sage: len([X for X in gc.get_objects() if isinstance(X, sage.rings.finite_rings.integer_mod_ring.IntegerModRing_generic)]) - n 0 From 65ece650612dfb15bd1139245f9ac72bd94c9b68 Mon Sep 17 00:00:00 2001 From: Simon King Date: Sun, 20 Sep 2015 09:45:13 +0200 Subject: [PATCH 2/3] A different approach to make the test deterministic --- src/sage/categories/fields.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/sage/categories/fields.py b/src/sage/categories/fields.py index 795f5275b0a..a57a52e1504 100644 --- a/src/sage/categories/fields.py +++ b/src/sage/categories/fields.py @@ -93,18 +93,19 @@ def __contains__(self, x): The following tests against a memory leak fixed in :trac:`13370`. In order to prevent non-deterministic deallocation of fields that have been created - in other doctests, :trac:`19244` disables garbage collection until we really - want it to happen. :: + in other doctests, we introduced a strong reference to all previously created + uncollected objects in :trac:`19244`. :: sage: import gc sage: _ = gc.collect() - sage: gc.disable() - sage: n = len([X for X in gc.get_objects() if isinstance(X, sage.rings.finite_rings.integer_mod_ring.IntegerModRing_generic)]) + sage: permstore = [X for X in gc.get_objects() if isinstance(X, sage.rings.finite_rings.integer_mod_ring.IntegerModRing_generic)] + sage: n = len(permstore) sage: for i in prime_range(100): ....: R = ZZ.quotient(i) ....: t = R in Fields() + sage: len([X for X in gc.get_objects() if isinstance(X, sage.rings.finite_rings.integer_mod_ring.IntegerModRing_generic)]) > n + True sage: del R - sage: gc.enable() sage: _ = gc.collect() sage: len([X for X in gc.get_objects() if isinstance(X, sage.rings.finite_rings.integer_mod_ring.IntegerModRing_generic)]) - n 0 From c289fb06c7788a935a1ee9a4852fa9bcc662676c Mon Sep 17 00:00:00 2001 From: Simon King Date: Sun, 20 Sep 2015 16:39:27 +0200 Subject: [PATCH 3/3] Explain rationale of the doctest modification --- src/sage/categories/fields.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/sage/categories/fields.py b/src/sage/categories/fields.py index a57a52e1504..9b941563492 100644 --- a/src/sage/categories/fields.py +++ b/src/sage/categories/fields.py @@ -103,8 +103,15 @@ def __contains__(self, x): sage: for i in prime_range(100): ....: R = ZZ.quotient(i) ....: t = R in Fields() + + First, we show that there are now more quotient rings in cache than before:: + sage: len([X for X in gc.get_objects() if isinstance(X, sage.rings.finite_rings.integer_mod_ring.IntegerModRing_generic)]) > n True + + When we delete the last quotient ring created in the loop and then do a garbage + collection, all newly created rings vanish:: + sage: del R sage: _ = gc.collect() sage: len([X for X in gc.get_objects() if isinstance(X, sage.rings.finite_rings.integer_mod_ring.IntegerModRing_generic)]) - n