<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Memory leak? Memory from data references not being released in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/memory-leak-memory-from-data-references-not-being-released/m-p/13619367#M2026761</link>
    <description>&lt;P&gt;You can find explanation between "used" and "allocated" here:&lt;/P&gt;&lt;P&gt;&lt;A href="https://me.sap.com/notes/649327/E" target="_blank" rel="noopener"&gt;649327 - Analysis of memory consumption - SAP for Me&lt;/A&gt;&lt;/P&gt;&lt;P&gt;I guess "allocated" is explained with this note.&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;</description>
    <pubDate>Mon, 26 Feb 2024 18:54:25 GMT</pubDate>
    <dc:creator>Sandra_Rossi</dc:creator>
    <dc:date>2024-02-26T18:54:25Z</dc:date>
    <item>
      <title>Memory leak? Memory from data references not being released</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/memory-leak-memory-from-data-references-not-being-released/m-p/13619067#M2026755</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;I have been doing some perfomance upgrade on some reports and I have encountered a bug regarding memory allocation.&lt;BR /&gt;The issue is the following. When we create a data reference&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;CREATE DATA &amp;lt;ref&amp;gt; type &amp;lt;whatever type&amp;gt;,&lt;BR /&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;This gets specially dangerous when we create references to structured types since the memory allocated is proportional to the size of the data type.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This happens regardless if the reference is generic or explicit, for example:&lt;/P&gt;&lt;P&gt;R_DATA type ref to DATA.&lt;/P&gt;&lt;P&gt;R_DATA type ref to BKPF.&lt;/P&gt;&lt;P&gt;&lt;EM&gt;(I have tried explicitly triggering the garbage collector to ensure memory release.)&lt;BR /&gt;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;I used the debugger's memory analysis tool to evaluate usage at different points. Total session memory can also be checked at SM04.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;For example (this program allocates large amount of data in memory and then tries to release it):&lt;/STRONG&gt;&lt;/P&gt;&lt;LI-CODE lang="abap"&gt;* 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--&amp;gt;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(&amp;lt;ls_data&amp;gt;).
CLEAR &amp;lt;ls_data&amp;gt;-r_data.
FREE &amp;lt;ls_data&amp;gt;-r_data.
ENDLOOP.

FREE t_data_leak.
cl_abap_memory_utilities=&amp;gt;do_garbage_collection( ).
COMMIT WORK AND WAIT."&amp;lt;&amp;lt;unnecesary
ENDMETHOD.

ENDCLASS.&lt;/LI-CODE&gt;&lt;P&gt;After executing method free_mem_leak memory is still allocated. Memory&lt;BR /&gt;This &lt;STRONG&gt;does not happen with non-ref types&lt;/STRONG&gt;. (allocated and session memory decreases to the current-used)&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="agustin_cusse_0-1708943595441.png" style="width: 400px;"&gt;&lt;img src="https://community.sap.com/t5/image/serverpage/image-id/71091iBBFC127E721809A6/image-size/medium?v=v2&amp;amp;px=400" role="button" title="agustin_cusse_0-1708943595441.png" alt="agustin_cusse_0-1708943595441.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Any ideas? I have tried several techniques with no success.&lt;/P&gt;&lt;P&gt;*1 Just freeing the table. nope&lt;BR /&gt;*2 Tried individually freeing all references. nope&lt;BR /&gt;*3 Tried Freeing the data with FS--&amp;gt;This does not work (and why should it?). nope&lt;BR /&gt;*4 Tried freeing the reference (in case of generic) and then replacing it with a new one to a smaller type .nope&lt;/P&gt;&lt;P&gt;*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&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Feb 2024 13:50:20 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/memory-leak-memory-from-data-references-not-being-released/m-p/13619067#M2026755</guid>
      <dc:creator>agustin_cusse</dc:creator>
      <dc:date>2024-02-26T13:50:20Z</dc:date>
    </item>
    <item>
      <title>Re: Memory leak? Memory from data references not being released</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/memory-leak-memory-from-data-references-not-being-released/m-p/13619367#M2026761</link>
      <description>&lt;P&gt;You can find explanation between "used" and "allocated" here:&lt;/P&gt;&lt;P&gt;&lt;A href="https://me.sap.com/notes/649327/E" target="_blank" rel="noopener"&gt;649327 - Analysis of memory consumption - SAP for Me&lt;/A&gt;&lt;/P&gt;&lt;P&gt;I guess "allocated" is explained with this note.&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Mon, 26 Feb 2024 18:54:25 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/memory-leak-memory-from-data-references-not-being-released/m-p/13619367#M2026761</guid>
      <dc:creator>Sandra_Rossi</dc:creator>
      <dc:date>2024-02-26T18:54:25Z</dc:date>
    </item>
    <item>
      <title>Re: Memory leak? Memory from data references not being released</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/memory-leak-memory-from-data-references-not-being-released/m-p/13619686#M2026771</link>
      <description>&lt;P&gt;Hi Sandra,&lt;/P&gt;&lt;P&gt;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.&amp;nbsp; That memory remains allocated until the session ends. (check SM04)&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="agustin_cusse_0-1708962734816.png" style="width: 400px;"&gt;&lt;img src="https://community.sap.com/t5/image/serverpage/image-id/71398iEDA249615D73764D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="agustin_cusse_0-1708962734816.png" alt="agustin_cusse_0-1708962734816.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;"To avoid frequent reallocation, you will, in general, allocate a little more memory than is needed at the moment"&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;PS: Thanks for the tip&lt;/P&gt;</description>
      <pubDate>Mon, 26 Feb 2024 15:52:44 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/memory-leak-memory-from-data-references-not-being-released/m-p/13619686#M2026771</guid>
      <dc:creator>agustin_cusse</dc:creator>
      <dc:date>2024-02-26T15:52:44Z</dc:date>
    </item>
    <item>
      <title>Re: Memory leak? Memory from data references not being released</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/memory-leak-memory-from-data-references-not-being-released/m-p/13619949#M2026783</link>
      <description>&lt;P&gt;At least, the memory allocated to the program can be used by the program for anything else.&lt;/P&gt;&lt;P&gt;But I guess it cannot be used by other programs during the program execution.&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;I'd like to hear from "memory experts".&lt;/P&gt;</description>
      <pubDate>Mon, 26 Feb 2024 19:18:44 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/memory-leak-memory-from-data-references-not-being-released/m-p/13619949#M2026783</guid>
      <dc:creator>Sandra_Rossi</dc:creator>
      <dc:date>2024-02-26T19:18:44Z</dc:date>
    </item>
  </channel>
</rss>

