#14711: Memleak when creating QuadraticField
-------------------------------------------------+-------------------------
       Reporter:  jpflori                        |        Owner:
           Type:  defect                         |  davidloeffler
       Priority:  critical                       |       Status:  new
      Component:  number fields                  |    Milestone:  sage-5.13
       Keywords:  memleak, number field,         |   Resolution:
  QuadraticField                                 |    Merged in:
        Authors:                                 |    Reviewers:
Report Upstream:  N/A                            |  Work issues:
         Branch:  u/SimonKing/ticket/14711       |       Commit:
   Dependencies:                                 |     Stopgaps:
-------------------------------------------------+-------------------------

Comment (by SimonKing):

 To summarise the requirements found in our discussion for a stable and
 memory
 friendly coercion system:

 - Maps registered by `P.register_coercion(mor)` are the backbone of the
 discover_coercion algorithm. Hence, they need to be kept healthy as long
 as `P` lives.
 - If P has a coerce embedding then this embedding needs to be kept healthy
 as long as P lives.
 - If the coercion system finds a coercion from P to Q different from the
 two preceding cases (i.e., by transitive closure), then P must not prevent
 Q and the coercion map from garbage collection, and Q must not prevent P
 and the coercion map from garbage collection. But the coercion map needs
 to stay healthy as long as both P and Q are alive. In particular, take
 care of composed maps and actions.

 To summarise how I think we can meet these requirements:

 - `P.register_coercion(mor)` keeps a strong reference to `mor.domain()` in
 a new attribute `P._registered_domains` (which is a list). All other maps
 in the coercion system will be weakened.
 - Homsets keep strong references to domain and codomain.
 - "Weakening a map" means to use a weak reference to the domain and a
 removed reference to the homset. But we will keep a strong reference to
 the codomain. Consequence: (1) In a composed map or action, the "middle
 parents" will always stay alive as long as the composed map lives. (2)
 Embeddings do what we required above: The embedding keeps the codomain
 alive.
 - We store the map phi found by `P.discover_coercion(Q)` in
 `P._coerce_from_hash`, which is a monodict. In particular, if Q will be
 garbage collected, then phi is immediately removed from the cache, and
 thus the strong reference of phi to the codomain P will not prevent P from
 garbage collection. And if P will be garbage collected, then the whole
 cache including phi is removed. However, if both P and Q live, then phi
 will stay in `P._coerce_from_hash[Q]`, and it will stay healthy, because
 its domain Q is still alive (and hence the weak reference is still ok).

 I think this model makes sense.

 And concerning a safe user interface: Perhaps we can do without. Namely,
 we could make it so that the string representation of a weakened map will
 consist of a warning, suggesting to use a copy. In this way, it would be
 sufficiently likely that the user wouldn't have too many bad surprises.

--
Ticket URL: <http://trac.sagemath.org/ticket/14711#comment:82>
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 unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-trac.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to