An object in the system class CL_ABAP_WEAK_REFERENCE represents a weak reference to an object in a class. Unlike regular object references, a weak reference is ignored when the garbage collector runs. This means that a weak reference does not prevent the referenced object from being deleted when the garbage collector is executed.
A weak reference to an existing object is created by passing an object reference to the instance constructor of CL_ABAP_WEAK_REFERENCE. The functional method GET can then be used to retrieve the reference afterwards. If the object was deleted in the meantime, the return value is initial.
A different type of reference retains objects until the available memory becomes limited. The class CL_ABAP_SOFT_REFERENCE is designed for these soft references, but this class is currently still implemented in the same way as class CL_ABAP_WEAK_REFERENCE.
A weak reference to the object of the object reference variable
oref is set
and the latter is then deleted. The weak reference points to the object until the garbage collector is executed. The return value is initial until the garbage collector is called explicitly.
CLASS demo DEFINITION. ENDCLASS. START-OF-SELECTION. DATA(oref) = NEW demo( ). DATA(wref) = NEW cl_abap_weak_reference( oref ). CLEAR oref. IF wref->get( ) IS NOT INITIAL. cl_demo_output=>write( 'Weak reference not initial' ). ENDIF. cl_demo_output=>write( 'Garbage collection' ). cl_abap_memory_utilities=>do_garbage_collection( ). IF wref->get( ) IS INITIAL. cl_demo_output=>write( 'Weak reference initial' ). ENDIF. cl_demo_output=>display( ).