Skip to content

Commit b4f4519

Browse files
authored
Expose is_emergency_collection to VM bindings (#997)
`is_emergency_collection` is useful during weak reference processing. JVMs can choose not to retain referents of `SoftReference` during emergency GC. The `is_emergency_collection` function was moved from `Plan` to `GlobalState` in 57af17f. After that, the `MMTK:state` field is private and inaccessible to VM bindings. VM bindings that depend on the to-be-deprecated built-in `ReferenceProcessor` still work because it is part of the `mmtk` crate, and can `mmtk.state.is_emergency_collection`. However, VM bindings that do weak reference processing on the VM side using the `Scanning::process_weak_refs` API can no longer call that function. This makes [this PR](mmtk/mmtk-jikesrvm#150) unable to merge. In the future, the OpenJDK binding will also need it when it off-loads weak reference processing to the binding side. This PR adds a public API function `MMTK::is_emergency_collection` which can be called by the VM bindings.
1 parent 57af17f commit b4f4519

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/mmtk.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,24 @@ impl<VM: VMBinding> MMTK<VM> {
272272
*self.state.gc_status.lock().unwrap() == GcStatus::GcProper
273273
}
274274

275+
/// Return true if the current GC is an emergency GC.
276+
///
277+
/// An emergency GC happens when a normal GC cannot reclaim enough memory to satisfy allocation
278+
/// requests. Plans may do full-heap GC, defragmentation, etc. during emergency in order to
279+
/// free up more memory.
280+
///
281+
/// VM bindings can call this function during GC to check if the current GC is an emergency GC.
282+
/// If it is, the VM binding is recommended to retain fewer objects than normal GCs, to the
283+
/// extent allowed by the specification of the VM or langauge. For example, the VM binding may
284+
/// choose not to retain objects used for caching. Specifically, for Java virtual machines,
285+
/// that means not retaining referents of [`SoftReference`][java-soft-ref] which is primarily
286+
/// designed for implementing memory-sensitive caches.
287+
///
288+
/// [java-soft-ref]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/ref/SoftReference.html
289+
pub fn is_emergency_collection(&self) -> bool {
290+
self.state.is_emergency_collection()
291+
}
292+
275293
/// The application code has requested a collection. This is just a GC hint, and
276294
/// we may ignore it.
277295
///

0 commit comments

Comments
 (0)