On Friday, December 15, 2017 at 2:51:37 PM UTC, Marco Caselli wrote: > > Thank you very much for your prompt replies. I was sure that range(n) > creates an iterator instead of the list itself, my bad. > In any case, even if the function is creating this list, why is it still > stored in memory after the function terminates? It is a local variable, and > is not returned so it should be erased from the RAM. >
it need not happen, unless you explicitly trigger garbage collection import gc gc.collect() see https://docs.python.org/2/library/gc.html for details > > > In my original code, I iterate ternary polynomials over a finite field K > and factor them. I am constructing them from the vector of the > coefficients, which is an element of K^n. In order to create the vector of > coefficients, I iterate over K^n (which I think should support iteration, > as K does, but after the mistake on range I am no more so confident). Here > an example: (actually it behaves badly even with binary polynomials): > > def ex_f(q): >> >> K = GF(q) >> >> P.<x,y,z> = PolynomialRing(K,3) >> >> monomials=[x,y,z,x^2,y^2,z^2] >> >> for v in K^6: >> >> f=1+sum([v*m for (v,m) in zip (v,monomials)]) >> >> f.factor() >> >> > With 7 as input it uses 283MB. By uses, as above, I mean the output of the > function "check_memory" which should return the difference of the amounts > of RAM dedicated at the Sage process before and after launching ex_f(7). > Running a similar function with univariate polynomials does not require > memory, so I guess should not be an iterator-related problem. > > Il giorno venerdì 15 dicembre 2017 12:27:22 UTC, Dima Pasechnik ha scritto: >> >> >> >> On Friday, December 15, 2017 at 12:16:04 PM UTC, vdelecroix wrote: >>> >>> You can fill your memory with something simpler >>> >>> sage: l = range(10**9) >>> >>> As far as I can see it has nothing to do with Sage or loops. In Python2 >>> the range functions constructs a list. And in the above example, the >>> list is huge. >>> >> >> indeed. In order to avoid this, use xrange() instead of range() >> (well, this is not going to make your code Python3-proof, but range() in >> Python3 >> is what xrange() is in Python3; see e.g. >> https://stackoverflow.com/a/15015199/557937 >> on how to make it portable) >> >> >> >> >>> >>> Vincent >>> >>> On 15/12/2017 12:06, Marco Caselli wrote: >>> > Hello there, >>> > I am currently having trouble in memory management, I wrote a code >>> that was >>> > not supposed to store anything but actually it was using a massive >>> amount >>> > of memory. At a first glance, I thought it was related to some >>> function in >>> > my code, such as factor() for polynomials in a multivariate ring over >>> a >>> > finite field. I dug a bit deeper and the problem seems even at a >>> lower >>> > level: >>> > >>> > def check_memory(k): >>> >> a=get_memory_usage() >>> >> silly_function(k) >>> >> return get_memory_usage()-a >>> >> >>> >> def silly_function(k): >>> >> for i in range(10^k): >>> >> 2+2 >>> > >>> > >>> > Notice that silly_function does not store neither return anything, so >>> the >>> > memory usage should be zero. >>> > check_memory(9) returned 23446.37109375, this amount is in MB, so >>> those are >>> > 23GB. This information is consistent with the data from top. At a >>> second >>> > run it returned just 972.62109375, which is still a lot. >>> > >>> >> -- You received this message because you are subscribed to the Google Groups "sage-support" group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-support+unsubscr...@googlegroups.com. To post to this group, send email to sage-support@googlegroups.com. Visit this group at https://groups.google.com/group/sage-support. For more options, visit https://groups.google.com/d/optout.