Skip to content

Commit

Permalink
gc: free unused parts of the heap before merging
Browse files Browse the repository at this point in the history
When a thread exits, the heap is merged into the main thread.
Before doing so, free any unused parts of the heap to reduce
memory usage. Attempts to partially address issue justinethier#534.
  • Loading branch information
yorickhardy committed Nov 15, 2024
1 parent bf9dda2 commit 20fc3c6
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2762,12 +2762,32 @@ void gc_heap_merge(gc_heap * hdest, gc_heap * hsrc)
*/
void gc_merge_all_heaps(gc_thread_data * dest, gc_thread_data * src)
{
gc_heap *hdest, *hsrc;
gc_heap *hdest, *hsrc, *cur, *prev;
int heap_type;

for (heap_type = 0; heap_type < NUM_HEAP_TYPES; heap_type++) {
hdest = dest->heap->heap[heap_type];
hsrc = src->heap->heap[heap_type];
if (!hdest) {
fprintf(stderr, "WARNING !!!!! merging heap type %d does not happen: hdest = %p hsrc = %p size = %d\n",
heap_type, hdest, hsrc, hsrc->size);
fflush(stderr);
}
if (hsrc) {
prev = hsrc;
cur = hsrc->next;
while (cur != NULL) {
if (gc_is_heap_empty(cur)) {
gc_heap_free(cur, prev);
}
prev = prev->next;
cur = prev->next;
}
if (gc_is_heap_empty(hsrc) && hsrc->next == NULL) {
free(hsrc);
hsrc = NULL;
}
}
if (hdest && hsrc) {
gc_heap_merge(hdest, hsrc);
ck_pr_add_ptr(&(dest->cached_heap_total_sizes[heap_type]),
Expand Down

0 comments on commit 20fc3c6

Please sign in to comment.