Skip to content

Instantly share code, notes, and snippets.

@matiasgarciaisaia
Created May 28, 2020 22:22
Show Gist options
  • Save matiasgarciaisaia/859a7128cdf883de64ceaa3de96b144a to your computer and use it in GitHub Desktop.
Save matiasgarciaisaia/859a7128cdf883de64ceaa3de96b144a to your computer and use it in GitHub Desktop.
Patch Ruby 2.5.3 to apply the experimental malloc_trim patch to the GC as suggested in https://www.joyfulbikeshedding.com/blog/2019-03-29-the-status-of-ruby-memory-trimming-and-how-you-can-help-with-testing.html
diff --git a/configure.ac b/configure.ac
index 90ce91b..6ad4317 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2340,6 +2340,7 @@ AC_CHECK_FUNCS(lstat)
AC_CHECK_FUNCS(lutimes)
AC_CHECK_FUNCS(malloc_usable_size)
AC_CHECK_FUNCS(malloc_size)
+AC_CHECK_FUNCS(malloc_trim)
AC_CHECK_FUNCS(mblen)
AC_CHECK_FUNCS(memalign)
AC_CHECK_FUNCS(memset_s)
diff --git a/gc.c b/gc.c
index f031861..2f7bbaf 100644
--- a/gc.c
+++ b/gc.c
@@ -6484,7 +6484,15 @@ gc_start(rb_objspace_t *objspace, const int full_mark, const int immediate_mark,
gc_prof_timer_start(objspace);
{
- gc_marks(objspace, do_full_mark);
+ gc_marks(objspace, do_full_mark);
+#ifdef HAVE_MALLOC_TRIM
+ /* [Experimental] Explicitly free all eligible pages to the kernel. See:
+ *
+ * - https://www.joyfulbikeshedding.com/blog/2019-03-14-what-causes-ruby-memory-bloat.html
+ * - https://bugs.ruby-lang.org/issues/15667
+ */
+ if (do_full_mark) malloc_trim(0);
+#endif
}
gc_prof_timer_stop(objspace);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment