# \[Open\] Make a cap for malloc\_limit adjustment

**URL:** <https://www.ruby-forum.com/t/open-make-a-cap-for-malloc-limit-adjustment/240129>\
**Category:** Development\
**Created:** [March 29, 2014, 5:44am UTC](https://www.ruby-forum.com/t/open-make-a-cap-for-malloc-limit-adjustment/240129 "2014-03-29T05:44:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![musicdenotation](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea666f/32.png) [@musicdenotation](https://www.ruby-forum.com/u/musicdenotation)\
**Post date:** [March 29, 2014, 5:44am UTC](https://www.ruby-forum.com/t/open-make-a-cap-for-malloc-limit-adjustment/240129/1 "2014-03-29T05:44:13Z")

</div>

Issue #9687 has been updated by Koichi Sasada.

Status changed from Closed to Open  
Backport changed from 2.0.0: UNKNOWN, 2.1: UNKNOWN to 2.0.0: UNKNOWN,  
2.1: REQUIRED

* * *

Bug #9687: Make a cap for malloc\_limit adjustment

> **[Bug #9687: Make a cap for malloc\_limit adjustment - Ruby master - Ruby Issue...](https://bugs.ruby-lang.org/issues/9687#change-45988)**
>
> Redmine

- Author: Koichi Sasada
- Status: Open
- Priority: Normal
- Assignee: Koichi Sasada
- Category: core
- Target version: current: 2.2.0
- ruby -v: 2.2
- Backport: 2.0.0: UNKNOWN, 2.1: REQUIRED

* * *

The blog post [Ruby Garbage Collection: Still Not Ready for Production]  
([http://www.omniref.com/blog/blog/2014/03/27/ruby-garbage-collection-still-not-ready-for-production/](http://www.omniref.com/blog/blog/2014/03/27/ruby-garbage-collection-still-not-ready-for-production/))  
pointed out that the following code consumes all of memory:

```ruby
while true
  "a" * (1024 ** 2)
end

```

This is because `malloc\_limit’ grows and grows infinitely.

The following patch will solve this issue.

```auto
Index: gc.c
===================================================================
--- gc.c (revision 45433)
+++ gc.c (working copy)
@@ -2890,7 +2890,7 @@ gc_before_sweep(rb_objspace_t *objspace)
       malloc_limit = (size_t)(inc * 
gc_params.malloc_limit_growth_factor);
       if (gc_params.malloc_limit_max > 0 && /* ignore max-check if 0 */
     malloc_limit > gc_params.malloc_limit_max) {
- malloc_limit = inc;
+ malloc_limit = gc_params.malloc_limit_max;
       }
   }
   else {

```

Maybe this patch should be applied for 2.1.x series.
