SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
Development resources, articles, tutorials, code samples, tools and downloads for SAP HANA and ABAP, HANA Database, SQLScript, SAP UI5, Screen Personas, Web Dynpro, Workflow

ABAP Shared Memory Objects Tutorial with Sample ABAP Code

This ABAP Shared Memory Objects tutorial includes sample ABAP code showing how to code to store data in shared memory objects and read data from shared memory object. By following the instructions in this step by step ABAP tutorial, developers can create a shared memory object in SAP. Given sample ABAP codes and sample programs will help developers to write data or read data from shared memory objects using shared memory programming.

ABAP developers can use following SAP transaction codes for Shared Memory Object development, debugging and monitoring:
SE80 or SE24 transaction for ABAP Workbench Class Builder editor to create root class for data structure.
SHMA SAP transaction for creating Shared Object Area class interface.
SHMM to monitor Shared Memory Areas objects, their memory consumption, and view and display data contained in shared memory object

Of course ABAP developers can use transaction SE38 to create two ABAP programs where they set the shared memory object value and read back from shared memory area object.

Steps to Implement ABAP Shared Object Solution

1.) Create a class that represents data structure. (I'll refer this class as root class). Use SE80 or SE24 tcode

2.) Create a shared memory area class to wrap data class for standard shared memory object methods. (I'll refer this class as handle class). Use SHMA transaction

3.) ABAP code to write data to shared memory object. For example you can build a simple ABAP program using SE38 to set shared memory object value.

4.) ABAP code to read data from shared memory object. A similar ABAP report can be written to read data from shared memory object using SE38.

Naming of the root class and the shared memory area is important. The first part of the names of both classes should be same. On the other hand root class name should be ending with "_root" and the shared memory area class name should end with "_area"

In fact this naming simpifies the work of ABAP developer. ABAP programmers will have a better understanding about the objects they're using to build a SAP shared memory object solution.

Root Class for Shared Memory Object

In this section of the ABAP tutorial, we will create a class with Shared Memory-Enabled option is selected.
The class name is Z_VBRK_SHM_TABLE_ROOT for the sake of naming standards.

First of all, call SE80 Workbench transaction code and choose Class / Interface as object type. Then type the name of the new root class for your shared memory object as seen in below screen shot.

SE80 SAP transaction to create shared memory root class

Press Enter key for next dialog

create shared memory object root class

When your approval is requested to create the mentioned class since it does not exist, click Yes to continue to create the new root class for shared memory object.

Choose Class option instead of Interface to indicate that you're creating a class not an interface

create class for shared memory object

Press Enter or OK icon button

Provide class description for the shared memory object root class.
Choose instantiation as Public.
Class type is Usual ABAP class and mark Final checkbox.

usual ABAP class properties for shared memory object

When you are finished with basic class properties, click Save to continue for the SAP Class Builder screen.
Be sure that you do not forget to mark Shared Memory-Enabled checkbox on class properties screen general data section.

ABAP Class Builder screen for Shared-Memory object root class

Now after we mark the class as shared memory-enabled, we will now create an attribute where data will be stored and two class methods to set and get shared data in the class attribute.

Now switch to Attributes tab and define a private attribute where data will be stored.
For example we can create an attribute where ABAP developers can store a list of SAP document numbers in VBELN data type.

When I searched the ABAP dictionary for an existing table type for VBELN data type, I found VBELN_VL_T table type. I'll use VBELN_VL_T table type in this tutorial to create a class attribute for storing SAP document numbers like invoice numbers or order numbers in a shared memory object.

SAP shared memory object attribute definition

After attribute is created, ABAP developers can continue with next step.
In this step, we will create two public methods.
These methods will be used to set data to private attribute and get data (read) from private attribute of the shared memory object.

SAP Shared Memory-Enabled class Set and Get methods

SET_VBELN method has import parameter P_VBELN of type VBELN_VL_T which is a table type for VBELN data element. This method takes internal table of table type VBELN_VL_T to set as shared memory object data content.

Set method parameter for ABAP shared memory class

GET_VBELN method has export parameter P_VBELN_TBL of type VBELN_VL_T which is a table type for document numbers. Using the export method Get_Vbeln, ABAP users will be able to return shared memory object table for all stored document numbers.

Get method for shared memory object class

After the developer defines class attribute and set/get methods of the root class, within the methods following ABAP codes can be used.



Set method assigns the input parameter value as the value of shared memory object attribute. And the Get method returns the attribute value as export parameter. We will see how we will call these shared memory enabled class methods within other ABAP programs later in this ABAP tutorial.

Create Shared Memory Area: Handle Class

This tutorial step demonstrates the creation of the shared memory area.
To create a Shared Memory Area ABAP developers can use SHMA SAP transaction code. Area name should be Z_VBRK_SHM_TABLE_AREA according to the naming rules which will help the developers to reference it without any confusion later

SAP SHMA tcode to create shared memory area

When you press Create button, following Create Area screen will be displayed.
Enter the root class name we have created as shared memory root class in Attributes tab basic properties section. In our tutorial, we have defined the root class name as Z_VBRK_SHM_TABLE_ROOT in previous steps.

shared memory area in ABAP development

If you look at the details of the above Create Area screen, you will realize that an ABAP developer can enable versioning with a Shared Area object. It is also possible to define the maximum shared area object size that will keep on memory in kByte and maximum number of allowed versions.

After you complete Shared Memory Area properties like deciding if you want to use it with Versioning is enabled or not, first press Save icon and then press Release button at top icons menu.

The class category is Area Class (Shared Objects) when you display the shared memory area using SE80. The Root attribute of the handle class is automatically set to previously created root class at step 1.

ABAP Programs

The below ABAP report whose source code is given reads shared memory object value.
Root class attach_for_read method must be called before reading shared memory data using our custom Get_Vbeln method. At the end of the read process, ABAP developers must call detach() method to free shared memory object.


DATA lt_vbeln type VBELN_VL_T.
DATA ls_vbeln like LINE OF lt_vbeln.


lr_handle = Z_VBRK_SHM_TABLE_AREA=>attach_for_read( ).
lr_handle->root->get_vbeln( IMPORTING P_VBELN_TBL = lt_vbeln ).
lr_handle->detach( ).

In order to update data stored in memory using shared memory objects, developers must consider using one of attach_for_write() or attach_for_update() methods.
Following ABAP report is demonstrating how to assign shared memory object value.
First of all, using below ABAP a check is done to see if an active version of the shared memory object exists or not. This check is done by reading the shared memory object.
If cx_shm_no_active_version exception is catched in the ABAP TRY and CATCH syntax, then ABAP developer should use attach_for_write method while creating the root class object handle. Otherwise, attach_for_update method can be used during root creation of the root class handle.

REPORT z_shared_memory_object_sample.

PARAMETERS: pr_vbeln LIKE vbrk-vbeln DEFAULT '1111111111'.
* ABAP program appends p_VBELN parameter value TO Shared Memory Object

DATA lt_vbeln type VBELN_VL_T.
DATA ls_vbeln like LINE OF lt_vbeln.

DATA lr_handle TYPE REF TO z_vbrk_shm_table_area. " Area Class
DATA lr_root TYPE REF TO z_vbrk_shm_table_root. " Root attribute of handle class. SHM: Model of a Data Clas

* First Try to Read
data excp type ref to cx_shm_no_active_version.
data lf_readerror type c1.

 lr_handle = z_vbrk_shm_table_area=>attach_for_read( ).
 lr_handle->root->get_vbeln( IMPORTING P_VBELN_TBL = lt_vbeln ).
 lr_handle->detach( ).
catch cx_shm_no_active_version into excp.
 lf_readerror = 'X'.

APPEND pr_vbeln to lt_vbeln. " append input parameter value to internal table

IF lf_readerror = 'X'.
 lr_handle = z_vbrk_shm_table_area=>attach_for_write( ).
 lr_handle = z_vbrk_shm_table_area=>attach_for_update( ).

CREATE OBJECT lr_root AREA HANDLE lr_handle.
lr_handle->set_root( lr_root ).

lr_root->set_vbeln( p_vbeln = lt_vbeln ). " custom SET method is executed

lr_handle->detach_commit( ).

At the end of the ABAP code block for storing data in shared memory object, COMMIT in database must be executed. Since the ABAP developer should free the SAP shared memory objects, the DETACH process must be executed too.
To detach from shared memory area with saving data in memory variables, detach_commit() method can be called.

Monitor Shared Memory Area using SHMM Transaction

SAP transaction SHMM is used for monitoring shared memory areas for memory consumption and to display data stored in shared memory objects.

When you call SHMM tcode, SAP Shared Memory: Areas screen will be displayed. On the screen on Areas tab which is the default displayed tab, you will see shared memory areas created on the related SAP system.

Highlight the shared memory area which you want to monitor in detail as follows.

monitor shared memory area in SAP system

When you double click the shared memory area, instances of the shared area will be displayed as a list on a new screen.

ABAP share memory area instances

Choose the default instance by highlighting it and click on Read Active Version icon to display shared memory object contents.

In a new SAP screen, we will be displayed root object data in different tabs. The default tab is Explorer. You will see the shared memory object attribute we have created earlier in this tutorial.

display ABAP shared memory object data

Highlight the SAP shared memory class attribute SHM_VBELN_TBL which is a table storing document numbers. Then double click it to view its stored row values.

You see below, tutorial sample shared memory object has two document numbers stored in its class attribute.

ABAP shared memory object storing data

To summarize, in this ABAP tutorial I tried to give brief information about SAP shared memory object and how to use shared memory in your ABAP programs. Using the transaction codes and step by step guide, an ABAP developer can create a sample shared memory root class and the shared memory area for this class. Then given ABAP reports can be used to write, update or read shared memory objects.

ABAP developers can read a more detailed guide for working with ABAP Shared Memory Objects at SAP Community Network portal.


Install SAP Free
CRM Companies List
Web Based CRM Software

Copyright © 2004 - 2021 Eralper YILMAZ. All rights reserved.