The FREE statement in ABAP is used to explicitly release the memory occupied by certain, often memory-intensive data objects and return this memory to the system (more precisely: to the current internal mode/context). At the same time, the variable to which FREE is applied is reset to its type-specific initial value.
The main focus of FREE is on memory management, especially for:
- Internal Tables: Releasing memory for table rows and the internal table administration.
- Strings (
STRING,XSTRING): Releasing memory that holds the content of (potentially long) character or byte strings. - Reference Variables (
REF TO ...): Resetting the reference toNULL.
For simple elementary data types, FREE behaves like CLEAR.
Syntax
1. Releasing a Single Data Object
FREE <data_object>.<data_object>: The name of the variable (especially internal table, string, reference or structure) whose memory should be released.
2. Releasing Multiple Data Objects (Chain Statement)
FREE: <data_object1>, <data_object2>, ..., <data_objectN>.Functionality and Effect on Different Data Types
FREE always resets the variable to its initial value. Additionally, memory is released:
Internal Tables (Standard, Sorted, Hashed)
- All rows are deleted (as with
CLEAR). - The memory allocated for the rows and for the internal table administration (e.g., hash administration, index management) is released. This is the main difference from
CLEAR, which primarily releases the memory of the rows.FREEis “more thorough” here.
Strings (STRING, XSTRING)
- The memory that holds the actual content of the string is released.
- The variable becomes an empty string (
'') or byte string.
Reference Variables (TYPE REF TO ...)
- The reference variable is set to
NULL(no longer points to anything). - If it’s an object reference and this was the last reference to the object, the Garbage Collector can release the object and its memory.
Structures
FREEis applied to each component of the structure individually.- For components that are internal tables, strings or references, the respective
FREElogic applies. - For other elementary components (e.g.,
I,P,C,D),FREEacts likeCLEAR.
Elementary Types (except Strings/References)
- For types like
I,P,F,C,N,D,T,X,FREEis functionally identical toCLEAR. It resets the variable to its initial value. There is no additional memory benefit fromFREEhere.
Distinction from CLEAR
| Feature | CLEAR <variable> | FREE <variable> |
|---|---|---|
| Reset value | Yes, to initial value | Yes, to initial value |
| Memory release | Primarily: Memory of rows for internal tables. | More comprehensive: Memory of rows and administration for tables; memory of strings/references. |
| Application to types | All types | All types (but only “more effective” than CLEAR for tables, strings, references) |
| Main purpose | Reset value | Release memory (+ reset value) |
When Should FREE Be Used?
- When large internal tables or very long strings are no longer needed and memory should be explicitly and as completely as possible released.
- In long-running programs (batch jobs, daemons), where memory leaks or unnecessary memory usage must be avoided.
- In memory-critical application areas.
- For simple elementary variables,
CLEARis usually sufficient and often expresses the intent (reset value) better.
Examples
1. Releasing an Internal Table
DATA: itab TYPE STANDARD TABLE OF i, mem_info_before TYPE abap_memory_utilities=>ty_memory_usage, mem_info_after TYPE abap_memory_utilities=>ty_memory_usage.
DO 500000 TIMES. APPEND sy-index TO itab.ENDDO.
mem_info_before = cl_abap_memory_utilities=>get_memory_usage_by_data( itab ).WRITE: / 'Rows before FREE:', lines( itab ).WRITE: / 'Memory (approx.) before FREE:', mem_info_before-allocated_all / 1024, 'KB'.
FREE itab. " Release memory
mem_info_after = cl_abap_memory_utilities=>get_memory_usage_by_data( itab ).WRITE: / 'Rows after FREE:', lines( itab ).WRITE: / 'Memory (approx.) after FREE:', mem_info_after-allocated_all / 1024, 'KB'.Possible output (memory values are system-dependent!):
Rows before FREE: 500000Memory (approx.) before FREE: 4.010 KBRows after FREE: 0Memory (approx.) after FREE: 0 KB2. Releasing a String
DATA huge_string TYPE string.huge_string = 'Start'.DO 15 TIMES. " Caution, gets very large! huge_string = huge_string && huge_string.ENDDO.WRITE: / 'Length before FREE:', strlen( huge_string ).
FREE huge_string.
WRITE: / 'Length after FREE:', strlen( huge_string ). " Output: 0WRITE: / 'Content after FREE:', |'{ huge_string }'|. " Output: ''3. Releasing a Structure with Table
TYPES: BEGIN OF ty_container, id TYPE string, content TYPE STANDARD TABLE OF i WITH EMPTY KEY, END OF ty_container.DATA container TYPE ty_container.
container-id = 'Container 1'.APPEND 1 TO container-content.APPEND 2 TO container-content.
WRITE: / 'Before FREE - ID:', container-id, 'Rows:', lines( container-content ).
FREE container.
WRITE: / 'After FREE - ID:', container-id, 'Rows:', lines( container-content ).Output:
Before FREE - ID: Container 1 Rows: 2After FREE - ID: Rows: 04. Simple Variable (like CLEAR)
DATA value TYPE p DECIMALS 2 VALUE '123.45'.WRITE: / 'Value before FREE:', value.FREE value.WRITE: / 'Value after FREE:', value. " Output: 0.00In summary: FREE is a specialized tool for memory management in ABAP that goes beyond the functionality of CLEAR by more aggressively releasing memory from internal tables, strings and references. It also resets the variable to its initial value. Use it specifically when memory optimization for these object types is required.