Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Declarations →  Declaration Statements →  Data Types and Data Objects →  Types and Objects - Overview →  Data Objects →  References 

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 point to objects or parts of objects in the heap
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 point to objects or parts of objects in the stack
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 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 data reference is a heap reference if it
  • points to an instance attribute or a part of an instance attribute of an object,
  • points to an anonymous data object or a part of an anonymous data object.
  • A data reference that points to a data object or a part of a data object in the stack is 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.

Other versions: 7.31 | 7.40 | 7.54


Notes

  • Field symbols have the same properties with respect to preserving memory and validity as data references. Field symbols to which objects or parts of objects are assigned in the heap are memory-preserving.

  • Internal tables are dynamic data objects and have a special role because they have their own memory management. They allocate and release memory regardless of the statement CREATE and Garbage Collector. This means that heap references to rows in internal tables can become invalid. This is not the case for strings, since no references to substrings of strings are allowed.

  • A reference is classified as a heap reference or a stack reference depending on its value only, and not by the type of the reference variable in which it is stored. In particular, assignments between reference variables do not change the relevant properties of the copied references.

Example

The heap reference heap_ref located on the stack points to an anonymous data object in the heap. The stack reference stack_ref also on the stack points to heap_ref.

DATA(heap_ref)  = NEW i( 111 ). 
DATA(stack_ref) = REF #( heap_ref ).