Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GC - collectGarbage() called recursively #1128

Open
pumkinpal opened this issue Jun 11, 2023 · 0 comments
Open

GC - collectGarbage() called recursively #1128

pumkinpal opened this issue Jun 11, 2023 · 0 comments

Comments

@pumkinpal
Copy link

pumkinpal commented Jun 11, 2023

I've observed that collectGarbage() can be called recursively during the sweep phase as freeing objects calls reallocate() while vm.bytesAllocated is still greater than vm.nextGC, which is only updated after the sweep phase ends.

Updating vm.nextGC before calling markRoots() in collectGarbage() seems to solve this issue as it ensures that vm.nextGC won't be less than vm.bytesAllocated throughout the sweep, preventing reallocate() from calling collectGarbage().

void collectGarbage()
{
#ifdef CLOX_DEBUG_LOG_GC
    printf("-- gc begin\n");
    size_t before = vm.bytesAllocated;
#endif

    // Update nextGC before mark and sweep to ensure GC isn't re-triggered by freeing memory
    vm.nextGC = vm.bytesAllocated;

    markRoots();
    traceReferences();
    tableRemoveWhite(&vm.strings);
    sweep();

    vm.nextGC = vm.bytesAllocated * CLOX_GC_HEAP_GROW_FACTOR;

#ifdef CLOX_DEBUG_LOG_GC
    printf("-- gc end\n");
    printf("   collected %zu bytes (from %zu to %zu) next at %zu\n",
        before - vm.bytesAllocated, before, vm.bytesAllocated,
        vm.nextGC);
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant