The public interface of the Mono GC is fairly limited, and its the only one that embedders should be using:
mono_gc_collect
generation | GC generation identifier |
Perform a garbage collection for the given generation, higher numbers
mean usually older objects. Collecting a high-numbered generation
implies collecting also the lower-numbered generations.
The maximum value for generation can be retrieved with a call to
mono_gc_max_generation
, so this function is usually called as:
mono_gc_collect (mono_gc_max_generation ());
mono_gc_collection_count
generation | a GC generation number |
Get how many times a garbage collection has been performed for the given generation number.
mono_gc_max_generation
Get the maximum generation number used by the current garbage collector. The value will be 0 for the Boehm collector, 1 or more for the generational collectors.
mono_gc_get_generation
object | a managed object |
Get the garbage collector's generation that object belongs to. Use this has a hint only.
mono_gc_get_heap_size
Get the amount of memory used by the garbage collector.
mono_gc_get_used_size
Get the approximate amount of memory used by managed objects.
mono_gc_walk_heap
flags | flags for future use |
callback | a function pointer called for each object in the heap |
data | a user data pointer that is passed to callback |
MONO_GC_EVENT_PRE_START_WORLD
profiler event handler.mono_gc_reference_queue_add
queue | the queue to add the reference to. |
obj | the object to be watched for collection |
user_data | parameter to be passed to the queue callback |
FALSE
if the queue is scheduled to be freed.
Queue an object to be watched for collection, when the obj is collected, the callback that was registered for the queue will be invoked with user_data as argument.
mono_gc_reference_queue_free
queue | the queue that should be freed. |
This operation signals that queue should be freed. This operation is deferred as it happens on the finalizer thread.
After this call, no further objects can be queued. It's the responsibility of the caller to make sure that no further attempt to access queue will be made.
mono_gc_reference_queue_new
callback | callback used when processing collected entries. |
Create a new reference queue used to process collected objects.
A reference queue let you add a pair of (managed object, user data)
using the mono_gc_reference_queue_add
method.
Once the managed object is collected callback will be called in the finalizer thread with 'user data' as argument.
The callback is called from the finalizer thread without any locks held. When an AppDomain is unloaded, all callbacks for objects belonging to it will be invoked.
The bridge is a mechanism for SGen to let clients override the death of some unreachable objects. We use it in monodroid to do garbage collection across the Mono and Java heaps.
The client can designate some objects as "bridged", which means that they participate in the bridge processing step once SGen considers them unreachable, i.e., dead. Bridged objects must be registered for finalization.
When SGen is done marking, it puts together a list of all dead bridged objects and then does a strongly connected component analysis over their object graph. That graph will usually contain non-bridged objects, too.
The output of the SCC analysis is passed to the `cross_references()` callback. It is expected to set the `is_alive` flag on those strongly connected components that it wishes to be kept alive. The value of `is_alive` will be ignored on any SCCs which lack bridges.
In monodroid each bridged object has a corresponding Java mirror object. In the bridge callback it reifies the Mono object graph in the Java heap so that the full, combined object graph is now instantiated on the Java side. Then it triggers a Java GC, waits for it to finish, and checks which of the Java mirror objects are still alive. For those it sets the `is_alive` flag and returns from the callback.
The SCC analysis is done while the world is stopped, but the callback is made with the world running again. Weak links to bridged objects and other objects reachable from them are kept until the callback returns, at which point all links to bridged objects that don't have `is_alive` set are nulled. Note that weak links to non-bridged objects reachable from bridged objects are not nulled. This might be considered a bug.
mono_gc_register_bridge_callbacks
mono_gc_wait_for_bridge_processing
SGen is a concurrent and generational GC, features which require tracking changes to the state of the heap. This is achieved through write barriers. Whenever native code is changing the state of the heap by storing references into another managed object, it needs to do it using this write barrier API.
mono_gc_wbarrier_arrayref_copy
dest_ptr | destination slot address |
src_ptr | source slot address |
count | number of references to copy |
mono_gc_wbarrier_generic_nostore
mono_gc_wbarrier_generic_store
ptr | address of field |
obj | object to store |
mono_gc_wbarrier_generic_store_atomic
mono_gc_wbarrier_generic_store
but performs the store
as an atomic operation with release semantics.mono_gc_wbarrier_object_copy
obj | destination object |
src | source object |
mono_gc_wbarrier_set_arrayref
arr | array containing the destination slot |
slot_ptr | address of slot inside the array |
value | reference to the object to be stored |
mono_gc_wbarrier_set_field
obj | object containing the destination field |
field_ptr | address of field inside the object |
value | reference to the object to be stored |