ABAP Keyword Documentation → ABAP - Reference → Declarative statemnts → 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 point to objects or parts of objects in the heap
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.
- Heap references point to objects or parts of objects in the heap
GET REFERENCE
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 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 logical expression IS BOUND
for an invalid reference is wrong.
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.