Skip to content

Commit c71f18a

Browse files
committed
free even more state for exited threads
1 parent 848ae2c commit c71f18a

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

src/gc-stacks.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ void sweep_stack_pools(void)
203203
assert(gc_n_threads);
204204
for (int i = 0; i < gc_n_threads; i++) {
205205
jl_ptls_t ptls2 = gc_all_tls_states[i];
206+
if (ptls2 == NULL)
207+
continue;
206208

207209
// free half of stacks that remain unused since last sweep
208210
for (int p = 0; p < JL_N_STACK_POOLS; p++) {
@@ -227,6 +229,9 @@ void sweep_stack_pools(void)
227229
small_arraylist_free(al);
228230
}
229231
}
232+
if (jl_atomic_load_relaxed(&ptls2->current_task) == NULL) {
233+
small_arraylist_free(ptls2->heap.free_stacks);
234+
}
230235

231236
small_arraylist_t *live_tasks = &ptls2->heap.live_tasks;
232237
size_t n = 0;

src/gc.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,6 +1746,19 @@ void gc_free_pages(void)
17461746
}
17471747
}
17481748

1749+
void gc_move_to_global_page_pool(jl_gc_page_stack_t *pgstack)
1750+
{
1751+
while (1) {
1752+
jl_gc_pagemeta_t *pg = pop_lf_back(pgstack);
1753+
if (pg == NULL) {
1754+
break;
1755+
}
1756+
jl_gc_free_page(pg);
1757+
jl_atomic_fetch_add_relaxed(&gc_heap_stats.heap_size, -GC_PAGE_SZ);
1758+
push_lf_back(&global_page_pool_freed, pg);
1759+
}
1760+
}
1761+
17491762
// setup the data-structures for a sweep over all memory pools
17501763
static void gc_sweep_pool(void)
17511764
{
@@ -1818,6 +1831,7 @@ static void gc_sweep_pool(void)
18181831
}
18191832
}
18201833
}
1834+
free(tmp);
18211835

18221836
// merge free lists
18231837
for (int t_i = 0; t_i < n_threads; t_i++) {
@@ -1848,6 +1862,7 @@ static void gc_sweep_pool(void)
18481862
}
18491863
}
18501864
}
1865+
free(pfl);
18511866

18521867
// cleanup
18531868
free(pfl);
@@ -3776,7 +3791,8 @@ static int _jl_gc_collect(jl_ptls_t ptls, jl_gc_collection_t collection)
37763791
ptls2->heap.remset->len = 0;
37773792
}
37783793
// free empty GC state for threads that have exited
3779-
if (ptls2->current_task == NULL) {
3794+
if (jl_atomic_load_relaxed(&ptls2->current_task) == NULL &&
3795+
(ptls->tid < gc_first_tid || ptls2->tid >= gc_first_tid + jl_n_gcthreads)) {
37803796
jl_thread_heap_t *heap = &ptls2->heap;
37813797
if (heap->weak_refs.len == 0)
37823798
small_arraylist_free(&heap->weak_refs);
@@ -3787,9 +3803,10 @@ static int _jl_gc_collect(jl_ptls_t ptls, jl_gc_collection_t collection)
37873803
if (heap->last_remset->len == 0)
37883804
arraylist_free(heap->last_remset);
37893805
if (ptls2->finalizers.len == 0)
3790-
arraylist_free(&ptls->finalizers);
3806+
arraylist_free(&ptls2->finalizers);
37913807
if (ptls2->sweep_objs.len == 0)
3792-
arraylist_free(&ptls->sweep_objs);
3808+
arraylist_free(&ptls2->sweep_objs);
3809+
gc_move_to_global_page_pool(&ptls2->page_metadata_buffered);
37933810
}
37943811
}
37953812

src/threading.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,8 @@ JL_DLLEXPORT jl_gcframe_t **jl_adopt_thread(void)
435435
void jl_task_frame_noreturn(jl_task_t *ct) JL_NOTSAFEPOINT;
436436
void scheduler_delete_thread(jl_ptls_t ptls) JL_NOTSAFEPOINT;
437437

438+
void jl_free_thread_gc_state(jl_ptls_t ptls);
439+
438440
static void jl_delete_thread(void *value) JL_NOTSAFEPOINT_ENTER
439441
{
440442
#ifndef _OS_WINDOWS_
@@ -509,9 +511,10 @@ static void jl_delete_thread(void *value) JL_NOTSAFEPOINT_ENTER
509511
pthread_mutex_unlock(&in_signal_lock);
510512
#endif
511513
free(ptls->bt_data);
514+
small_arraylist_free(&ptls->locks);
515+
ptls->previous_exception = NULL;
512516
// allow the page root_task is on to be freed
513517
ptls->root_task = NULL;
514-
void jl_free_thread_gc_state(jl_ptls_t ptls);
515518
jl_free_thread_gc_state(ptls);
516519
// then park in safe-region
517520
(void)jl_gc_safe_enter(ptls);

0 commit comments

Comments
 (0)