#12215: Memleak in UniqueRepresentation, @cached_method
--------------------------------------------------------------+-------------
       Reporter:  vbraun                                      |         Owner:  
                
           Type:  defect                                      |        Status:  
needs_review    
       Priority:  major                                       |     Milestone:  
sage-5.3        
      Component:  memleak                                     |    Resolution:  
                
       Keywords:  UniqueRepresentation cached_method caching  |   Work issues:  
Rebase wrt #7980
Report Upstream:  N/A                                         |     Reviewers:  
                
        Authors:  Simon King                                  |     Merged in:  
                
   Dependencies:  #11115 #11900 #12645 #11599 #12808 #7980    |      Stopgaps:  
                
--------------------------------------------------------------+-------------
Changes (by SimonKing):

  * status:  needs_work => needs_review


Old description:

> 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 review, using weak references for caching coerce maps).
>   * #5970 (the polynomial rings cache use strong references, which may
> now be a duplicate, as I introduce the weak cache in #715)
>
> 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.
>
> __Apply__
>
>  * [attachment:trac12215_weak_cached_function.patch]
>  * [attachment:trac12215_segfault_fixes.patch]

New description:

 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 review, using weak references for caching coerce maps).
   * #5970 (the polynomial rings cache use strong references, which may now
 be a duplicate, as I introduce the weak cache in #715)

 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.

 __Apply__

  * [attachment:trac12215_weak_cached_function-sk.patch]
  * [attachment:trac12215_segfault_fixes.patch]

--

Comment:

 The patches are now rebased rel #7980. I have not been able to replace my
 first patch, because trac believes that it isn't my patch but Nicolas'...

 Apply trac12215_weak_cached_function-sk.patch
 trac12215_segfault_fixes.patch

-- 
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/12215#comment:95>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica, 
and MATLAB

-- 
You received this message because you are subscribed to the Google Groups 
"sage-trac" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sage-trac?hl=en.

Reply via email to