Static storage with array data structures or multiple-occurrence data structures
Last Post 27 Nov 2012 12:24 PM by Lynne Noll. 4 Replies.
AddThis - Bookmarking and Sharing Button Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
Tzvi Kahn
New Member
New Member
Posts:57

--
26 Nov 2012 05:08 AM
I defined an array data structure with 1000 elements, with 1 field defined as varying:

D MSGS            DS                  DIM(1000) QUALIFIED  

D  MSGS_TXT                   1000    VARYING

After compilation the static storage looks like this:

Static storage size (bytes):                                 

  Minimum  . . . . . . . . . . . . . . . . . . :   1004624   
  Maximum  . . . . . . . . . . . . . . . . . . :   1004624


I changed the number of elements to 2000:

D MSGS            DS                  DIM(2000) QUALIFIED   
D  MSGS_TXT                   1000    VARYING

After compilation the static storage looks like this:

Static storage size (bytes):                               

  Minimum  . . . . . . . . . . . . . . . . . . :   2006624 
  Maximum  . . . . . . . . . . . . . . . . . . :   2006624

The difference between the sizes of the static storage is precisely 1000 elements times the size of each element (2 bytes for the varying field + 1000 bytes per element).


Question 1: Why is the total size allocated in static memory, despite the fact that the field is defined as varying?  This implementation of static size is not varying, it's fixed, with the added disadvantage that 2 more bytes are added for each element, ostensibly to handle the "varying" size.

Question 2: Is there a way to move this to dynamic storage?  I'd like to see the difference between the 2 definitions in non-static storage.


Brad
Advanced Member
Advanced Member
Posts:458
Avatar

--
26 Nov 2012 09:40 AM
I don't believe I have the technical answer to your questions but I do believe you are confusing a varying length field internally defined with the static storage of a varying fields in an external file.
Lynne Noll
Senior Member
Senior Member
Posts:6567

--
26 Nov 2012 12:00 PM
With the RPG program model, all space needed for the data storage is allocated when the program or service program is started up. Varying takes a little more space than plain character because it needs to allocate the maximum size plus 2 bytes (or 4, depending on size.) Doesn't matter whether the data storage is local or global, static or dynamic; you get the whole ball of wax. Data storage includes all the memory for the data base rows, the variables, and actually, the instructions. You get it back when you reclaim the activation group or end the job. Normally, the main drawback to big memory allocation is the slowness of initializing big memory areas; if you don't do that, you can be a bit spendthrift. The difference between global or static memory and local memory is that local gets initialized each time the procedure is entered. Most systems have plenty of memory, and the amount allocated is not a big deal, although I've usually set smaller limits because I started in tiny systems and it is a habit.

Dynamic memory is NOT allocated at initialization, so you can make it a based data structure, allocate the amount you need, and run with it. This kind of memory utilization resembles the memory allocation used by objects in Java. However, you don't get all Java's support for memory handling and garbage collection, and all Java's constant memory allocation slows it down over old style C and RPG. So avoid dynamic if there isn't a good reason for it. (I've used dynamic when I really should not have, but I don't always go for the most efficient approach; sometimes, I'm fooling around.)
davesmith
Veteran Member
Veteran Member
Posts:1241
Avatar

--
26 Nov 2012 12:47 PM
Question 1: Why is the total size allocated in static memory, despite the fact that the field is defined as varying? This implementation of static size is not varying, it's fixed, with the added disadvantage that 2 more bytes are added for each element, ostensibly to handle the "varying" size.

The "varying" datatype in RPG doesnt imply anything about the type of memory used to implement it, just that it has an additional attribute to record the current length. Arrays in RPG by definition occupy contiguous storage, and so it makes no sense to perhaps release storage beyond the current length of each array element (which is what your question implies).


Question 2: Is there a way to move this to dynamic storage? I'd like to see the difference between the 2 definitions in non-static storage.

My question would be why would you want to see that? In most... cases it will make no appreciable difference to the performance of your code. In some cases it could even make it worse.


Dave
Lynne Noll
Senior Member
Senior Member
Posts:6567

--
27 Nov 2012 12:24 PM
Tzvi Kahn, what are you trying to do? If you are working with messages, a message file might be what you really want. There are nice provisions for multiple language support, it handles variable data, and you can merge in your own data. It is a different type of storage from a data base file.


You are not authorized to post a reply.

Acceptable Use Policy