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 |
This function can be used to iterate over all the live objects in the heap:
for each object, callback is invoked, providing info about the object's
location in memory, its class, its size and the objects it references.
For each referenced object it's offset from the object address is
reported in the offsets array.
The object references may be buffered, so the callback may be invoked
multiple times for the same object: in all but the first call, the size
argument will be zero.
Note that this function can be only called in the MONO_GC_EVENT_PRE_START_WORLD
profiler event handler.
mono_gc_reference_queue_add
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.
returns false if the queue is scheduled to be freed.
mono_gc_reference_queue_free
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
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 a AppDomain is unloaded, all callbacks for objects belonging to it will be invoked.
returns the new queue.
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
mono_gc_wbarrier_arrayref_copy
mono_gc_wbarrier_generic_nostore
mono_gc_wbarrier_generic_store
mono_gc_wbarrier_generic_store_atomic
mono_gc_wbarrier_object_copy
Write barrier to call when obj is the result of a clone or copy of an object.
mono_gc_wbarrier_set_arrayref
mono_gc_wbarrier_set_field