Skip to content

Commit f77743c

Browse files
authored
fix #30653, call malloc_trim sometimes (#32428)
this works around what seems to be a glibc bug
1 parent 0d9c72d commit f77743c

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/gc.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
#include "gc.h"
44
#include "julia_gcext.h"
55
#include "julia_assert.h"
6+
#ifdef __GLIBC__
7+
#include <malloc.h> // for malloc_trim
8+
#endif
69

710
#ifdef __cplusplus
811
extern "C" {
@@ -568,6 +571,10 @@ static int prev_sweep_full = 1;
568571
// Full collection heuristics
569572
static int64_t live_bytes = 0;
570573
static int64_t promoted_bytes = 0;
574+
#ifdef __GLIBC__
575+
// maxrss at last malloc_trim
576+
static int64_t last_trim_maxrss = 0;
577+
#endif
571578

572579
static int64_t last_full_live_ub = 0;
573580
static int64_t last_full_live_est = 0;
@@ -2725,6 +2732,8 @@ static void jl_gc_queue_bt_buf(jl_gc_mark_cache_t *gc_cache, jl_gc_mark_sp_t *sp
27252732
}
27262733
}
27272734

2735+
size_t jl_maxrss(void);
2736+
27282737
// Only one thread should be running in this function
27292738
static int _jl_gc_collect(jl_ptls_t ptls, int full)
27302739
{
@@ -2883,6 +2892,18 @@ static int _jl_gc_collect(jl_ptls_t ptls, int full)
28832892
}
28842893
}
28852894

2895+
#ifdef __GLIBC__
2896+
if (sweep_full) {
2897+
// issue #30653
2898+
// empirically, the malloc runaway seemed to occur within a growth gap
2899+
// of about 20-25%
2900+
if (jl_maxrss() > (last_trim_maxrss/4)*5) {
2901+
malloc_trim(0);
2902+
last_trim_maxrss = jl_maxrss();
2903+
}
2904+
}
2905+
#endif
2906+
28862907
uint64_t gc_end_t = jl_hrtime();
28872908
uint64_t pause = gc_end_t - t0;
28882909
gc_final_pause_end(t0, gc_end_t);

0 commit comments

Comments
 (0)