Collect garbage and return an :class:`~refcycle.object_graph.ObjectGraph`
    based on collected garbage.

    The collected elements are removed from ``gc.garbage``, but are still kept
    alive by the references in the graph.  Deleting the
    :class:`~refcycle.object_graph.ObjectGraph` instance and doing another
    ``gc.collect`` will remove those objects for good.

def garbage():
    """
    Collect garbage and return an :class:`~refcycle.object_graph.ObjectGraph`
    based on collected garbage.

    The collected elements are removed from ``gc.garbage``, but are still kept
    alive by the references in the graph.  Deleting the
    :class:`~refcycle.object_graph.ObjectGraph` instance and doing another
    ``gc.collect`` will remove those objects for good.

    """
    with restore_gc_state():
        gc.disable()
        gc.set_debug(gc.DEBUG_SAVEALL)
        collected_count = gc.collect()
        if collected_count:
            objects = gc.garbage[-collected_count:]
            del gc.garbage[-collected_count:]
        else:
            objects = []
        return ObjectGraph(objects)