Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Memory leak? Memory from data references not being released

agustin_cusse
Explorer

Hi All,

I have been doing some perfomance upgrade on some reports and I have encountered a bug regarding memory allocation.
The issue is the following. When we create a data reference

CREATE DATA <ref> type <whatever type>,

the memory for this reference is allocated. This is of course expected. The problem is that when we do a FREE to this data reference, the allocated memory for this reference remains allocated and there seems to be no way of releasing it whatsoever.

This gets specially dangerous when we create references to structured types since the memory allocated is proportional to the size of the data type.  

This happens regardless if the reference is generic or explicit, for example:

R_DATA type ref to DATA.

R_DATA type ref to BKPF.

(I have tried explicitly triggering the garbage collector to ensure memory release.)

I used the debugger's memory analysis tool to evaluate usage at different points. Total session memory can also be checked at SM04.

For example (this program allocates large amount of data in memory and then tries to release it):

* This structure generates leaks once data ref is created
types: begin of tys_data_leak,
            f1(10) type c,
            r_DATA type REF TO DATA, "type ref to BKPF shows the same issue
           END OF TYS_DATA_LEAK,
           tyt_data_leak type STANDARD TABLE OF tys_data_leak.


CLASS lcl_test DEFINITION.
* either with T_DATA as an attribute or as part of the class, same issue
* to check just set or remove r_data from the structure
* once r_data is referenced, there is no way of deallocating the memory
PUBLIC SECTION.

DATA: t_data_leak TYPE tyt_data_leak.

METHODS alloc_mem_leak.
METHODS free_mem_leak.

ENDCLASS.

CLASS lcl_test IMPLEMENTATION.
METHOD alloc_mem_leak.
DATA: ls_data TYPE LINE OF tyt_data_leak.
BREAK-POINT.
DO 100000 TIMES.
ls_data-f1 = 'AAAAAAAAAA'.
CREATE DATA ls_data-r_data TYPE bkpf. "
INSERT ls_data INTO TABLE t_data_leak.
ENDDO.

ENDMETHOD.

METHOD free_mem_leak.

BREAK-POINT.
*1 Try just freeing the table. nope
*2 Tried individually freeing all references. nope
*3 Tried Freeing the data with FS-->This does not work (and why should it?). nope
*4 Tried freeing the reference (in case of generic) and then replacing it with a new one to a smaller type .nope

LOOP AT t_data_leak ASSIGNING FIELD-SYMBOL(<ls_data>).
CLEAR <ls_data>-r_data.
FREE <ls_data>-r_data.
ENDLOOP.

FREE t_data_leak.
cl_abap_memory_utilities=>do_garbage_collection( ).
COMMIT WORK AND WAIT."<<unnecesary
ENDMETHOD.

ENDCLASS.

After executing method free_mem_leak memory is still allocated. Memory
This does not happen with non-ref types. (allocated and session memory decreases to the current-used)

agustin_cusse_0-1708943595441.png

Any ideas? I have tried several techniques with no success.

*1 Just freeing the table. nope
*2 Tried individually freeing all references. nope
*3 Tried Freeing the data with FS-->This does not work (and why should it?). nope
*4 Tried freeing the reference (in case of generic) and then replacing it with a new one to a smaller type .nope

*5 Also tried typing the table with different key settings including standard and empty key, in case there is somehow an obscure implicit reference in the table key

Thanks!

 

 

 

3 REPLIES 3

Sandra_Rossi
Active Contributor
0 Kudos

You can find explanation between "used" and "allocated" here:

649327 - Analysis of memory consumption - SAP for Me

I guess "allocated" is explained with this note.

But I wonder why the "used memory" doesn't revert back exactly to its starting point after FREE + garbage collection. In my 7.40 system, it jumps from 1MB to 380MB, then go back to 9MB after FREE + GC, I don't know why it doesn't go back to 1MB.

0 Kudos

Hi Sandra,

I understand according to this note, the allocation is expected behaviour when a reference is bound, but it seems as if they forgot to deallocate when a reference is destroyed.  That memory remains allocated until the session ends. (check SM04)

agustin_cusse_0-1708962734816.png

"To avoid frequent reallocation, you will, in general, allocate a little more memory than is needed at the moment"

PS: Thanks for the tip

At least, the memory allocated to the program can be used by the program for anything else.

But I guess it cannot be used by other programs during the program execution.

It's probably something to keep in mind when creating a program, that the same memory size should be used during a program run. It's what is usually done in a batch program (i.e. working with packages of same size), but to be honest I didn't know this special case.

I'd like to hear from "memory experts".