Optimizing Memory in ABAP: When and How to Use FREE for Internal Tables and Strings

Category
ABAP-Statements
Published
Author
Johannes

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 to NULL.

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. FREE is “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

  • FREE is applied to each component of the structure individually.
  • For components that are internal tables, strings or references, the respective FREE logic applies.
  • For other elementary components (e.g., I, P, C, D), FREE acts like CLEAR.

Elementary Types (except Strings/References)

  • For types like I, P, F, C, N, D, T, X, FREE is functionally identical to CLEAR. It resets the variable to its initial value. There is no additional memory benefit from FREE here.

Distinction from CLEAR

FeatureCLEAR <variable>FREE <variable>
Reset valueYes, to initial valueYes, to initial value
Memory releasePrimarily: Memory of rows for internal tables.More comprehensive: Memory of rows and administration for tables; memory of strings/references.
Application to typesAll typesAll types (but only “more effective” than CLEAR for tables, strings, references)
Main purposeReset valueRelease 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, CLEAR is 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: 500000
Memory (approx.) before FREE: 4.010 KB
Rows after FREE: 0
Memory (approx.) after FREE: 0 KB

2. 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: 0
WRITE: / '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: 2
After FREE - ID: Rows: 0

4. 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.00

In 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.