# Newbie Mem Leak Issue

**URL:** <https://rubytalk.org/t/newbie-mem-leak-issue/44683>\
**Category:** ruby-talk\
**Created:** [21 February 2008 21:56 UTC](https://rubytalk.org/t/newbie-mem-leak-issue/44683 "2008-02-21T21:56:34Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Barr\_Keith](https://avatars.discourse-cdn.com/v4/letter/b/b5e925/32.png) [@Barr\_Keith](https://rubytalk.org/u/Barr_Keith)\
**Post date:** [21 February 2008 21:56 UTC](https://rubytalk.org/t/newbie-mem-leak-issue/44683/1 "2008-02-21T21:56:34Z")

</div>

I am fairly new to Ruby and a program that I have created seems to have  
a memory leak. I have generated a small program which does the basics  
of my program that leaks, and the test program also seems to leak. Can  
anyone spot the design flaw?

Any help you can provide is greatly appreciated.

Thanks,  
Keith

> **···**
>
> =================================  
> class LeakTest  
> &nbsp;&nbsp;def initialize  
> &nbsp;&nbsp;&nbsp;&nbsp;print("initialized\n")  
> &nbsp;&nbsp;&nbsp;&nbsp;# fill up the standard 8M of space so the leak becomes apparent  
> faster  
> &nbsp;&nbsp;&nbsp;&nbsp;str = "0123456789 0123456789 0123456789 0123456789 0123456789  
> 0123456789 0123456789 0123456789 0123456789 0123456789 "  
> &nbsp;&nbsp;&nbsp;&nbsp;@huge\_mem = Array.new(1000000, str)  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def get\_files(pathname)  
> &nbsp;&nbsp;&nbsp;&nbsp;listing = Array.new
> 
> &nbsp;&nbsp;&nbsp;&nbsp;Dir.glob("#{pathname}/\*",0) do |f|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# add the files  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;listing \<\< f  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# now add directories.  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(File.directory?(f))  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;returnlist = get\_files(f)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;returnlist.each {|filename| listing \<\< filename }  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;return listing  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;include GC  
> &nbsp;&nbsp;def cleanup  
> &nbsp;&nbsp;&nbsp;&nbsp;printf("cleaning up")  
> &nbsp;&nbsp;&nbsp;&nbsp;GC.start  
> &nbsp;&nbsp;&nbsp;&nbsp;5.times do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print"."  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sleep(1)  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;print"\n"  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def monitor  
> &nbsp;&nbsp;&nbsp;&nbsp;listing = get\_files("C:/Program Files")  
> &nbsp;&nbsp;&nbsp;&nbsp;printf("Found %d files\n", listing.length)  
> &nbsp;&nbsp;&nbsp;&nbsp;5.times do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print"."  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sleep(1)  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;print"\n"  
> &nbsp;&nbsp;end  
> end
> 
> # lt = LeakTest.new while true &nbsp;&nbsp;lt.monitor &nbsp;&nbsp;lt.cleanup end
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [21 February 2008 22:04 UTC](https://rubytalk.org/t/newbie-mem-leak-issue/44683/2 "2008-02-21T22:04:50Z")

</div>

Keith Barr wrote:

> &nbsp;&nbsp;&nbsp;&nbsp;str = "0123456789 0123456789 0123456789 0123456789 0123456789  
> 0123456789 0123456789 0123456789 0123456789 0123456789 "  
> &nbsp;&nbsp;&nbsp;&nbsp;@huge\_mem = Array.new(1000000, str)

That's an array of 1000000 references to the same string. Did you want lots of strings? This is the way to do that:

Array.new(1000000) {str}

------------------------------------------------------------- Array::new  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Array.new(size=0, obj=nil)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Array.new(array)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Array.new(size) {|index| block }

> **···**
>
> ------------------------------------------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Returns a new array. In the first form, the new array is empty. In  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the second it is created with \_size\_ copies of \_obj\_ (that is,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_size\_ references to the same \_obj\_).
> 
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [21 February 2008 22:51 UTC](https://rubytalk.org/t/newbie-mem-leak-issue/44683/3 "2008-02-21T22:51:57Z")

</div>

Keith Barr wrote:

> I am fairly new to Ruby and a program that I have created seems to have  
> a memory leak. I have generated a small program which does the basics  
> of my program that leaks, and the test program also seems to leak. Can  
> anyone spot the design flaw?
> 
> Any help you can provide is greatly appreciated.

Does not seem to leak with ruby-1.8.6p111 on linux. (I replaced "C:/Program Files" with the mount point of my ntfs partition.) It stays under 18Mb of VM.

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

---

<div class="post-metadata">

**Author:** ![Ron\_Fox](https://avatars.discourse-cdn.com/v4/letter/r/e8c25b/32.png) [@Ron\_Fox](https://rubytalk.org/u/Ron_Fox)\
**Post date:** [22 February 2008 10:59 UTC](https://rubytalk.org/t/newbie-mem-leak-issue/44683/4 "2008-02-22T10:59:56Z")

</div>

Dunno about the memory leak but are you aware that in get\_files, you only go one level of sub-directory deep, rather than down the whole depth of the directory tree e.g. (with apologies to the excellent  
Xwin32 product):

if I have:  
c:\program files\  
&nbsp;&nbsp;&nbsp;&nbsp;+--\> Starnet  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+----\>xwin32  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+-----\> xwin32.exe

I'll never see xwin32.exe in the list of files?  
If you want full depth my suggestion is that you  
recursively call get\_files (when it encounters a directory)  
and then append the results it returns to the original result.

Ron.

Keith Barr wrote:

> **···**
>
> > I am fairly new to Ruby and a program that I have created seems to have  
> > a memory leak. I have generated a small program which does the basics  
> > of my program that leaks, and the test program also seems to leak. Can  
> > anyone spot the design flaw?
> > 
> > Any help you can provide is greatly appreciated.
> > 
> > Thanks,  
> > Keith
> > 
> > =================================  
> > class LeakTest  
> > &nbsp;&nbsp;def initialize  
> > &nbsp;&nbsp;&nbsp;&nbsp;print("initialized\n")  
> > &nbsp;&nbsp;&nbsp;&nbsp;# fill up the standard 8M of space so the leak becomes apparent  
> > faster  
> > &nbsp;&nbsp;&nbsp;&nbsp;str = "0123456789 0123456789 0123456789 0123456789 0123456789  
> > 0123456789 0123456789 0123456789 0123456789 0123456789 "  
> > &nbsp;&nbsp;&nbsp;&nbsp;@huge\_mem = Array.new(1000000, str)  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;def get\_files(pathname)  
> > &nbsp;&nbsp;&nbsp;&nbsp;listing = Array.new
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;Dir.glob("#{pathname}/\*",0) do |f|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# add the files  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;listing \<\< f  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# now add directories.  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(File.directory?(f))  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;returnlist = get\_files(f)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;returnlist.each {|filename| listing \<\< filename }  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;return listing  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;include GC  
> > &nbsp;&nbsp;def cleanup  
> > &nbsp;&nbsp;&nbsp;&nbsp;printf("cleaning up")  
> > &nbsp;&nbsp;&nbsp;&nbsp;GC.start  
> > &nbsp;&nbsp;&nbsp;&nbsp;5.times do  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print"."  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sleep(1)  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;print"\n"  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;def monitor  
> > &nbsp;&nbsp;&nbsp;&nbsp;listing = get\_files("C:/Program Files")  
> > &nbsp;&nbsp;&nbsp;&nbsp;printf("Found %d files\n", listing.length)  
> > &nbsp;&nbsp;&nbsp;&nbsp;5.times do  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print"."  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sleep(1)  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;print"\n"  
> > &nbsp;&nbsp;end  
> > end
> > 
> > # lt = LeakTest.new while true &nbsp;&nbsp;lt.monitor &nbsp;&nbsp;lt.cleanup end

---

<div class="post-metadata">

**Author:** ![Barr\_Keith](https://avatars.discourse-cdn.com/v4/letter/b/b5e925/32.png) [@Barr\_Keith](https://rubytalk.org/u/Barr_Keith)\
**Post date:** [21 February 2008 22:16 UTC](https://rubytalk.org/t/newbie-mem-leak-issue/44683/5 "2008-02-21T22:16:59Z")

</div>

Joel VanderWerf wrote:

> Keith Barr wrote:
> 
> > &nbsp;&nbsp;&nbsp;&nbsp;str = "0123456789 0123456789 0123456789 0123456789 0123456789  
> > 0123456789 0123456789 0123456789 0123456789 0123456789 "  
> > &nbsp;&nbsp;&nbsp;&nbsp;@huge\_mem = Array.new(1000000, str)
> 
> That's an array of 1000000 references to the same string. Did you want  
> lots of strings? This is the way to do that:
> 
> Array.new(1000000) {str}
> 
> ------------------------------------------------------------- Array::new  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Array.new(size=0, obj=nil)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Array.new(array)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Array.new(size) {|index| block }

oops, yeah, good catch.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Barr\_Keith](https://avatars.discourse-cdn.com/v4/letter/b/b5e925/32.png) [@Barr\_Keith](https://rubytalk.org/u/Barr_Keith)\
**Post date:** [22 February 2008 03:54 UTC](https://rubytalk.org/t/newbie-mem-leak-issue/44683/6 "2008-02-22T03:54:45Z")

</div>

Joel VanderWerf wrote:

> Keith Barr wrote:
> 
> > I am fairly new to Ruby and a program that I have created seems to have  
> > a memory leak. I have generated a small program which does the basics  
> > of my program that leaks, and the test program also seems to leak. Can  
> > anyone spot the design flaw?
> > 
> > Any help you can provide is greatly appreciated.
> 
> Does not seem to leak with ruby-1.8.6p111 on linux. (I replaced  
> "C:/Program Files" with the mount point of my ntfs partition.) It stays  
> under 18Mb of VM.

Interesting. I am running (obviously) on Windows and I also have a  
Linux box, both on patch 111, and both leaking. It isn't a quick leak,  
if you run for an a while it might show up. How long did you let it go?

If you don't ever see anything leaking, I wonder what could be different  
in our setup or builds.

I certainly don't see anything in there that looks like a memory leak,  
but it certainly grows over time on my boxes. Very difficult problem.

KB

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![John\_Woods](https://avatars.discourse-cdn.com/v4/letter/j/a88e4f/32.png) [@John\_Woods](https://rubytalk.org/u/John_Woods)\
**Post date:** [21 February 2008 22:36 UTC](https://rubytalk.org/t/newbie-mem-leak-issue/44683/7 "2008-02-21T22:36:53Z")

</div>

> > Array.new(1000000) {str}

Isn't the above still just an array of 1000000 refs to the same string? Perhaps you meant str.dup?

For example:

> str = "asdf"  
> puts Array.new(3){str}.collect{|s|s.object\_id}

-605717778  
# the above three object\_ids are the same

> puts Array.new(3){str.dup}.collect{|s|s.object\_id}

-605741458  
-605741468  
-605741478  
# the above three object\_ids are different

> **···**
>
> -----Original Message-----  
> From: Keith Barr  
> Sent: 02/21/2008 02:16 PM
> 
> > Joel VanderWerf wrote:
> > 
> > > Keith Barr wrote:
> > > 
> > > > &nbsp;&nbsp;&nbsp;&nbsp;str = "0123456789 0123456789 0123456789 0123456789 0123456789  
> > > > 0123456789 0123456789 0123456789 0123456789 0123456789 "  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;@huge\_mem = Array.new(1000000, str)
> > > 
> > > That's an array of 1000000 references to the same string. Did you want  
> > > lots of strings? This is the way to do that:
> > > 
> > > Array.new(1000000) {str}
> > > 
> > > ------------------------------------------------------------- Array::new  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Array.new(size=0, obj=nil)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Array.new(array)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Array.new(size) {|index| block }
> > 
> > oops, yeah, good catch.

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [22 February 2008 05:43 UTC](https://rubytalk.org/t/newbie-mem-leak-issue/44683/8 "2008-02-22T05:43:14Z")

</div>

Keith Barr wrote:

> Interesting. I am running (obviously) on Windows and I also have a Linux box, both on patch 111, and both leaking. It isn't a quick leak, if you run for an a while it might show up. How long did you let it go?

Only 10 cycles, but it didn't seem to be growing...

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [21 February 2008 22:47 UTC](https://rubytalk.org/t/newbie-mem-leak-issue/44683/9 "2008-02-21T22:47:07Z")

</div>

John Woods wrote:

> \>\> Array.new(1000000) {str}
> 
> Isn't the above still just an array of 1000000 refs to the same string? Perhaps you meant str.dup?

Oops, you're right. Using #dup or the following will work:

a = Array.new(5) {"str"}  
p a.map {|s| s.object\_id}.uniq.size # =\> 5

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [22 February 2008 06:32 UTC](https://rubytalk.org/t/newbie-mem-leak-issue/44683/10 "2008-02-22T06:32:43Z")

</div>

Joel VanderWerf wrote:

> Keith Barr wrote:
> 
> > Interesting. I am running (obviously) on Windows and I also have a Linux box, both on patch 111, and both leaking. It isn't a quick leak, if you run for an a while it might show up. How long did you let it go?
> 
> Only 10 cycles, but it didn't seem to be growing...

After 150 cycles, still under 18M. (This is the same code as yours with the one dir path modification.)

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

---

<div class="post-metadata">

**Author:** ![Jan\_Svitok](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@Jan\_Svitok](https://rubytalk.org/u/Jan_Svitok)\
**Post date:** [22 February 2008 10:35 UTC](https://rubytalk.org/t/newbie-mem-leak-issue/44683/11 "2008-02-22T10:35:42Z")

</div>

I have run it under winxp, the code and results can be found at  
[http://bryvecer.sk/leak/](http://bryvecer.sk/leak/)  
It seems that it might slowly leak something, but I don't know for sure.

I have used pslist from sysinternals.com for memory measurements.

> **···**
>
> On Fri, Feb 22, 2008 at 7:32 AM, Joel VanderWerf \<vjoel@path.berkeley.edu\> wrote:
> 
> > Joel VanderWerf wrote:  
> > \> Keith Barr wrote:  
> > \>\> Interesting. I am running (obviously) on Windows and I also have a  
> > \>\> Linux box, both on patch 111, and both leaking. It isn't a quick  
> > \>\> leak, if you run for an a while it might show up. How long did you  
> > \>\> let it go?  
> > \>  
> > \> Only 10 cycles, but it didn't seem to be growing...
> > 
> > After 150 cycles, still under 18M. (This is the same code as yours with  
> > the one dir path modification.)

---

<div class="post-metadata">

**Author:** ![Jan\_Svitok](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@Jan\_Svitok](https://rubytalk.org/u/Jan_Svitok)\
**Post date:** [22 February 2008 11:38 UTC](https://rubytalk.org/t/newbie-mem-leak-issue/44683/12 "2008-02-22T11:38:17Z")

</div>

I have run Ruby MemoryValidator by SoftwareVerify on your code:

This version doesn't leak:

#DATA\_PATH = "C:/Program Files" # 80\_000 files  
DATA\_PATH = "C:/Program Files/Microsoft Visual Studio 8" # 19\_000 files

class LeakTest  
def get\_files(pathname)  
&nbsp;&nbsp;&nbsp;listing = Array.new

&nbsp;&nbsp;&nbsp;Dir.glob("#{pathname}/\*",0) do |f|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# add the files  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;listing \<\< f  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# now add directories.  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(File.directory?(f))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;returnlist = get\_files(f)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;returnlist.each {|filename| listing \<\< filename }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;return listing  
end

include GC  
def cleanup  
&nbsp;&nbsp;&nbsp;GC.start  
&nbsp;&nbsp;&nbsp;sleep(5)  
end

def monitor  
&nbsp;&nbsp;&nbsp;listing = get\_files(DATA\_PATH)  
&nbsp;&nbsp;&nbsp;sleep(5)  
end  
end

lt = LeakTest.new  
while true  
lt.monitor  
lt.cleanup  
end

This is my version, that seems to use less objects  
(I'm 1. using an accumulator, and 2. leave the arrays as are, until  
they are all ready, and then flattening them,  
so GC does it work once for all objects) It's possible that 1. without  
2. is better.

#DATA\_PATH = "C:/Program Files" # 80\_000 files  
DATA\_PATH = "C:/Program Files/Microsoft Visual Studio 8" # 19\_000 files

class LeakTest  
def get\_files(listing, pathname)  
&nbsp;&nbsp;&nbsp;files = Dir.glob("#{pathname}/\*", 0)  
&nbsp;&nbsp;&nbsp;# add the files  
&nbsp;&nbsp;&nbsp;listing \<\< files  
&nbsp;&nbsp;&nbsp;# now add directories.  
&nbsp;&nbsp;&nbsp;files.each do |f|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;get\_files(listing, f) if File.directory?(f)  
&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;return listing  
end

include GC  
def cleanup  
&nbsp;&nbsp;&nbsp;GC.start  
&nbsp;&nbsp;&nbsp;sleep(5)  
end

def monitor  
&nbsp;&nbsp;&nbsp;listing =   
&nbsp;&nbsp;&nbsp;get\_files(listing, DATA\_PATH)  
&nbsp;&nbsp;&nbsp;listing.flatten!  
&nbsp;&nbsp;&nbsp;sleep(5)  
end  
end

lt = LeakTest.new  
while true  
lt.monitor  
lt.cleanup  
end

It seems that the problem is in the little debug messages, that are  
hard to collect.

> **···**
>
> On Fri, Feb 22, 2008 at 11:35 AM, Jano Svitok \<jan.svitok@gmail.com\> wrote:
> 
> > On Fri, Feb 22, 2008 at 7:32 AM, Joel VanderWerf \> \<vjoel@path.berkeley.edu\> wrote:
> > 
> > \> Joel VanderWerf wrote:  
> > \> \> Keith Barr wrote:  
> > \> \>\> Interesting. I am running (obviously) on Windows and I also have a  
> > \> \>\> Linux box, both on patch 111, and both leaking. It isn't a quick  
> > \> \>\> leak, if you run for an a while it might show up. How long did you  
> > \> \>\> let it go?  
> > \> \>  
> > \> \> Only 10 cycles, but it didn't seem to be growing...  
> > \>  
> > \> After 150 cycles, still under 18M. (This is the same code as yours with  
> > \> the one dir path modification.)
> > 
> > I have run it under winxp, the code and results can be found at  
> > [http://bryvecer.sk/leak/](http://bryvecer.sk/leak/)  
> > It seems that it might slowly leak something, but I don't know for sure.
> > 
> > I have used pslist from sysinternals.com for memory measurements.

---

<div class="post-metadata">

**Author:** ![Jan\_Svitok](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@Jan\_Svitok](https://rubytalk.org/u/Jan_Svitok)\
**Post date:** [22 February 2008 16:34 UTC](https://rubytalk.org/t/newbie-mem-leak-issue/44683/13 "2008-02-22T16:34:11Z")

</div>

I meant: there's no leak in ruby land - i.e. the object count stays  
the same. However, there might be a leak in C land.

> **···**
>
> On Fri, Feb 22, 2008 at 12:38 PM, Jano Svitok \<jan.svitok@gmail.com\> wrote:
> 
> > This version doesn't leak:

---

<div class="post-metadata">

**Author:** ![Barr\_Keith](https://avatars.discourse-cdn.com/v4/letter/b/b5e925/32.png) [@Barr\_Keith](https://rubytalk.org/u/Barr_Keith)\
**Post date:** [22 February 2008 17:37 UTC](https://rubytalk.org/t/newbie-mem-leak-issue/44683/14 "2008-02-22T17:37:19Z")

</div>

Jano Svitok wrote:

> **···**
>
> > On Fri, Feb 22, 2008 at 12:38 PM, Jano Svitok \<jan.svitok@gmail.com\> \> wrote:
> > 
> > > This version doesn't leak:
> > 
> > I meant: there's no leak in ruby land - i.e. the object count stays  
> > the same. However, there might be a leak in C land.
> 
> Thanks for everyone's ideas. After further testing, it seems like the  
> move from p110 to p111 made a difference.
> 
> I appreciate the help.
> 
> Keith  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
