# Hard question: Any ideas why this code leaks memory?

**URL:** <https://www.ruby-forum.com/t/hard-question-any-ideas-why-this-code-leaks-memory/146300>\
**Category:** Ruby\
**Created:** [August 19, 2008, 9:50pm UTC](https://www.ruby-forum.com/t/hard-question-any-ideas-why-this-code-leaks-memory/146300 "2008-08-19T21:50:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sven\_CSKoehler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3be4f8/32.png) [@Sven\_CSKoehler](https://www.ruby-forum.com/u/Sven_CSKoehler)\
**Post date:** [August 19, 2008, 9:50pm UTC](https://www.ruby-forum.com/t/hard-question-any-ideas-why-this-code-leaks-memory/146300/1 "2008-08-19T21:50:29Z")

</div>

Hello,

could someone help me understand why this code leaks memory? (I am using  
ruby 1.8.6 (2007-12-03 patchlevel 113) [x86\_64-linux].)

def rss\_leaks  
/^VmRSS:\s+(\d+)/m.match(File.open("/proc/#{$$}/status") {|f|  
f.read })[1].to\_i  
end

| irb(main):002:0\> 100000.times { rss\_leaks }; rss\_leaks  
| =\> 11624  
| irb(main):003:0\> 100000.times { rss\_leaks }; rss\_leaks  
| =\> 16836  
| irb(main):004:0\> 100000.times { rss\_leaks }; rss\_leaks  
| =\> 22008  
| irb(main):005:0\> 100000.times { rss\_leaks }; rss\_leaks  
| =\> 27248  
| irb(main):006:0\> 100000.times { rss\_leaks }; rss\_leaks  
| =\> 32468

Calling GC.start inbetween does not help, but when we rewrote rss\_leaks  
this way the leaks disappeared:

def rss\_no\_leaks()  
d = File.open("/proc/#{$$}/status") {|f| f.read }  
/^VmRSS:\s+(\d+)/m.match(d)[1].to\_i  
end

| irb(main):002:0\> 100000.times { rss\_no\_leaks }; rss\_no\_leaks  
| =\> 6568  
| irb(main):003:0\> 100000.times { rss\_no\_leaks }; rss\_no\_leaks  
| =\> 6568  
| irb(main):004:0\> 100000.times { rss\_no\_leaks }; rss\_no\_leaks  
| =\> 6588  
| irb(main):005:0\> 100000.times { rss\_no\_leaks }; rss\_no\_leaks  
| =\> 6592  
| irb(main):006:0\> 100000.times { rss\_no\_leaks }; rss\_no\_leaks  
| =\> 6572

Any help to further understand what actually causes these leaks would be  
very appreaciated!

Best wishes,

Sven

---

<div class="post-metadata">

**Author:** ![Sven\_CSKoehler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3be4f8/32.png) [@Sven\_CSKoehler](https://www.ruby-forum.com/u/Sven_CSKoehler)\
**Post date:** [August 20, 2008, 2:32am UTC](https://www.ruby-forum.com/t/hard-question-any-ideas-why-this-code-leaks-memory/146300/2 "2008-08-20T02:32:14Z")

</div>

On Tue, Aug 19, 2008 at 9:46 PM, Sven C. Koehler [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> def rss\_leaks  
> /^VmRSS:\s+(\d+)/m.match(File.open(“/proc/#{$$}/status”) {|f|  
> f.read })[1].to\_i  
> end

No idea about the leak, just wanted to point out for the archives that  
/m does nothing there because there’s no dot.

/m in Ruby is /s in Perl (“.” matches “\n”), and Perl’s /m (multi-line  
mode) is always on in Ruby, there’s no way to switch that one off. ^  
is always beginning of line, and you assert beginning of string with  
\A. To tangle this a little more /s in Ruby exists and means the  
regexp is written in the SJIS encoding.

---

<div class="post-metadata">

**Author:** ![Sven\_CSKoehler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3be4f8/32.png) [@Sven\_CSKoehler](https://www.ruby-forum.com/u/Sven_CSKoehler)\
**Post date:** [August 20, 2008, 4:54am UTC](https://www.ruby-forum.com/t/hard-question-any-ideas-why-this-code-leaks-memory/146300/3 "2008-08-20T04:54:22Z")

</div>

Hi,

At Wed, 20 Aug 2008 04:46:24 +0900,  
Sven C. Koehler wrote in [ruby-talk:311847]:

> could someone help me understand why this code leaks memory? (I am using  
> ruby 1.8.6 (2007-12-03 patchlevel 113) [x86\_64-linux].)
> 
> def rss\_leaks  
> /^VmRSS:\s+(\d+)/m.match(File.open("/proc/#{$$}/status") {|f|  
> f.read })[1].to\_i  
> end

> Calling GC.start inbetween does not help, but when we rewrote rss\_leaks  
> this way the leaks disappeared:
> 
> def rss\_no\_leaks()  
> d = File.open("/proc/#{$$}/status") {|f| f.read }  
> /^VmRSS:\s+(\d+)/m.match(d)[1].to\_i  
> end

Seems a bug.

---

<div class="post-metadata">

**Author:** ![Sven\_CSKoehler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3be4f8/32.png) [@Sven\_CSKoehler](https://www.ruby-forum.com/u/Sven_CSKoehler)\
**Post date:** [August 20, 2008, 5:54am UTC](https://www.ruby-forum.com/t/hard-question-any-ideas-why-this-code-leaks-memory/146300/4 "2008-08-20T05:54:00Z")

</div>

On Wed, Aug 20, 2008 at 11:50:16AM +0900, Nobuyoshi N. wrote:

> Seems a bug.

Yep, and it seems to be fixed in 1.8.7-p71 :  
[http://rubyforge.org/tracker/?func=detail&aid=15425&group\_id=426&atid=1698](http://rubyforge.org/tracker/?func=detail&aid=15425&group_id=426&atid=1698)

-S.

---

<div class="post-metadata">

**Author:** ![Sven\_CSKoehler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3be4f8/32.png) [@Sven\_CSKoehler](https://www.ruby-forum.com/u/Sven_CSKoehler)\
**Post date:** [August 20, 2008, 8:44am UTC](https://www.ruby-forum.com/t/hard-question-any-ideas-why-this-code-leaks-memory/146300/5 "2008-08-20T08:44:54Z")

</div>

Hi,

At Wed, 20 Aug 2008 12:49:41 +0900,  
Sven C. Koehler wrote in [ruby-talk:311908]:

> Yep, and it seems to be fixed in 1.8.7-p71 :  
> [http://rubyforge.org/tracker/?func=detail&aid=15425&group\_id=426&atid=1698](http://rubyforge.org/tracker/?func=detail&aid=15425&group_id=426&atid=1698)

Hmmm, indeed. But it seems to occur with the latest 1.8.

---

<div class="post-metadata">

**Author:** ![Sven\_CSKoehler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3be4f8/32.png) [@Sven\_CSKoehler](https://www.ruby-forum.com/u/Sven_CSKoehler)\
**Post date:** [August 20, 2008, 1:24pm UTC](https://www.ruby-forum.com/t/hard-question-any-ideas-why-this-code-leaks-memory/146300/6 "2008-08-20T13:24:04Z")

</div>

On Wed, Aug 20, 2008 at 03:44:15PM +0900, Nobuyoshi N. wrote:

> At Wed, 20 Aug 2008 12:49:41 +0900,  
> Sven C. Koehler wrote in [ruby-talk:311908]:
> 
> > Yep, and it seems to be fixed in 1.8.7-p71 :  
> > [http://rubyforge.org/tracker/?func=detail&aid=15425&group\_id=426&atid=1698](http://rubyforge.org/tracker/?func=detail&aid=15425&group_id=426&atid=1698)
> 
> Hmmm, indeed. But it seems to occur with the latest 1.8.

I tried some of the patches mentioned on the page on my ruby-1.8.6, and  
this is  
the patch which worked for me (saves me some time before upgrading to  
1.8.7).

-S.

— parse.old.y 2008-08-20 10:59:56.000000000 +0000  
+++ parse.y 2008-08-20 11:08:16.000000000 +0000  
@@ -5716,7 +5716,7 @@  
rb\_mem\_clear(vars+i, len-i);  
}  
else {

- 

```
              *vars++ = (VALUE)ruby_scope;

```

- 

```
              *vars++ = 0;
              rb_mem_clear(vars, len);
          }
          ruby_scope->local_vars = vars;

```

@@ -5732,6 +5732,7 @@  
if (!(ruby\_scope-\>flags & SCOPE\_CLONE))  
xfree(ruby\_scope-\>local\_tbl);  
}

- 

```
       ruby_scope->local_vars[-1] = 0; /* no reference needed */
      ruby_scope->local_tbl = local_tbl();
  }

```

}

---

<div class="post-metadata">

**Author:** ![Sven\_CSKoehler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3be4f8/32.png) [@Sven\_CSKoehler](https://www.ruby-forum.com/u/Sven_CSKoehler)\
**Post date:** [August 20, 2008, 10:38pm UTC](https://www.ruby-forum.com/t/hard-question-any-ideas-why-this-code-leaks-memory/146300/7 "2008-08-20T22:38:44Z")

</div>

Hi,

At Wed, 20 Aug 2008 20:19:43 +0900,  
Sven C. Koehler wrote in [ruby-talk:311956]:

> I tried some of the patches mentioned on the page on my ruby-1.8.6, and this is  
> the patch which worked for me (saves me some time before upgrading to 1.8.7).

It has been applied to the latest 1.8, of course.

---

<div class="post-metadata">

**Author:** ![Sven\_CSKoehler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3be4f8/32.png) [@Sven\_CSKoehler](https://www.ruby-forum.com/u/Sven_CSKoehler)\
**Post date:** [October 6, 2008, 8:28pm UTC](https://www.ruby-forum.com/t/hard-question-any-ideas-why-this-code-leaks-memory/146300/8 "2008-10-06T20:28:05Z")

</div>

Issue #461 has been updated by Roger P…

## You should try SVN HEAD and see if it’s still there. See also [http://rubyforge.org/tracker/?func=detail&atid=1698&aid=15425&group\_id=426](http://rubyforge.org/tracker/?func=detail&atid=1698&aid=15425&group_id=426)

[http://redmine.ruby-lang.org/issues/show/461](http://redmine.ruby-lang.org/issues/show/461)

---

<div class="post-metadata">

**Author:** ![Sven\_CSKoehler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3be4f8/32.png) [@Sven\_CSKoehler](https://www.ruby-forum.com/u/Sven_CSKoehler)\
**Post date:** [September 25, 2010, 9:20pm UTC](https://www.ruby-forum.com/t/hard-question-any-ideas-why-this-code-leaks-memory/146300/9 "2010-09-25T21:20:27Z")

</div>

Issue #461 has been updated by Shyouhei U…

Category set to core  
Status changed from Assigned to Feedback

Can this still happen? I can’t observe on my machine with  
ruby 1.8.8dev (2010-09-24 revision 27061) [x86\_64-linux].

irb(main):005:0\> 100000.times { rss\_leaks }; rss\_leaks  
=\> 5616  
irb(main):006:0\> 100000.times { rss\_leaks }; rss\_leaks  
=\> 5616  
irb(main):007:0\> 100000.times { rss\_leaks }; rss\_leaks  
=\> 5616  
irb(main):008:0\> 100000.times { rss\_leaks }; rss\_leaks  
=\> 5616

* * *

[http://redmine.ruby-lang.org/issues/show/461](http://redmine.ruby-lang.org/issues/show/461)
