Memory Organization and Sessions
|
This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production. |
When you log on to an SAP system and start work, the system does not give your program the whole machine. It gives you a small,
well-defined set of memory areas, and the rules about which program can see which area decide whether a value set in one
program is still there when another one runs. This page explains those areas in plain words: the session you log on with, the
windows you open inside it, the program contexts that stack up when one program starts another, and the three ways to hand
data from one program to the next. It is written for Standard ABAP release 8.16 and ABAP for Cloud Development release 9.20
(2608). The session model itself applies to every ABAP system. The statements that start other programs by name
(SUBMIT, CALL TRANSACTION), the user-wide parameter memory and the ABAP memory statements are documented for
classic, Standard ABAP; the ABAP Cloud Keyword Documentation has no entry for SUBMIT or SET PARAMETER, and its
EXPORT offers only a byte string or an internal table as target, so there you reach other code through released
classes and services instead. After reading you can predict where a value will survive and where it will vanish.
Before you read
-
Getting Started: the three tiers of an application server and what a work process is.
-
Basic ABAP statements such as
DATAandWRITE, taught on the data types page of this section (Data Types and Data Objects).
Key terms
-
User session: everything that belongs to one logon of one user to one application server.
-
Main session: one window of a user session, called an ABAP session in the Keyword Documentation; it holds its own stack of programs.
-
Internal session: the memory context created by one program call inside a main session, holding the data of the programs loaded into it.
-
ABAP memory: a memory area shared by all internal sessions of one main session and by nobody else.
-
SAP memory: the user memory of key-value parameters (SPA/GPA parameters) that all main sessions of one user session can access.
-
Shared memory: an area of an application server instance that many users and programs can access, used for data that is expensive to build.
-
Program group: a main program, such as a loaded class pool or function pool, together with the programs loaded for it inside one internal session.
Sessions, from the outside in
Think of the memory a user can see as nested boxes.
-
The outermost box is the user session. It starts when the user logs on and ends at logoff. Anything stored in it with a user-wide scope is visible from every window the user has open.
-
Inside it are the main sessions, which the Keyword Documentation calls ABAP sessions. In SAP GUI you can open an additional one by entering a transaction code after
/oin the command field. Main sessions of the same user are independent of each other, so a variable in one window is invisible in another. The documentation states a maximum of 16 main sessions per user session, set by the profile parameterrdisp/max_alt_modes(default 16). -
Inside a main session lives a stack of internal sessions. Each call of an ABAP program creates a new internal session in which the called program is loaded, and there can be at most nine internal sessions in one main session. When an internal session is left, it is closed and its data can no longer be reached.
What an internal session holds
Each internal session has a session memory, which the documentation describes as divided into a heap and a stack. The modifiable data objects of the loaded programs live there, while the unchanging parts of the programs are held in a memory area shared by the whole application server instance. When a work process serves a program, the data of its session is rolled in, and it is rolled out when the work process is released. You never address this from ABAP, but it explains two facts that matter every day.
-
The data objects of a program belong only to that program and are visible only there. A variable of the calling program is not visible to the called one, so you must pass values on purpose through parameters, ABAP memory or a database table.
-
When an internal session is left, it is closed and nothing in it can be reached any more. Nothing is kept unless you stored it somewhere outside the internal session first.
Several programs can be loaded into one internal session, organised in program groups. Calling an executable program with
SUBMIT or a transaction code creates the main program group, whose main program is the program called. Each time a class
pool or a function pool is loaded because another program uses it, an additional program group is created for it and kept
for the whole life of the internal session. A loaded program keeps its data objects after control returns to the caller, so
a function module can find a value from its earlier call as long as the same internal session is still running.
How programs open new internal sessions
Calling a program by name from another program stacks a new internal session. The calling statements differ in whether the caller continues afterwards.
| Statement | What happens to the caller | Availability |
|---|---|---|
|
The calling program is interrupted, not ended; the report runs and control comes back to the caller afterwards. |
Standard ABAP documentation |
|
The calling program and its SAP LUW are ended; the submitted report does not return to it. |
Standard ABAP documentation |
|
The transaction’s program runs in a new internal session of the current call sequence. |
Standard ABAP documentation |
|
The whole current call sequence is exited: all earlier internal sessions are deleted and the ABAP memory is deleted too. |
Standard ABAP documentation |
Method or function module call |
No new internal session: the code runs in the caller’s internal session (a class or function pool is loaded as an additional program group). |
Both |
A new main session is opened by the user (for example with /o) or by the system, not by a normal ABAP call. Each main
session has its own ABAP memory.
Three places to share data
When a value has to survive a program call, pick the memory area whose reach matches what you need.
| Area | Who can read it | Typical statements | Lifetime |
|---|---|---|---|
ABAP memory |
The programs of one call sequence in one main session |
|
As long as the top-level transaction of the call sequence remains, or until freed |
SAP memory (user memory) |
All main sessions of one user session |
|
The whole duration of the user session |
Shared memory |
Programs of one application server instance, across users |
Shared objects (area classes), |
Depends on the area and on the instance |
The statements in the table are documented in the Standard ABAP Keyword Documentation. The ABAP Cloud Keyword Documentation
has no SET PARAMETER or SUBMIT entry and lists only a byte string and an internal table as targets of EXPORT, so
in ABAP Cloud you pass data through method parameters instead. Check the statement in the documentation of your language
version before you rely on it.
ABAP memory between two programs
The following sketch passes the number of an expense claim from a calling report to a report it submits. This listing requires an SAP system; not run here.
REPORT zexp_caller.
DATA claim_id TYPE c LENGTH 10 VALUE 'C000000042'.
* Put the value into ABAP memory under a name both programs agree on.
EXPORT claim_id = claim_id TO MEMORY ID 'ZEXP_CLAIM_ID'.
* The called report runs in a new internal session and returns to this one.
SUBMIT zexp_callee AND RETURN.
* After the call we can still read our own copy; clean up the shared slot.
DELETE FROM MEMORY ID 'ZEXP_CLAIM_ID'.
Source: EXPORT, medium. DELETE FROM MEMORY ID is the documented statement for removing a data cluster. FREE MEMORY ID has the same effect, but the documentation marks FREE MEMORY without ID, which deletes every cluster of the ABAP memory, as obsolete, and recommends DELETE FROM because there ID is mandatory.
The second program also requires an SAP system; not run here.
REPORT zexp_callee.
DATA claim_id TYPE c LENGTH 10.
* Read the value written by the caller. sy-subrc is not 0 when no such data cluster exists.
IMPORT claim_id = claim_id FROM MEMORY ID 'ZEXP_CLAIM_ID'.
IF sy-subrc = 0.
WRITE: / 'Claim received:', claim_id.
ELSE.
WRITE: / 'No claim passed in ABAP memory.'.
ENDIF.
Source: FREE MEMORY and EXPORT, medium
If the callee were started from a different main session, the import would fail, because that session has its own ABAP memory.
SAP memory for user-wide defaults
SAP memory holds short values stored under a parameter ID of up to 20 characters. The documentation calls them SPA/GPA
parameters or SET/GET parameters, and the user memory that holds them is reachable from all main sessions of a user session.
A program can only set or read a parameter whose name is entered in the table TPARA, so a parameter ID of your own must be created there first. A program works on a local copy of the parameters, which is copied to the user memory when the program is rolled out,
for example when it ends or calls another program with SUBMIT. The statements are documented for Standard ABAP. This
listing requires an SAP system; not run here.
DATA claim_ref TYPE c LENGTH 10.
* Store a value under a parameter ID. The ID must exist in the table TPARA
* (the example assumes an entry ZEXP_CLAIM, created beforehand).
SET PARAMETER ID 'ZEXP_CLAIM' FIELD 'C000000042'.
* Possibly in another window or program of the same user:
GET PARAMETER ID 'ZEXP_CLAIM' FIELD claim_ref.
Shared memory for data many users need
Data that is costly to build and rarely changes, such as a table of currencies or a parsed configuration, can be placed once
in shared memory and read by many programs without each building its own copy. Every application server instance has its
own shared memory, which the runtime also uses for programs and buffers, so the documentation warns that it can be a scarce
resource. Shared objects are instances of classes, or anonymous data objects, kept in that memory and managed through
areas that you maintain with transaction SHMA. Each area is linked to a global area root class, which must be declared
shared-memory-enabled. Programs reach an area through the generated area class, a subclass of CL_SHM_AREA: its attach
methods bind the program to an area instance and return an area handle. Because the data is shared, the design must decide
who may change it and when it is rebuilt. See Shared memory
for the term and the Keyword Documentation page on shared objects for the details.
Older material you may meet
Books and code from before ABAP 7.40 describe the same session model, and it has not changed in substance. What has changed
is the surroundings. Older programs often rely heavily on SAP memory parameters and on SUBMIT or CALL TRANSACTION
to pass data between screens, on global variables of function groups as hidden state, and on EXPORT/IMPORT of whole
tables with TO MEMORY. New development, especially in ABAP Cloud, passes data through method parameters, returns it from
released classes and keeps state in the database or in managed application objects. The older constructs are still
documented for Standard ABAP, which is why you will meet them when you maintain existing programs.
Summary
-
A user session holds main sessions; a main session holds up to nine internal sessions, one per program call.
-
Each internal session has its own data; a called program does not see the caller’s variables, and its data disappears when it ends.
-
A program group is a main program, such as a class pool or function pool, plus the programs loaded for it, inside one internal session.
-
ABAP memory reaches the programs of one call sequence in one main session; SAP memory reaches every main session of one user; shared memory reaches all users of one application server instance.
-
SUBMIT,SET PARAMETERandEXPORT ... TO MEMORY IDare documented for classic ABAP; in ABAP Cloud pass data through parameters and released interfaces.