Skip to Content
Technical Articles
Author's profile photo Ruchira Ushan Sanjeewa

Shared Memory Objects – Transfer data between ABAP programs

Shared memory is a memory area resides in the application server which is accessible to all the ABAP programs. In this post I’ll be discussing the way to access and use that memory area to cater business requirements by using a shared memory object.

Business Requirement

Sharing data between ABAP programs is a common business requirement. There are different ways of achieving that. Let’s take a simple business scenario.

There is a PO (purchase order) creation program which is having the functionality to create PO s in foreground and background. Background job is essential if the user is dealing with multiple PO s. From the technical perspective there are different ways of achieving the above functionality. One  such method is to upload the required data foreground and execute the BAPI call in background via another abap program(Submit program). In such a case, Data should be transferred from the foreground program to background program.

Shared memory object comes handy in such cases.

Shared memory objects

Shared memory is a memory area which is accessible to all ABAP programs in the application server. There are different ways of accessing this memory area . One way of accessing this memory area is using IMPORT and EXPORT statements which is performance wise very inefficient. It is highly recommended to use shared memory objects because it is performance wise efficient than the earlier method.

Benefits of using memory objects

  • Performance wise efficient
  • Provides more flexible way of monitoring objects
  • Can be used accross different LUW(Logical Unit of Work) to access data

 

Let’s see how we can create shared objects.

Transaction to create shared memory object : SHMA

Step by step

 

1 : Creating the Root Class

Go to SE24 and create a new global class (ZCL_SHMA_PO_ROOT1) .It is important to switched on the shared-memory check box in the properties tab of the class.

 

2: Define attributes

Move to the attribute tab of the class and define the attributes which are needed to be shared with other programs. Here 2 tables which contain field list and PO list with a parameter(flag) have been defined as instance attributes.

 

3: Assign Interface to the class

Next move to the interface tab and assign the interface ‘IF_SHM_BUILD_INSTANCE’ to the class and activate the class to confirm whether there are any errors or not.

 

4: SET and GET methods

Add two instance methods to the class ‘SET_ATTRIBUTES’ and ‘GET_ATTRIBUTES’ for writing and reading values to the shared objects.

For ‘SET_ATTRIBUTES’ method add following import parameters

IM_T_PO

IM_T_FIELDCAT

METHOD set_attributes.
 
GT_TABLE[] = IM_T_PO[].
GT_FIELDCATALG[] = IM_T_FIELDCAT[].   

ENDMETHOD.

For ‘GET_ATTRIBUTES’ method add following export parameters

EX_T_PO

EX_T_FIELDCAT


 METHOD get_attributes.

  EX_T_PO = GT_TABLE.
  EX_T_FIELDCAT = GT_FIELDCATALG.
  CLEAR:GT_TABLE,GT_FIELDCATALG.

 ENDMETHOD.

 

5. Create Shared memory area

This is done by tcode:SHMA

Create a memory area name ‘ZCL_SHMA_PO_AREA1’ and provide the root class and constructor class as the above class which we have created earlier ‘ZCL_SHMA_PO_ROOT1’.

Check the boxes ‘Auto Area Build’ , ‘With Versioning ‘.

Select ‘Displacement not possible’ and ‘Auto start if Read Request and for every Invalidation’ from the dropdowns. Refer the below screenshot for more details.

 

6: Implement area constructor ‘IF_SHM_BUILD_INSTANCE~BUILD’ static method in root class ‘ZCL_SHMA_PO_ROOT1’

  METHOD if_shm_build_instance~build.

    DATA:area  TYPE REF TO zcl_shma_po_area1,
         root  TYPE REF TO zcl_shma_po_root1,
         excep TYPE REF TO cx_root.

    DATA: lt_po TYPE zp2p_po_all_fld_tt,
          lt_fieldcat TYPE LVC_T_FCAT.

    TRY.
      area = zcl_shma_po_area1=>attach_for_write( ).

      CATCH cx_shm_error INTO excep.
        RAISE EXCEPTION TYPE cx_shm_build_failed
          EXPORTING
            previous = excep.
    ENDTRY.

    CREATE OBJECT root AREA HANDLE area.

     CALL METHOD root->set_attributes      "Initialize all the tables here
        EXPORTING
           im_t_po  = lt_po    
           im_t_fieldcat = lt_fieldcat.

    area->set_root( root ).

    area->detach_commit( ).

  ENDMETHOD.

 

Function served by the method ‘detach_commit‘  is to update the shared memory area and releasing the lock object.

7: Using the shared memory in the foreground PO creation program to set values to memory area.

  DATA:lo_area TYPE REF TO zcl_shma_po_area1,
       lo_root TYPE REF TO zcl_shma_po_root1.

  TRY.
      zcl_shma_po_area1=>build( ).
    CATCH cx_shma_not_configured.
      RETURN.
    CATCH cx_shm_inconsistent.
      RETURN.
    CATCH cx_shm_build_failed.
      RETURN.
  ENDTRY.

  TRY.
      lo_area = zcl_shma_po_area1=>attach_for_update( ).

    CATCH cx_shm_pending_lock_removed.
      RETURN.
    CATCH cx_shm_change_lock_active.
      RETURN.
    CATCH cx_shm_version_limit_exceeded.
      RETURN.
    CATCH cx_shm_exclusive_lock_active.
      RETURN.
    CATCH cx_shm_inconsistent.
      RETURN.
    CATCH cx_shm_no_active_version.
      RETURN.
  ENDTRY.

  lo_root ?= lo_area->get_root( ).
  IF lo_root IS  INITIAL.
    CREATE OBJECT lo_root AREA HANDLE lo_area.
  ENDIF.

  CALL METHOD lo_root->set_attributes      "Set the attributes here
     EXPORTING
        im_t_po  = lt_po  "comes from foreground program  
        im_t_fieldcat = lt_fieldcat. "comes from foreground program  


  lo_area->set_root( lo_root ).

  lo_area->detach_commit( ).



  

 

7: Using the shared memory in the background PO creation program to read values from the memory area.

    TRY.
      area = zcl_shma_po_area1=>attach_for_read( ).
    ENDTRY.


    area->root->get_attributes(
      IMPORTING
          ex_t_po = DATA(lt_po)
          ex_t_fiedcat = DATA(lt_fieldcat)

    ).

    area->detach( ).

 

Hope you have gained a good idea on shared memory objects . Enhance the performance of your program using that.

 

Assigned Tags

      8 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Sandra Rossi
      Sandra Rossi

      Does your article add something to this other blog post about Shared Objects: https://blogs.sap.com/2012/04/02/shared-memory-enabled-classes-and-create-data-area-handle/? Thank you.

      Author's profile photo Sap Blogs
      Sap Blogs

      Yeah correct both articles look the same. A bit of new syntax in this article and that's what i see as a difference.

      More research on similar articles could prevent duplicated content. 

      But the article given on your link is much more clear. 

      Author's profile photo Ruchira Ushan Sanjeewa
      Ruchira Ushan Sanjeewa
      Blog Post Author

      Thank you for your interest. In this article,  way to use shared objects to cater a business requirement(Practicle business scanario) has been analysed rather than explaining just the steps to create a memory object with more simplified version of coding.

      Author's profile photo Sandra Rossi
      Sandra Rossi

      Thank you.

      Author's profile photo Chairat (Par) Onyaem
      Chairat (Par) Onyaem

      My understanding is that shared memory object resides on each server separately, not shared across the servers, as it resides in RAM.

      So the use case for shared memory object is for caching and may not good for sharing between processes.

      Author's profile photo Matthew Billingham
      Matthew Billingham

      That's correct. Shared memory is appserver specific.

      Author's profile photo Ajeet Singh
      Ajeet Singh

      Good doc, explained in very easy language. Keep sharing

      Author's profile photo Patel Bhesh Kumar
      Patel Bhesh Kumar

      Thank you very much Sanjeewa, great help. I found it very useful and used it for a much needed requirement. Keep posting such good ideas no great ideas 🙂  👍👏