Overview
This suggestion calls for adding a way to help developers and code analysis software to hint the garbage collector that a group of objects may no longer be reachable. Such hints would reduce the time spent on the garbage collection cycles and improve system CPU utilization. Those hints would never be taken as a commitment, and will always be checked before being used to deallocate objects.
Methodology
The solution would be based on two components:
-
A new package, gchint, which will allow the developer to hint that a collection of objects is no longer reachable.
-
A code in the garbage collector which would analyze such hints, verify that each hinted collection of objects is indeed unreachable and deallocates them if so
Example
#Web request initialization
request = request_factory()
request.status = status_create()
request.status.request_backpointer = request
/*** request and status point to each other *****/
gchint.hint_manager.define_cycle(request, “request”, request.id)
gchint.hint_manager.define_cycle(request.status, “request”, request.id)
.
.
.
# request and status are no longer reachable, and can be
# deallocated but they will not be deallocated by simple reference
# count #math since they point to each other and form a cycle
gchint.hint_manager.cycle_unreachable(“request”, request.id)
.
.
# GC Code
Iterate on hints
Iterate on all objects of a specific hint
Count total reference count of the object
Count number of references between members of the objects to other
objects in the hinted cycle
If both counts are identical, deallocate
In the above example, we have a hint for two objects, request and status, each has a reference count of 1, and each has one reference to the other object, and hence they are not referenced by any other reference and can be safely deallocated.
Effect
Effective usage of this package would allow a fast object deallocation and reduce the time spent in the full generation scan of the garbage collection, by increasing the number of times the GC process is skipped due to the small number of objects allocated.
Correctness
Note that this is a hint only, and the deallocated object cycle is always verified to have no external references. Furthermore, if the system identifies a number of false hints above a certain threshold from a certain category it can ignore that category.
Cycle Hint Extension
If a hint for a collection of cycles proves wrong, it can be considered to attempt to add more objects using a simple heuristics. For example, if one of these objects contains a reference to an object with reference count being one, this object can be added to the tested cycle. In case such object contains references to other hinted objects a cycle can be detected.
Logging
An optional API for logging cycle detection, which will provide insights to how many of the hints in cycles were indeed deallocated per each category can be provided, especially for debug/test environment. Such statistics can help humans and code pre-processing tools to improve and re-iterate.