Opened 8 years ago

Last modified 7 years ago

#12215 closed defect

Memleak in UniqueRepresentation, @cached_method — at Version 2

Reported by: vbraun Owned by: rlm
Priority: major Milestone: sage-5.7
Component: memleak Keywords: UniqueRepresentation cached_method caching
Cc: SimonKing, jdemeyer, mhansen, vbraun, jpflori Merged in:
Authors: Reviewers:
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Description (last modified by SimonKing)

The documentation says that UniqueRepresentation? uses weak refs, but this was switched over to the @cached_method decorator. The latter does currently use strong references, so unused unique parents stay in memory forever:

import sage.structure.unique_representation
len(sage.structure.unique_representation.UniqueRepresentation.__classcall__.cache)

for i in range(2,1000):
    ring = ZZ.quotient(ZZ(i))
    vectorspace = ring^2

import gc
gc.collect()
len(sage.structure.unique_representation.UniqueRepresentation.__classcall__.cache)

Related tickets:

  • #11521 (needs review, introducing weak references for caching homsets), and
  • #715 (needs a lot of work, eventually aiming at using weak references for caching coerce maps).
  • #5970 (the polynomial rings cache use strong references)

Further notes:

  • not everything in Python can be weakref'ed, for example None cannot.
  • some results that are expensive to compute should not just be cached by a weak reference. Perhaps there is place for a permanent cache, or maybe some minimal age before garbage collecting it.

Change History (2)

comment:1 Changed 8 years ago by vbraun

  • Cc SimonKing added

comment:2 Changed 8 years ago by SimonKing

  • Description modified (diff)
Note: See TracTickets for help on using tickets.