@@ -1478,6 +1478,10 @@ static int rgengc_remember(rb_objspace_t *objspace, VALUE obj);
1478
1478
static void rgengc_mark_and_rememberset_clear (rb_objspace_t * objspace , rb_heap_t * heap );
1479
1479
static void rgengc_rememberset_mark (rb_objspace_t * objspace , rb_heap_t * heap );
1480
1480
1481
+ #ifdef USE_THIRD_PARTY_HEAP
1482
+ static size_t rb_mmtk_heap_limit (void );
1483
+ #endif
1484
+
1481
1485
static inline int
1482
1486
RVALUE_FLAGS_AGE (VALUE flags )
1483
1487
{
@@ -1859,18 +1863,10 @@ rb_objspace_alloc(void)
1859
1863
dont_gc_on ();
1860
1864
1861
1865
#ifdef USE_THIRD_PARTY_HEAP
1862
- const char * envval ;
1863
- long heap_size ;
1864
- if ((envval = getenv ("THIRD_PARTY_HEAP_LIMIT" )) != 0 ) {
1865
- heap_size = atol (envval );
1866
- } else {
1867
- heap_size = gc_params .heap_init_slots * sizeof (RVALUE );
1868
- }
1869
-
1870
- // Note: this limit is currently broken for NoGC, but we still attempt to
1866
+ // Note: the limit is currently broken for NoGC, but we still attempt to
1871
1867
// initialise it properly regardless.
1872
1868
// See https://github.com/mmtk/mmtk-core/issues/214
1873
- mmtk_init_binding (heap_size , & ruby_upcalls );
1869
+ mmtk_init_binding (rb_mmtk_heap_limit () , & ruby_upcalls );
1874
1870
#endif
1875
1871
1876
1872
return objspace ;
@@ -15047,4 +15043,43 @@ RubyUpcalls ruby_upcalls = {
15047
15043
rb_mmtk_scan_object_ruby_style ,
15048
15044
};
15049
15045
15046
+ // Use up to 80% of memory for the heap
15047
+ static const int rb_mmtk_heap_limit_percentage = 80 ;
15048
+
15049
+ static size_t rb_mmtk_system_physical_memory (void )
15050
+ {
15051
+ #ifdef __linux__
15052
+ const long physical_pages = sysconf (_SC_PHYS_PAGES );
15053
+ const long page_size = sysconf (_SC_PAGE_SIZE );
15054
+ if (physical_pages == -1 || page_size == -1 )
15055
+ {
15056
+ rb_bug ("failed to get system physical memory size" );
15057
+ }
15058
+ return (size_t ) physical_pages * (size_t ) page_size ;
15059
+ #else
15060
+ #error no implementation of rb_mmtk_system_physical_memory on this platform
15061
+ #endif
15062
+ }
15063
+
15064
+ static size_t rb_mmtk_available_system_memory (void )
15065
+ {
15066
+ /*
15067
+ * If we're in a container, we should use the maximum container memory,
15068
+ * otherwise each container will try to use all system memory. There's
15069
+ * example logic for this in the JVM and SVM (see CgroupV1Subsystem
15070
+ * and CgroupV2Subsystem).
15071
+ */
15072
+
15073
+ return rb_mmtk_system_physical_memory ();
15074
+ }
15075
+
15076
+ size_t rb_mmtk_heap_limit (void ) {
15077
+ const char * envval ;
15078
+ if ((envval = getenv ("THIRD_PARTY_HEAP_LIMIT" )) != 0 ) {
15079
+ return atol (envval );
15080
+ } else {
15081
+ return rb_mmtk_available_system_memory () / 100 * rb_mmtk_heap_limit_percentage ;
15082
+ }
15083
+ }
15084
+
15050
15085
#endif
0 commit comments