Skip to main content

Compacting internal memory of SAP tables in ABAP.

How to force-free memory from deleted entries in internal tables in SAP ABAP, since the garbage collector won't touch these. This is only needed in rare occasions and/or when memory fragmentation needs to be avoided.
This method will do it fast, and correctly:

CLASS cl_demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS compact CHANGING ct_tab TYPE ANY TABLE.
ENDCLASS.

CLASS cl_demo IMPLEMENTATION.
  METHOD compact.
    FIELD-SYMBOLS: <lt_buffer>      TYPE ANY TABLE,
                   <lt_buffer_std>  TYPE STANDARD TABLE,
                   <ls_buffer>      TYPE any,
                   <ls_buffer_prev> TYPE any.

    DATA: ltr_buffer TYPE REF TO data,
          lsr_buffer TYPE REF TO data,
          l_kind     TYPE c LENGTH 1.

    " simple case:
    IF ct_tab IS INITIAL.
      FREE ct_tab.
      RETURN.
    ENDIF.

    CREATE DATA ltr_buffer LIKE ct_tab.

    DESCRIBE TABLE ct_tab KIND l_kind.
    CASE l_kind.
      WHEN sydes_kind-standard.

        ASSIGN ltr_buffer->* TO <lt_buffer_std>.
        <lt_buffer_std> = ct_tab.

        " this will create/copy a new instance of the table
        APPEND INITIAL LINE TO <lt_buffer_std>.
        DELETE <lt_buffer_std> INDEX sy-tabix.

        ct_tab = <lt_buffer_std>.

      WHEN OTHERS. " hased / sorted (unique/non-unique)

        ASSIGN ltr_buffer->* TO <lt_buffer>.
        <lt_buffer> = ct_tab.

        LOOP AT <lt_buffer> ASSIGNING <ls_buffer>.
          " this is done only once: work with a actual line copy
          CREATE DATA lsr_buffer LIKE LINE OF ct_tab.
          ASSIGN lsr_buffer->* TO <ls_buffer_prev>.
          <ls_buffer_prev> = <ls_buffer>.

          " this will create/copy a new instance of the table (slow for sorted/hashed)
          DELETE TABLE <lt_buffer> FROM <ls_buffer_prev>.
          INSERT <ls_buffer_prev> INTO TABLE <lt_buffer>.

          EXIT.
        ENDLOOP.

        ct_tab = <lt_buffer>.

    ENDCASE.

    " For immediate garbage collection (when trying to prevent fragmentation):
    " Run: cl_abap_memory_utilities=>do_garbage_collection( ).

  ENDMETHOD.

Comments

Popular posts from this blog

Firefox: Open diverted links in new background tab instead of new window

This has frustrated me for a long time... something I really liked about Opera. Go to 'about:config' url and set: browser.tabs.loadInBackground to true (should be already) browser.tabs.showSingleWindowModePrefs to true browser.tabs.loadDivertedInBac kground to true Then in advanced preferences is a new option: 'Force links that open a new window to:' can be used for immesdiate effect.

My Custom Mechanical 60% Keyboard Build

My Custom Mechanical 60% Keyboard Build All the parts   1x GH60 (Satan) board - $35 61x Cherry Clear switches  - $35 2x Cherry Blue switches - $3 10x Cherry Red switches - $10 1x PCB Stabilizer set (6.25 space) - $6 1x Royal Oak Glam 60% case (Black Walnut) - $78 1x Aluminium 60% plate - $19 1x SA profile Choclatier keycaps - $102 ( base, modifiers, and novelties ) 1x Braided Nylon USB cable - $15 Solder, Tools, Risers, Labor, etc. Total: ± $300 (Other single switch types added as needed) Testing the board I had my helper do the work here. We needed to test the board before applying any solder. Assembling Add the stabilizers first Add the plate (using a few switches as spacers) Then add the rest Solder solder solder... And test again: The casing The casing I received did to have built-in risers: So I added my own: Then Assemble (using temporary spare GMK Carbon Cherry-profile k...

Why torrent low-ratio banning is killing the network and is generally a bad idea.

It seems a growing trend for people to get banned for low share ratios. Now I can understand how this would appeal to some people`s sense of (in)justice as there are people out there who do not understand that not sharing while you download will ultimately make you download yours slower as you supply bits to other people who would otherwise been supplied by the seeder, making it possible for you to download from open slots at the seeder. These are typlically low-intellect `kiddies` who don`t do math and then download hacked clients because they are just stupid. I agree that these people are evil. But putting a share-ratio of 0.9 and higher as mandatory is just insane. (IMHO 0.1 is the absolute maximum) Take the following example (using mathematical induction and taking values to extremes) if I limited my download to 1k/s and my upload to 25k/s (I have a 256/256 connection) my share ratio inexorably tends to go to sub-1.0. In fact, I find that only the T1 people seems to...