# Memory Leak (again)

**URL:** <https://www.ruby-forum.com/t/memory-leak-again/52816>\
**Category:** Ruby\
**Created:** [January 19, 2006, 12:34pm UTC](https://www.ruby-forum.com/t/memory-leak-again/52816 "2006-01-19T12:34:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Horacio\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/97f17d/32.png) [@Horacio\_S](https://www.ruby-forum.com/u/Horacio_S)\
**Post date:** [January 19, 2006, 12:34pm UTC](https://www.ruby-forum.com/t/memory-leak-again/52816/1 "2006-01-19T12:34:21Z")

</div>

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-ruby-objects?commented=1#c000092](http://theexciter.com/articles/finding-leaking-ruby-objects?commented=1#c000092)

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

---

<div class="post-metadata">

**Author:** ![Horacio\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/97f17d/32.png) [@Horacio\_S](https://www.ruby-forum.com/u/Horacio_S)\
**Post date:** [January 20, 2006, 3:02am UTC](https://www.ruby-forum.com/t/memory-leak-again/52816/2 "2006-01-20T03:02:34Z")

</div>

Horacio S. 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-ruby-objects?commented=1#c000092](http://theexciter.com/articles/finding-leaking-ruby-objects?commented=1#c000092)
> 
> 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
