-
Notifications
You must be signed in to change notification settings - Fork 76
Special topic chapter for finalizers and weak references #1265
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
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
94f192b
Special topic chapter for finalizer & weakref
wks be789b2
State machine and deprecated things
wks a176a41
Optimization section
wks f1c0970
Update comments
wks 29d443c
Minor fixes
wks 79c8398
Use the word "resurrect" consistently
wks fc946c8
Rewrite part of the finalizers section
wks 160368c
Revise the weakref and optimization sections
wks b9c4086
Use en_US spelling consistently
wks 9ec8d5d
Ephemeron
wks e671ab0
Rename "Special Topics" to "VM-specific Concerns"
wks 2dd16e2
Define finalizer and weak references
wks e038fc1
WIP: Use "retain" instead of "resurrect"
wks 30f5bfc
Use "retain" instead of "resurrect"
wks b07dbd9
Minor changes
wks 1d67c22
Merge branch 'master' into feature/weak-final-guide
wks abf91d1
Minor changes
wks aa0930d
Clarify the scope and return value of is_reachable
wks 8b2b89d
Use a mock test to host the example code.
wks caefc0c
Fix typo
wks 540f477
Link to .md instead of .html
wks f7df472
Merge branch 'master' into feature/weak-final-guide
wks 5a9bd5f
Define emergency GC in the glossary
wks 1aa4511
Address comments from Kunal
wks fb47ae5
Added more inter-section links
wks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Glossary | ||
|
||
This document explains basic concepts of garbage collection. MMTk uses those terms as described in | ||
this document. Different VMs may define some terms differently. Should there be any confusion, | ||
this document will help disambiguating them. We use the book [*The Garbage Collection Handbook: The | ||
Art of Automatic Memory Management*][GCHandbook] as the primary reference. | ||
|
||
[GCHandbook]: https://gchandbook.org/ | ||
|
||
## Object graph | ||
|
||
Object graph is a graph-theory view of the garbage-collected heap. An **object graph** is a | ||
directed graph that contains *nodes* and *edges*. An edge always points to a node. But unlike | ||
conventional graphs, an edge may originate from either another node or a *root*. | ||
|
||
Each *node* represents an object in the heap. | ||
|
||
Each *edge* represents an object reference from an object or a root. A *root* is a reference held | ||
in a slot directly accessible from [mutators][mutator], including local variables, global variables, | ||
thread-local variables, and so on. A object can have many fields, and some fields may hold | ||
references to objects, while others hold non-reference values. | ||
|
||
An object is *reachable* if there is a path in the object graph from any root to the node of the | ||
object. Unreachable objects cannot be accessed by [mutators][mutator]. They are considered | ||
garbage, and can be reclaimed by the garbage collector. | ||
|
||
[mutator]: #mutator | ||
|
||
## Mutator | ||
|
||
TODO | ||
|
||
## Emergency Collection | ||
|
||
Also known as: *emergency GC* | ||
|
||
In MMTk, an emergency collection happens when a normal collection cannot reclaim enough memory to | ||
satisfy allocation requests. Plans may do full-heap GC, defragmentation, etc. during emergency | ||
collections in order to free up more memory. | ||
|
||
VM bindings can call `MMTK::is_emergency_collection` to query if the current GC is an emergency GC. | ||
During emergency GC, the VM binding is recommended to retain fewer objects than normal GCs, to the | ||
extent allowed by the specification of the VM or the language. For example, the VM binding may | ||
choose not to retain objects used for caching. Specifically, for Java virtual machines, that means | ||
not retaining referents of [`SoftReference`][java-soft-ref] which is primarily designed for | ||
implementing memory-sensitive caches. | ||
|
||
[java-soft-ref]: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/ref/SoftReference.html | ||
|
||
<!-- | ||
vim: tw=100 ts=4 sw=4 sts=4 et | ||
--> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# VM-specific Concerns | ||
|
||
Every VM is special in some way. Because of this, some VM bindings may use MMTk features not | ||
usually used by most VMs, and may even deviate from the usual steps of integrating MMTk into the VM. | ||
Here we provide special guides to cover such cases. |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/vm/tests/mock_tests/mock_test_doc_weakref_code_example.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
//! This module tests the example code in `Scanning::process_weak_refs` and `weakref.md` in the | ||
//! Porting Guide. We only check if the example code compiles. We cannot actually run it because | ||
//! we can't construct a `GCWorker`. | ||
|
||
use crate::{ | ||
scheduler::GCWorker, | ||
util::ObjectReference, | ||
vm::{ObjectTracer, ObjectTracerContext, Scanning, VMBinding}, | ||
}; | ||
|
||
use super::mock_test_prelude::MockVM; | ||
|
||
#[allow(dead_code)] // We don't construct this struct as we can't run it. | ||
struct VMScanning; | ||
|
||
// Just to make the code example look better. | ||
use MockVM as MyVM; | ||
|
||
// Placeholders for functions supposed to be implemented byu the VM. | ||
mod my_vm { | ||
use crate::util::ObjectReference; | ||
|
||
pub fn get_finalizable_object() -> Vec<ObjectReference> { | ||
unimplemented!() | ||
} | ||
|
||
pub fn set_new_finalizable_objects(_objects: Vec<ObjectReference>) {} | ||
|
||
pub fn enqueue_finalizable_object_to_be_executed_later(_object: ObjectReference) {} | ||
} | ||
|
||
// ANCHOR: process_weak_refs_finalization | ||
impl Scanning<MyVM> for VMScanning { | ||
fn process_weak_refs( | ||
worker: &mut GCWorker<MyVM>, | ||
tracer_context: impl ObjectTracerContext<MyVM>, | ||
) -> bool { | ||
let finalizable_objects: Vec<ObjectReference> = my_vm::get_finalizable_object(); | ||
let mut new_finalizable_objects = vec![]; | ||
|
||
tracer_context.with_tracer(worker, |tracer| { | ||
for object in finalizable_objects { | ||
if object.is_reachable() { | ||
// `object` is still reachable. | ||
// It may have been moved if it is a copying GC. | ||
let new_object = object.get_forwarded_object().unwrap_or(object); | ||
new_finalizable_objects.push(new_object); | ||
} else { | ||
// `object` is unreachable. | ||
// Retain it, and enqueue it for postponed finalization. | ||
let new_object = tracer.trace_object(object); | ||
my_vm::enqueue_finalizable_object_to_be_executed_later(new_object); | ||
} | ||
} | ||
}); | ||
|
||
my_vm::set_new_finalizable_objects(new_finalizable_objects); | ||
|
||
false | ||
} | ||
|
||
// ... | ||
// ANCHOR_END: process_weak_refs_finalization | ||
|
||
// Methods after this are placeholders. We only ensure they compile. | ||
|
||
fn scan_object<SV: crate::vm::SlotVisitor<<MockVM as VMBinding>::VMSlot>>( | ||
_tls: crate::util::VMWorkerThread, | ||
_object: ObjectReference, | ||
_slot_visitor: &mut SV, | ||
) { | ||
unimplemented!() | ||
} | ||
|
||
fn notify_initial_thread_scan_complete(_partial_scan: bool, _tls: crate::util::VMWorkerThread) { | ||
unimplemented!() | ||
} | ||
|
||
fn scan_roots_in_mutator_thread( | ||
_tls: crate::util::VMWorkerThread, | ||
_mutator: &'static mut crate::Mutator<MockVM>, | ||
_factory: impl crate::vm::RootsWorkFactory<<MockVM as VMBinding>::VMSlot>, | ||
) { | ||
unimplemented!() | ||
} | ||
|
||
fn scan_vm_specific_roots( | ||
_tls: crate::util::VMWorkerThread, | ||
_factory: impl crate::vm::RootsWorkFactory<<MockVM as VMBinding>::VMSlot>, | ||
) { | ||
unimplemented!() | ||
} | ||
|
||
fn supports_return_barrier() -> bool { | ||
unimplemented!() | ||
} | ||
|
||
fn prepare_for_roots_re_scanning() { | ||
unimplemented!() | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.