ABAP Keyword Documentation →  ABAP − Reference →  Declarations →  Declaration Statements →  Data Types and Data Objects →  Types and Objects - Overview →  Details of Special Data Types or Data Objects →  Reference Types → 

Heap References and Stack References

The objects referenced by references can be located in the heap or in the stack. Objects in the heap are instances of classes or are anonymous data objects. Objects in the stack are the global data objects of programs and the local data objects of procedures stacked there.

Heap references are object references or data references. They are created either when new objects are created in the heap (using the statements CREATE OBJECT or CREATE DATA) or by applying the statement GET REFERENCE or the addition REFERENCE INTO to data objects in the heap. Heap references are always memory-preserving, which means that, as long as a heap reference points to an object or a part of an object, then the object is not deleted by Garbage Collector.
Stack references are always data references and are created only by applying the statement GET REFERENCE, the reference operator REF, or the addition REFERENCE INTO to data objects in the stack stack. Stack references are not memory-preserving.

A non-initial object reference always points to an instance of a class, which means it is always a heap reference. A data reference can be a heap reference or a stack reference:

A part of a data object is either a component of a structure, a row of an internal table, or a section specified by an offset/length.

Heap references preserve the memory and cannot become invalid, with one exception: A heap reference to a row in an internal table becomes invalid if the table row is deleted. Stack references also become invalid if the referenced data object is removed from the stack (that is, if the associated procedure is ended). The latter also applies to the local data in instance methods. The predicate expression IS BOUND for an invalid reference is false.

Notes