Forum: Ruby-dev Make a cap for malloc_limit adjustment

5cf8f058a4c094bb708174fb43e7a387?d=identicon&s=25 unknown (Guest)
on 2014-05-04 18:18
(Received via mailing list)
Issue #9687 has been updated by Tomoyuki Chikanaga.

Backport changed from 2.0.0: DONTNEED, 2.1: REQUIRED to 2.0.0: DONTNEED,
2.1: DONE

r45468 was backported into ruby_2_1 branch at r45817.

----------------------------------------
Bug #9687: Make a cap for malloc_limit adjustment
https://bugs.ruby-lang.org/issues/9687#change-46515

* Author: Koichi Sasada
* Status: Closed
* Priority: Normal
* Assignee: Koichi Sasada
* Category: core
* Target version: current: 2.2.0
* ruby -v: 2.2
* Backport: 2.0.0: DONTNEED, 2.1: DONE
----------------------------------------
The blog post [Ruby Garbage Collection: Still Not Ready for Production]
(http://www.omniref.com/blog/blog/2014/03/27/ruby-g...)
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.

```
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.
This topic is locked and can not be replied to.