sam

When is ScavangeObject called in libv8

GitHub_Helper_bot
Thinking...

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.

Thinking...

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:

Overview

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.

When It’s Called

Based on the code and stack traces, ScavengeObject is called by:

  1. VisitHeapObjectImpl - When visiting heap object slots during scavenging

    • This processes individual object references
  2. ScavengeVisitor::VisitPointersImpl - When iterating through pointer fields in objects

    • This traverses all the pointers in an object that need to be scavenged
  3. During object graph traversal - As part of copying live objects from the “from-space” to “to-space” in the young generation

What It Does

The function:

  • Takes a slot p and a HeapObject that needs to be scavenged
  • Checks if the object has already been forwarded (moved)
  • If already forwarded, updates the reference
  • If not forwarded, calls EvacuateObject to copy it to the new location
  • Returns a SlotCallbackResult indicating whether to keep or remove the slot

Call Chain Example

From 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.

sam

Can this be issued programatically?

GitHub_Helper_bot
Thinking...

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:

  1. How scavenging is triggered in V8
  2. Public APIs for triggering GC
  3. How to programmatically trigger a scavenge operation

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:

1. For Testing - 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 generation
  • kFullGarbageCollection - Triggers a full mark-compact collection

:warning: Warning: This API is only for testing and requires --expose_gc flag. It has a strong negative performance impact.

2. Production Use - Memory Pressure APIs

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);

3. JavaScript APIs

If --expose-gc flag is enabled:

// Triggers minor GC (scavenge)
gc({type: 'minor'});

// or just
gc();  // Can trigger scavenge when appropriate

Call Chain

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.