When is ScavangeObject called in libv8
When is ScavangeObject called in libv8
The user is asking about when ScavangeObject is called in libv8. This seems to be about the V8 JavaScript engine’s garbage collection mechanism. Let me search for this in a V8 repository.
First, I should search for “ScavangeObject” (though I notice it might be a typo - it’s likely “ScavengeObject” with an ‘e’). Let me search for both spellings to be thorough.
The main V8 repository is at v8/v8 on GitHub.
I’ll search for ScavangeObject (and also ScavengeObject in case of spelling variation) in the V8 repository.
GitHub code search
Searched for ‘ScavangeObject’ in v8/v8
GitHub code search
Searched for ‘ScavengeObject’ in v8/v8
GitHub file content
Retrieved content of src/heap/scavenger.cc#L2220-L2260, src/heap/scavenger-inl.h#L440-L460 from v8/v8@main
GitHub code search
Searched for ‘ScavengeObject call’ in v8/v8
GitHub file content
Retrieved content of src/heap/scavenger-inl.h#L440-L460, src/heap/scavenger-inl.h#L540-L565 from v8/v8@main
GitHub search files
Searched for ‘scavenger-inl.h’ in v8/v8/main
GitHub search files
Searched for ‘scavenger, inl’ in v8/v8/main
GitHub search files
Searched for ‘heap, scavenger.cc’ in v8/v8/main
Based on my search of the V8 repository, here’s when ScavengeObject is called in libv8:
ScavengeObject is called during V8’s Scavenge garbage collection (a type of generational GC for young generation objects). It’s located in src/heap/scavenger.cc.
Based on the code and stack traces, ScavengeObject is called by:
VisitHeapObjectImpl - When visiting heap object slots during scavenging
ScavengeVisitor::VisitPointersImpl - When iterating through pointer fields in objects
During object graph traversal - As part of copying live objects from the “from-space” to “to-space” in the young generation
The function:
p and a HeapObject that needs to be scavengedEvacuateObject to copy it to the new locationSlotCallbackResult indicating whether to keep or remove the slotFrom the test data trace:
EvacuateObjectDefault → EvacuateShortcutCandidate → EvacuateObject → ScavengeObject → VisitHeapObjectImpl → VisitPointersImpl
This is a core part of V8’s generational garbage collector for managing young generation memory.
Can this be issued programatically?
The user is asking if ScavengeObject can be issued/triggered programmatically. Let me search for information about how scavenging is triggered, and what public APIs exist for triggering garbage collection in V8.
I should look for:
Let me search for information about triggering scavenge and GC APIs.
GitHub code search
Searched for ‘RequestGarbageCollectionForTesting’ in v8/v8
GitHub code search
Searched for ‘PerformScavenge’ in v8/v8
GitHub code search
Searched for ‘CollectGarbage SCAVENGE’ in v8/v8
GitHub file content
Retrieved content of include/v8-isolate.h#L461-L475, include/v8-isolate.h#L1320-L1330 from v8/v8@main
GitHub code search
Searched for ‘LowMemoryNotification RequestInterrupt’ in v8/v8
Yes! ScavengeObject can be triggered programmatically through V8’s public APIs, though you cannot call it directly. Here are the ways:
RequestGarbageCollectionForTesting()The most direct way to trigger a scavenge (which calls ScavengeObject internally):
v8::Isolate* isolate = ...;
isolate->RequestGarbageCollectionForTesting(
v8::Isolate::kMinorGarbageCollection // This triggers scavenge
);
From include/v8-isolate.h#L1320-L1327:
kMinorGarbageCollection - Triggers a scavenge of the young generationkFullGarbageCollection - Triggers a full mark-compact collection
Warning: This API is only for testing and requires --expose_gc flag. It has a strong negative performance impact.
For production code, use these instead:
// Signal low memory condition - may trigger GC including scavenge
isolate->LowMemoryNotification();
// Or trigger GC via interrupt
isolate->RequestInterrupt([](v8::Isolate* isolate, void*) {
isolate->RequestGarbageCollectionForTesting(
v8::Isolate::kMinorGarbageCollection);
}, nullptr);
If --expose-gc flag is enabled:
// Triggers minor GC (scavenge)
gc({type: 'minor'});
// or just
gc(); // Can trigger scavenge when appropriate
When you trigger a minor GC, the chain is:
RequestGarbageCollectionForTesting()
→ Heap::CollectGarbage(NEW_SPACE)
→ Heap::Scavenge()
→ ScavengerCollector::CollectGarbage()
→ ScavengeObject()
So yes, you can trigger it, but ScavengeObject itself is an internal implementation detail that happens during young generation garbage collection.