Forum: Ruby Memory Leak (again)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
81cf8dab4b4af8aa3148c28421afd845?d=identicon&s=25 Horacio Sanson (Guest)
on 2006-01-19 12:34
(Received via mailing list)
There is a little memory leak in my script I am not able to spot. I have
tried
everything I found about detecting leaks but no luck.

I first tried all the recomendations found here

http://theexciter.com/articles/finding-leaking-rub...

I can see the number of objects but the total number of objects does not
increment indefinitely but the script keeps consuming memory.

I tried this code:

##################################33
Entry = Struct.new( "MEntry", :c, :mem )
class MEntry; def to_s() "#{c} : #{mem}"; end; end

GroupEntry = Struct.new( "GroupEntry", :c, :mem, :total )
class GroupEntry; def to_s() "#{c} x#{total}(#{mem})"; end; end

def profile_mem
    groups = {}
    ObjectSpace.each_object { |x|
            e = nil
            begin
                e = MEntry.new( x.class, Marshal::dump(x).size )
            rescue TypeError # undumpable
                e = MEntry.new( x.class, 0 )
            end
            if groups.has_key? e.c
                groups[e.c].mem += e.mem
                groups[e.c].total += 1
            else
                groups[e.c] = GroupEntry.new( e.c, e.mem, 1 )
            end
    }
    File.open( "mem_log", "a+" ) { |file|
        total = 0
        groups.to_a.sort_by { |e| e[1].mem }.each { |e|
            file << "#{e[1]} ";
            total += e[1].mem
        }
        file << "TOTAL == #{total}"
        file << "\n"
    }
end


This code gives a list of all objects in ObjectSpace and their memory
usage.
Also I log the total memory usage.

The log indicates that my scripts uses between 3 to 4 megs of memory
constantly, that is, it fluctuates between these values but the windows
manager indicates that the memory increase all the time and never goes
down.

Running the script for a day consumes all 2G ram of my machine and
causes the
script to bail out.

So are there any other tips on how to detect memory leaks??  Ruby Memory
Validator is only available to accepted beta testers so is not an option
for
me.

I use Windows XP pro with the One-Click Ruby installer (1.8.2) and
ActiveRecord (1.13.2).

Any ideas are welcome...

regards,
Horacio
Fe9b2d0628c0943af374b2fe5b320a82?d=identicon&s=25 Eero Saynatkari (rue)
on 2006-01-20 03:02
Horacio Sanson wrote:
> There is a little memory leak in my script I am not able to spot. I have
> tried
> everything I found about detecting leaks but no luck.
>
> I first tried all the recomendations found here
>
> http://theexciter.com/articles/finding-leaking-rub...
>
> I can see the number of objects but the total number of objects does not
> increment indefinitely but the script keeps consuming memory.
>
> I tried this code:
>
> ##################################33
> Entry = Struct.new( "MEntry", :c, :mem )
> class MEntry; def to_s() "#{c} : #{mem}"; end; end
>
> GroupEntry = Struct.new( "GroupEntry", :c, :mem, :total )
> class GroupEntry; def to_s() "#{c} x#{total}(#{mem})"; end; end
>
> def profile_mem
>     groups = {}
>     ObjectSpace.each_object { |x|
>             e = nil
>             begin
>                 e = MEntry.new( x.class, Marshal::dump(x).size )
>             rescue TypeError # undumpable
>                 e = MEntry.new( x.class, 0 )
>             end
>             if groups.has_key? e.c
>                 groups[e.c].mem += e.mem
>                 groups[e.c].total += 1
>             else
>                 groups[e.c] = GroupEntry.new( e.c, e.mem, 1 )
>             end
>     }
>     File.open( "mem_log", "a+" ) { |file|
>         total = 0
>         groups.to_a.sort_by { |e| e[1].mem }.each { |e|
>             file << "#{e[1]} ";
>             total += e[1].mem
>         }
>         file << "TOTAL == #{total}"
>         file << "\n"
>     }
> end
>
>
> This code gives a list of all objects in ObjectSpace and their memory
> usage.
> Also I log the total memory usage.
>
> The log indicates that my scripts uses between 3 to 4 megs of memory
> constantly, that is, it fluctuates between these values but the windows
> manager indicates that the memory increase all the time and never goes
> down.

The above logger seems to be reasonably good (although it
will not count Symbols and Fixnums) so there is a chance
this might be caused by your platform.

> Running the script for a day consumes all 2G ram of my machine and
> causes the
> script to bail out.
>
> So are there any other tips on how to detect memory leaks??  Ruby Memory
> Validator is only available to accepted beta testers so is not an option
> for
> me.
>
> I use Windows XP pro with the One-Click Ruby installer (1.8.2) and
> ActiveRecord (1.13.2).
>
> Any ideas are welcome...

If you can publish the script or a minimal version exhibiting
the same behaviour, we can try to troubleshoot it (or just run
it on different platforms in case the problem is caused by WinXP).

> regards,
> Horacio


E
This topic is locked and can not be replied to.