Memory efficient ABAP Programming - Boxed Components



One of the important aspects of efficient programming, that is often forgotten by programmers, 
is the code's memory allocation. 
SAP unveiled a new ABAP feature called BOXED Components the later releases of SAP, which is a useful memory conservation technique for ABAP. It is a usefull technique when we have often unused fields in an internal table. 


Boxed Components in the Dictionary Structures


from SAP help "Boxed components are structures that are not saved in the higher-level context itself. Instead, an internal reference that points to the actual structure is stored in place of the structure. A boxed component is always a deep component of its context."


Example: 

In an FI document the reference ( XBLNR ) and document header text ( BKTXT ) are two fields that are often empty. In a standard structure layout the space for this variables is always allocated ( figure 1 ).


Figure 1: Standard structure layout - the text component is always allocated even if empty


Using Boxed components we only allocate the memory for the text structure if at least one of the texts ( Reference and Document header Text ) are not initial ( Figure 2 ).



Figure 2: Boxed Compoments - the memory for the text component is only allocated if at least one of the texts is not initial


Using the Memory Analysis tool in the debugger we can compare both implementations: with boxed components and without boxed components.

Figure 3 - Memory Analysis Tool



*&---------------------------------------------------------------------*
*& Report  ZBOXED_COMPONENTS_00
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT  zboxed_components_00.


TYPES:

  BEGIN OF ty_s_texts,
    xblnr TYPE xblnr1,    " Reference
    bktxt TYPE bktxt,     " Document Header Text
  END OF ty_s_texts,

  BEGIN OF ty_s_boxed,
  bukrs TYPE bukrs,       " Company Code
  belnr TYPE belnr_d,     " Document Number
  gjahr TYPE gjahr,       " Fiscal Year
  texts TYPE ty_s_texts,  " Document Text Structure
  END OF ty_s_boxed.

DATA: lt_normal_table TYPE STANDARD TABLE OF ty_s_boxed.


SELECT bukrs belnr gjahr xblnr bktxt INTO TABLE lt_normal_table FROM bkpf.

BREAK-POINT.
Code - Without Boxed Components


Without Boxed Components



*&---------------------------------------------------------------------*
*& Report  ZBOXED_COMPONENTS_01
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT  zboxed_components_01.

TYPES:

BEGIN OF ty_s_texts,
    xblnr TYPE xblnr1,    " Reference
    bktxt TYPE bktxt,     " Document Header Text
END OF ty_s_texts,

BEGIN OF ty_s_boxed,
  bukrs TYPE bukrs,       " Company Code
  belnr TYPE belnr_d,     " Document Number
  gjahr TYPE gjahr,       " Fiscal Year
  texts TYPE ty_s_texts BOXED,  " Document Text Structure
END OF ty_s_boxed.

DATA: lt_normal_table TYPE STANDARD TABLE OF ty_s_boxed.


SELECT bukrs belnr gjahr xblnr bktxt INTO CORRESPONDING FIELDS OF TABLE lt_normal_table FROM bkpf.

BREAK-POINT.
Code 2 - with boxed components




Using Boxed Components



Comparing both programs using the Memory Analysis tool provided in the ABAP debugger you can see that using BOXED components is one solution to solve the memory problem in ABAP. With boxed components, it consumed about two thirds of the total memory of the implementation that did not use Boxed Components. SAP recommends that we use it for boxed components with more than 100 bites.

It is also important to be aware that if one component of the boxed structure is filled then all of the boxed component memory will be allocated even if the other fields remain empty.


1 comment:

  1. Since we are using into corresponding fields of table in boxed internal table the fields within the boxed structure will always come as blank which is not expected. Please suggest how to select values from data base table into the boxed internal table.

    ReplyDelete