Home > AS/400 Tips > iSeries administrator tips > Dynamic memory in V5: Harness the power -- part 1
iSeries 400 Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ISERIES ADMINISTRATOR TIPS

Dynamic memory in V5: Harness the power -- part 1


Ron Turull
07.18.2006
Rating: -4.00- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



Ron Turull
Report Program Generator (RPG) programmers have been able to use dynamic memory (or "heap storage" as IBM likes to call it) for a number of years now, but not until V5 hit the streets had it been fully implemented. With RPG's dynamic memory built-in functions, working with dynamic memory is easier and more intuitive than ever. This is powerful stuff -- it puts you in charge! It is also necessary in order to use many of the new technologies IBM has put into the iSeries (AS/400), for example, sockets, multi-threading and Java integration.

More Information

The evolution of dynamic memory

For years, the C language's dynamic memory support gave C programmers the ability to, for example, dynamically (i.e., during run-time) build chains of data structures and grow and shrink arrays at will. Since V3R2, Integrated Language Environment (ILE) RPG and ILE COBOL programmers have also been able to take advantage of dynamic memory, using the ILE dynamic memory APIs. But even though these APIs are relatively simple, they are still complicated by extraneous parameters that could have been left off or left to another API.

A couple of years later, in V4R2, came the introduction of the dynamic memory operation codes in RPG. This greatly simplified using dynamic memory, as the RPG op-codes were much more intuitive than the APIs.

Finally, with V5, the implementation of the dynamic memory support in RPG has reached an apex. In addition to the fixed-format op-codes, RPG now supports two dynamic memory built-in functions, %Alloc and %Realloc, and the free-format op-code Dealloc. The relatively new built-in functions make the process of managing dynamic memory a breeze.

Understanding the three kinds of memory

On most computers, there are three common kinds of memory available for use by programs and their variables:

1. Static

2. Automatic

3. Dynamic

The main difference among these three is the way the memory is allocated, initialized, and de-allocated (or freed). It is important to remember that these different types of memory are distinguished logically, not necessarily physically; that is, while they might be located in different storage blocks, they are all kept in the main computer memory (i.e., the RAM).

These three different kinds of memory give rise to three different types of variables:

  • Static variables

  • Automatic variables

  • Dynamic variables

    The memory for every static variable declared in your program is allocated at program initialization (a.k.a. program activation). Currently in RPG, all variables declared in the main source section are static variables (as opposed to variables declared in subprocedures -- or non-main procedures -- which are automatic variables by default). Static variables are initialized when they are allocated; character variables are initialized to blanks and numeric variables are initialized to zero (pointers are initialized to null). Static memory allocated by a program is freed automatically when the program is unloaded (e.g., when an RPG program returns with LR set on -- this is called program deactivation ).

    Memory for automatic variables, sometimes referred to as stack variables, is allocated when an automatic variable comes into scope and is freed when the variable goes out of scope. An automatic variable comes into scope when the procedure in which it is declared is called (i.e., put on the call stack) and it goes out of scope when the procedure returns to its caller (i.e., taken off the call stack). In RPG, variables declared in subprocedures are automatic variables by default, but can be declared as static if you use the static keyword on the associated D-spec.

    The memory for a dynamic variable is allocated and freed manually by your program. For example, if your program needs memory for an array of 100 elements, each two characters long, it would request 200 bytes of dynamic memory. Later, if the program needs to expand the array to 200 elements, it can request that the original 200-byte allocation be increased to 400 bytes.

    Memory for dynamic variables is allocated off an internal memory structure called the heap. The heap is simply a chunk of memory that the system allocates for an activation group when the activation group is created. Any program in the activation group can request storage off that activation group's heap (in a coming installment, we'll discuss 10 important things to remember about heaps).

    We'll discuss RPG's dynamic memory management built-in functions -- and see them in action -- in the next installment.

    -----------------------------------
    About the author: Ron Turull is editor of Inside Version 5. He has more than 20 years' experience programming for and managing AS/400-iSeries systems.


    Rate this Tip
    To rate tips, you must be a member of Search400.com.
    Register now to start rating these tips. Log in if you are already a member.




    Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



    RELATED CONTENT
    RPG iSeries programming
    Enhancing RPG with external SQL stored procedures
    Introduction to SQLRPGLE on IBM i: Making a report
    Making the most of RPG data handling on IBM i
    IBM i shop boosts online sales with RPG-based Web platform
    Migrating from RPG to EGL on IBM i
    Allow access to data from a stored procedure result set using COBOL or RPG
    EGL Rich UI on IBM i: Do you Dojo?
    Programming for the Web on the IBM i, what is possible
    A taste of COMMON: ILE, IBM releases, Web applications and new products
    Documenting nested program structures on the AS/400

    iSeries administrator tips
    Researching high availability for your System i shop
    Translating Linux for IBM i admins: Using GUI to make it easy
    Translating Linux for IBM i admins: Working with jobs and networking
    OpenOffice: What to know before making the transition from Microsoft Office
    OpenOffice: An enterprise open source solution
    Database performance comparisons on IBM i
    Translating Linux for IBM i admins: User profile commands
    Modern System i reports using Client Access
    Tips for installing Lotus Domino server on a System i partition
    The iSeries Blog has a new home on IT Knowledge Exchange

    RELATED GLOSSARY TERMS
    Terms from Whatis.com − the technology online dictionary
    Report Program Generator  (Search400.com)

    RELATED RESOURCES
    2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
    Search Bitpipe.com for the latest white papers and business webcasts
    Whatis.com, the online computer dictionary

    DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



  • iSeries Security - Security Tools, Physical Security and System Security
    HomeNewsTopicsITKnowledge ExchangeTipsBlogsAsk the ExpertsMultimediaWhite PapersProducts
    About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
    SEARCH 
    TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

    TechTarget Corporate Web Site  |  Media Kits  |  Site Map




    All Rights Reserved, Copyright 1999 - 2009, TechTarget | Read our Privacy Policy
      TechTarget - The IT Media ROI Experts