-
-
Notifications
You must be signed in to change notification settings - Fork 481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use weak references to cache homsets #11521
Comments
comment:1
After looking at #10548, I might have a better idea of the culprit:
|
comment:2
So this could be just #715 . |
comment:3
This is definitely #715. Resetting the coercion cache and calling gc.collect() deletes the cached elements. I guess weak refs should be used in the different TripleDict objects of CoercionModel_cache_maps. So this should be closed as duplicate/won't fix. |
comment:5
Jean-Pierre, why did you change the status to "needs review"? There is no patch to review. Also, how to you reset the coercion cache? I would be interested if you have a workaround for the
Paul |
comment:6
As far as I'm concerned, this is nothing but a concrete example of the vague #715. I seem to remember that I had some workarounds to delete some parts of the cache (so that I could perform my computations), but not all of them. |
comment:7
for the computations I need to perform, which need to create about 200000 prime fields, this I would be satisfied if I had a magic command to type to explicitly free the memory used by Paul |
comment:8
I'm having a look at your problem right now. Here are some hints to study the problem, mostly stolen from #10548. I put it here for the record, and so that i can go faster next time. First, if I only create the finite fields and do nothing with them, I do not seem to get a memleak. Some fields are not garbage collected immediately, but calling gc.collect() does the trick.
Of course I guess you actually do something with your finite fields. So here is a small example causing the fields to stay cached.
The zero here is bad and indeed
If you want to know where it comes from you can use the objgraph python module (on my debian I just installed the python-objgraph package, updated sys.path in Sage to include '/usr/lib/python2.6/dist-packages')
And look at the png or use
So the culprit is "_cache" in sage.categories.homset which has a direct reference to the finite fields in its keys. The clean solution woul be to used weakref in the keys (let's say something as WeakKeyDirectory in python). However, resetting cache should be a (potentially partial) workaround (and could kill your Sage?).
It also seems to be enough if I do "a = k.random_element(); a = a+a" in the for loop, but not if I add "a = 47*a+3". I'm currently investigating that last case. |
comment:9
For info, using "k(47)*a+k(3)" is solved, so the problem left really comes from coercion and action of integers.
does not help. |
comment:10
First, the second example above is missing the line "k(1);" in the for loop, otherwise it does nothing more than the first example. Second, I guess the remaining references to the finite fields are in the different lists and dictionnaries of the integer ring named _coerce_from_list, _convert_from_list etc. You can not directly access them from Python level, but there a function _introspect_coerce() (defined in parent.pyx) which returns them. |
comment:11
In fact, everything is in _*_hash. And to conclude, I'd say that right now you can not directly delete entries in this dictionaries from the Python level. So for minimum changes, you should eitheir avoid coercion, or make the dictionaries "cdef public" in parent.pxd to be able to explicitely delete every created entries (be aware that it could be written in different places for example in ZZ but also in QQ and CC and I don't know where...). And I could also have missed other dictionaries used by Sage. |
comment:12
Jean-Pierre, I cannot reproduce that:
Paul |
comment:13
Did you "apt-get install python-objgraph" on your system? If yes, is it a version for python 2.6 ? |
comment:14
The path I gave above might also be different on your system... As the package manager. |
comment:15
Any progress on your side? If you found any other dicitonaries leading to cahing problems, it would be great to mention them here for the record. Hence the day someone will finally decide to tackle ticket #715, it will speed up the search of the culprit. |
comment:16
Replying to @jpflori:
no time so far. I will look at this during the SageFlint days in December, unless someone else has Paul |
comment:17
Replying to @jpflori:
If you think it should be closed, then I think you should set the milestone to duplicate/wontfix. Otherwise, it is probably better to change the status back to 'new'. |
comment:18
with Sage 4.7.2 I get the following:
thus whenever we use the finite field we get a memleak. Paul |
comment:19
I confirm (cf comment [comment:8]) that if I comment out the line
in categories/homset.py, then the memory leak from comment [comment:18] disappears. Paul |
comment:20
I think we have a different problem here. The finite fields themselves should be cached. The cache (see Also, there are special methods However, it seems that all elements of a finite field are cached - and that's bad!
I see absolutely no reason why all Fortunately, we have no caching for larger finite fields:
|
comment:21
In the following example, there is no memory leak that would be caused by the
However, when you do a conversion
The homset cache does use weak references. Hence, it is surprising that the unused stuff can not be garbage collected. Apparently there is some direct reference involved at some point. I am very stronglyagainst removing the cache of However, since we actually have coercion maps (not just any odd map), I expect that the direct reference comes from the coercion model. I suggest to look into the coercion code and use weak references there. By the way, I don't know why the status is "needs review". I think it clearly is "needs work". |
comment:185
Replying to @jdemeyer:
attachment: trac_11521_doctestfix.patch looks fine to me. I'll try to build the latest Sage prerelease on bsd.math with these changes and do tests. |
Changed reviewer from Jean-Pierre Flori, Nils Bruin to Jean-Pierre Flori, Nils Bruin, Simon King |
Changed author from Simon King to Simon King, Nils Bruin |
comment:186
It works! All tests pass on bsd.math, and I also did The new patch looks fine. Hence, it is a positive review. |
comment:187
I confirm that doctests pass. |
Merged: sage-5.5.beta0 |
Changed merged from sage-5.5.beta0 to none |
comment:189
Sorry to bring bad news, but a trial sage-5.5.beta1 caused a Segmentation Fault in |
Merged: sage-5.5.beta0 |
Originally, this ticket was about the following memory leak when computing with elliptic curves:
This example is in fact solved by #715. However, while working on that ticket, another leak has been found, namely
So, I suggest to start with #715 and solve the second memory leak on top of it. It seems that a strong cache for homsets is to blame. I suggest to use the weak
TripleDict
instead, which were introduced in #715.To be merged with #715. Apply
Depends on #12969
Depends on #715
Dependencies: #12969; to be merged with #715
CC: @jpflori @nthiery
Component: coercion
Keywords: sd35
Author: Simon King, Nils Bruin
Reviewer: Jean-Pierre Flori, Nils Bruin, Simon King
Merged: sage-5.5.beta0
Issue created by migration from https://trac.sagemath.org/ticket/11521
The text was updated successfully, but these errors were encountered: