From: Ritwik Banerjee Date: 2010-10-30T19:12:09+09:00 Subject: Re: Ruby 1.9.1 linear memory increase in a simple for loop. Calamitas wrote in post #958065: > On Fri, Oct 29, 2010 at 3:17 PM, Ritwik Banerjee > wrote: >> 1, 1],[0, 1, 1, 1, 1, 1, 1, 1],[0, 1, 0, 1, 1, 0, 1, 1]] >> # ObjectSpace.garbage_collect >> codes with lots of references, and in those cases the memory rise is >> exponential. In the above loop, invoking garbage collection manually >> (it's commented out in the snippet above) slows the memory increase, but >> only by a constant factor. >> For example, if the loop used up 30 kB in 100 iterations originally, it >> uses up 30kB in 500 iterations after invoking garbage collection >> manually. >> >> Understanding this is critical for me since I am dealing with matrices >> of enormous sizes, so even a small memory leak can jeopardize my >> project. > > You are calculating n=m**i (m to the i-th power) in subsequent > iterations. The entries in n grow exponentially with rising i. Storage > size of integers is logarithmic (in the Bignum range), meaning that > storage size for n increases linearly. That's why you see memory > increase linearly. > > Peter I had thought of that too. But, as you can see, in this particular example I only raise it to the 100th power with an initial binary matrix. The numbers don't get big enough to explain the kind of memory increase I see. Also, an increase in the size of integers should not increase the number of objects in classes like T_ARRAY, T_HASH, T_STRUCT, right? Another point: If I use a matrix from my actual project, however, even going up to 25 iterations consumes almost my entire 2GB RAM. There too, the initial matrix consists only of 0/1 entries. In 25 iterations, I don't think it should be anywhere near 2GB! Even so, do you think it is a good idea to cast into float at every iteration to see if that helps? I've avoided floats because of problems such as (0.3 - 0.2 == 0.1) returns false. -- Posted via http://www.ruby-forum.com/.