@@ -64,11 +64,37 @@ void jl_gc_init(void) {
6464
6565 arraylist_new (& to_finalize , 0 );
6666 arraylist_new (& finalizer_list_marked , 0 );
67-
67+ gc_num . interval = default_collect_interval ;
6868 gc_num .allocd = 0 ;
6969 gc_num .max_pause = 0 ;
7070 gc_num .max_memory = 0 ;
7171
72+ // Necessary if we want to use Julia heap resizing heuristics
73+ uint64_t mem_reserve = 250 * 1024 * 1024 ; // LLVM + other libraries need some amount of memory
74+ uint64_t min_heap_size_hint = mem_reserve + 1 * 1024 * 1024 ;
75+ uint64_t hint = jl_options .heap_size_hint ;
76+
77+ // check if heap size specified on command line
78+ if (jl_options .heap_size_hint == 0 ) {
79+ char * cp = getenv (HEAP_SIZE_HINT );
80+ if (cp )
81+ hint = parse_heap_size_hint (cp , "JULIA_HEAP_SIZE_HINT=\"<size>[<unit>]\"" );
82+ }
83+ #ifdef _P64
84+ if (hint == 0 ) {
85+ uint64_t constrained_mem = uv_get_constrained_memory ();
86+ if (constrained_mem > 0 && constrained_mem < uv_get_total_memory ())
87+ hint = constrained_mem ;
88+ }
89+ #endif
90+ if (hint ) {
91+ if (hint < min_heap_size_hint )
92+ hint = min_heap_size_hint ;
93+ jl_gc_set_max_memory (hint - mem_reserve );
94+ }
95+
96+ // MMTK supports setting the heap size using the
97+ // MMTK_MIN_HSIZE and MMTK_MAX_HSIZE environment variables
7298 long long min_heap_size ;
7399 long long max_heap_size ;
74100 char * min_size_def = getenv ("MMTK_MIN_HSIZE" );
@@ -77,7 +103,8 @@ void jl_gc_init(void) {
77103 char * max_size_def = getenv ("MMTK_MAX_HSIZE" );
78104 char * max_size_gb = getenv ("MMTK_MAX_HSIZE_G" );
79105
80- // default min heap currently set as Julia's default_collect_interval
106+ // If min and max values are not specified, set them to 0 here
107+ // and use stock heuristics as defined in the binding
81108 if (min_size_def != NULL ) {
82109 char * p ;
83110 double min_size = strtod (min_size_def , & p );
@@ -87,10 +114,9 @@ void jl_gc_init(void) {
87114 double min_size = strtod (min_size_gb , & p );
88115 min_heap_size = (long ) 1024 * 1024 * 1024 * min_size ;
89116 } else {
90- min_heap_size = default_collect_interval ;
117+ min_heap_size = 0 ;
91118 }
92119
93- // default max heap currently set as 70% the free memory in the system
94120 if (max_size_def != NULL ) {
95121 char * p ;
96122 double max_size = strtod (max_size_def , & p );
@@ -100,7 +126,7 @@ void jl_gc_init(void) {
100126 double max_size = strtod (max_size_gb , & p );
101127 max_heap_size = (long ) 1024 * 1024 * 1024 * max_size ;
102128 } else {
103- max_heap_size = uv_get_free_memory () * 70 / 100 ;
129+ max_heap_size = 0 ;
104130 }
105131
106132 // Assert that the number of stock GC threads is 0; MMTK uses the number of threads in jl_options.ngcthreads
@@ -159,7 +185,17 @@ void jl_free_thread_gc_state(struct _jl_tls_states_t *ptls) {
159185}
160186
161187JL_DLLEXPORT void jl_gc_set_max_memory (uint64_t max_mem ) {
162- // MMTk currently does not allow setting the heap size at runtime
188+ #ifdef _P32
189+ max_mem = max_mem < MAX32HEAP ? max_mem : MAX32HEAP ;
190+ #endif
191+ max_total_memory = max_mem ;
192+ }
193+
194+ JL_DLLEXPORT uint64_t jl_gc_get_max_memory (void )
195+ {
196+ // FIXME: We should return the max heap size set in MMTk
197+ // when not using Julia's heap resizing heuristics
198+ return max_total_memory ;
163199}
164200
165201STATIC_INLINE void maybe_collect (jl_ptls_t ptls )
@@ -415,12 +451,6 @@ JL_DLLEXPORT void jl_gc_get_total_bytes(int64_t *bytes) JL_NOTSAFEPOINT
415451 * bytes = (num .total_allocd + num .deferred_alloc + num .allocd );
416452}
417453
418- JL_DLLEXPORT uint64_t jl_gc_get_max_memory (void )
419- {
420- // FIXME: should probably return MMTk's heap size
421- return max_total_memory ;
422- }
423-
424454// These are needed to collect MMTk statistics from a Julia program using ccall
425455JL_DLLEXPORT void (jl_mmtk_harness_begin )(void )
426456{
0 commit comments