Search code examples
ibm-midrangerpgle

Can ILE RPG program variable values remain in memory between calls?


If I create an ILE RPG program with DFTACTGRP(*NO), my understanding is the program remains in memory until the activation group is destroyed. If I exit the program with LR switched off, do variables get reinitialized on the next call or do they hold their values?


Solution

  • Short Answer
    No, they do not get reinitialized, unless ACTGRP(*New) is set or you reclaim the activation group in some other way.

    Long Answer *INLR is a function of the RPG cycle, not of Activation groups, and should function mostly as expected (except the program is not unloaded), as long as you are using a cycle main program. For linear main programs, all bets are off. The manual says that features dependent on the cycle "may not be specified" in linear modules. The documentation does not specifically indicate directly that *INLR will not function as expected in a linear module, but, you need to know that *INLR is tied to the cycle, and that linear modules do not include the cycle. A linear module is one that uses the MAIN or NOMAIN keyword. Incidently, there are other things that control variable initialization as well as (or instead of) LR.

    If you use ACTGRP(*NEW), a new activation group is created when the program starts, and implicitly ended when the program ends. So the program is closed down every time whether LR is on or off, or even whether the program is a cycle main program, or a linear main program.

    If you use a named activation group, or ACTGRP(*CALLER) and the caller is running in a named activation group, and manually reclaim the activation group when all programs in the activation group are ended, then fields will be initialized. RCLRSC has no affect here.

    If you use the default activation group, or ACTGRP(*CALLER) and the caller is running in the default activation group, and manually RCLRSC, then fields will be initialized. RCLACTGRP has no affect here. Be careful, this can cause issues.

    And finally, This all applies only to variables in the global scope. Local scope variables, those defined within a sub-procedure, are always initialized on entry, and released on return, unless they are defined as static. Static local variables behave like global variables. That is, they are allocated and initialized during the first call of the procedure, and persist across calls.