Memory Allocation with COBOL
Generally, the use of a table/array (Static Memory) is most common in COBOL modules in an application system. When a space abend (SOC4) occurs, due to the exceeding of a dimension of an array, the developer must have to find each & every place in the legacy system where that table is declared and used, and then to increase the dimension by a sufficient amount. But still what should be the sufficient amount is often unclear. Again all the related modules need to be recompiled & tested. The bottom line is the exceeding of a dimension of a table can be a very costly maintenance item.
Some common questions come to the programmer’s mind while using a table/array in a COBOL Module, as below.
- Is it ever accurately possible to determine a maximum table size that does not waste memory?
- Will it suffice enough to hold large additional data items during the lifetime of a legacy system?
- Doesn’t the exceeding of array dimension crop up inevitably and repeatedly over time? And isn’t it a pain to repair?
Here comes the concept of Dynamic Memory Allocation to handle the Static allocation in a more efficient and cost effective way.
Dynamic Memory Allocation
A dynamically allocated area uses just the amount of memory necessary to hold all data items during runtime of the program. A dynamically allocated area is thus efficient with regard to memory usage and OS paging. Dynamic memory allocation is when an executing program requests that the operating system give it a block of main memory. Usually the purpose is to add a node to a data structure.
Advantage of Dynamic Memory Allocation
Consider a COBOL table/array is defined to occur 1000 items in order to hold an expected number data item and considering the future growth of the table. Then the difference between the actual number of items (n* size of table item) and the maximum dimension of the table (1000 * size of table item) is wasted memory. Such wasted memory has implications with regard to paging by the operating system and can degrade overall system performance. The bigger the declared area for the table, the more pages/memory needed to hold the table.
Take an example of two subroutines statically utilizing 1000 item tables. In the case of Static Memory allocation the subroutine will require 2*1000* the size of a table element. But in case of dynamic memory allocation, the memory will be allocated to the first table and then the memory will be freed which can be used by the 2nd table later. The maximum memory used at any point of time by the program in case of Dynamic Allocation will be 1000 * the size of a table element. Hence, Dynamic allocated tables will use only half as much memory as statically allocated one.
The dynamically allocation memory uses just the amount of memory necessary to hold all data items. A dynamically allocated area is thus efficient with regard to memory usage and operating system paging.
Some commonly used Dynamic Allocation concepts and techniques in COBOL are
- Pointer
- Heap
POINTER
A pointer variable is a reference or a table variable that stores a memory address. This address can be arbitrarily changed, enabling the contents of any accessible memory location to be addressed and manipulated directly. For example, when a program requests additional memory during runtime from the OS, the address of the starting location to the new, dynamically allocated variable is returned. Since the value stored in a preexisting compiler-allocated pointer variable can be altered, the running program can preserve the address passed back by the OS.
However, if the pointer variable was defined within an inner block, it will be destroyed when the block terminates. The location of the dynamically allocated memory is then lost and the memory cannot subsequently be accessed or later freed.
Consider going to a Library to find a book about a specific subject matter. Most likely, you will be able to use some kind of electronic reference or a catalog, to determine the title and author of the book you want. Since the books are typically shelved by category, and within each category sorted by author’s name, it is a fairly straightforward and painless process to then physically select your book from the shelves. Now, suppose instead you came to the library in search of a particular book, but instead of organized shelves, were stored with large bags lining both sides of the room, each arbitrarily filled with books that may or may not have anything to do with one another. It would take hours, or even days, to find the book you needed, a comparative eternity. This is how software runs when data is not stored in an efficient format appropriate to the application.
When setting up data structures Linked List, Queues and Trees, it is necessary to have pointers to help manage how the structure is implemented and controlled. Typical examples of pointers are start pointers, end pointers, and stack pointers.
These pointers can either be
1. Absolute Pointer: - The actual physical address or a virtual address in virtual memory.
2. Relative Pointer :- An offset from an absolute start address (Base Address)
The COBOL programming language supports pointers to variables. Primitive or group (record) data objects declared within the LINKAGE SECTION of a program are inherently pointer-based, where the only memory allocated within the program is space for the address of the data item (typically a single memory word). In program source code, these data items are used just like any other WORKING-STORAGE variable, but their contents are implicitly accessed through their LINKAGE pointers. Memory space for each pointed-to data object is typically allocated dynamically using external CALL statements or via embedded extended language constructs such as EXEC CICS or EXEC SQL statements. Extended versions of COBOL also provide pointer variables declared with USAGE IS POINTER clauses. The values of such pointer variables are established and modified using SET and SET ADDRESS statements. Some extended versions of COBOL also provide PROCEDURE-POINTER variables, which are capable of storing the addresses of executable code.
Defining a COBOL Pointer
A pointer is a 4-byte elementary item that can be compared for equality, or used to set the value of other pointer items and can be defined in two ways.
- With the USAGE POINTER clause. The resulting data item is called a pointer data item.
- With the USAGE PROCEDURE-POINTER clause. The resulting data item is called a procedure-pointer data item.
A pointer or procedure-pointer data item can be used only in:
- A SET statement
- A relation condition (only Equal To)
- The USING phrase of a CALL statement, or the Procedure Division header
- The operand for the LENGTH OF and ADDRESS OF special registers.
Pointer data items are defined explicitly with the USAGE IS POINTER clause, and are implicit when using an ADDRESS OF special register or the ADDRESS OF an item.
If a group item is described with the USAGE IS POINTER clause, the elementary items within the group item are pointer data items. The group itself is not a pointer data item, and cannot be used in the syntax where a pointer data item is allowed. The default value of a COBOL pointer is NULL.
In the above example, the PTRA is a 77-level data item. So the ADDRESS OF PTRA is the ADDRESS OF special register. Because a special register is an actual storage area, the SET statement moves the contents of ADDRESS OF AVAR into pointer data item APTR.
Pointers can be defined at any level (except 88) in the FILE, WORKING-STORAGE, or LINKAGE SECTIONS of a program. When a pointer is referenced it must be on a 16-byte storage boundary. Pointer alignment refers to the compiler's process of positioning pointer items within a group item to offsets and should be multiples of 16 bytes from the beginning of the record. If a pointer item is not on a 16-byte boundary, a pointer alignment exception is sent to the program.
HEAP Allocation in COBOL
Heap memory is a common pool of free memory used for dynamic memory allocations within an application. Dynamic memory allocation is the allocation of memory that is dynamically allocated and deallocated by the application. The term heap memory is used because most implementations for dynamic memory allocation make use of a binary tree data structure called a heap.
There are three primary operations performed on a heap.
a. Allocation. This operation reserves a block of storage of a given size and returns a pointer to the reserved block. If no free element is large enough to satisfy the request, an additional heap segment is allocated via GETMAIN.
b. Deallocation. This operation returns a given block of storage (previously allocated by the application) to the heap. When a block of storage is deallocated, ownership of the block is returned to the heap.
c. Reallocation. This operation resizes a given block of storage to a new size and returns a (possibly updated) pointer to the block of storage.
The HEAP is an area of storage that is allocated to contain data that will remain available until the MAIN program terminates non-LIFO storage. For COBOL programs compiled with RENT (Re-entrant) WORKING-STORAGE is in the HEAP, however, if the program in not Re-entrant (NORENT) then the working-storage is in the load module.
The COBOL provides two common functions to allocate and free storage.
· The subroutine CEEGTST allocates storage in the user heap and returns the address of that storage.
· The subroutine CEEFRST releases storage back to the user heap.
Storage that is not released during program execution will be released when the main program terminates.
Heap Vs Stack – The Difference
The stack is the memory set aside as scratch space for a thread of execution. The stack is always reserved in a LIFO (Last in First Out) order; the most recently reserved block is always the next block to be freed. This makes it really simple to keep track of the stack; freeing a block from the stack is nothing more than adjusting one pointer.
The heap is memory set aside for dynamic allocation. Unlike the stack, there's no enforced pattern to the allocation and deallocation of blocks from the heap; you can allocate a block at any time and free it at any time. This makes it much more complex to keep track of which parts of the heap are allocated or free at any given time.
Each thread gets a stack, while there's typically only one heap for the application (although it isn't uncommon to have multiple heaps for different types of allocation).
Memory Allocation with COBOL的更多相关文章
- Linux下TomcatVM参数修改:Native memory allocation (mmap) failed to map 3221225472 bytes for committing reserved memory.
不可行的方法最初我直接修改catalina.sh, 将JAVA_OPTS变量加上了 -server -Xms1G -Xmx1G -XX:+UserG1GC最初看起来没啥问题,但是当服务器运行几天后,发 ...
- Memory Allocation in the MySQL Server
https://dev.mysql.com/doc/internals/en/memory-allocation-mysql-server.html MySQL Internals Manual / ...
- 内存管理(memory allocation内存分配)
Memory management is the act of managing computer memory. The essential requirement of memory manage ...
- .NET Memory Allocation Profiling with Visual Studio 2012
.NET Memory Allocation Profiling with Visual Studio 2012 This post was written by Stephen Toub, a fr ...
- Memory Allocation Error
Memory allocation error happened when I tried to install MySQL 5.7.13 in my server, which has 2G mem ...
- [C++] 2D Array's memory allocation
2D Array's memory allocation
- Advanced Memory Allocation 内存分配进阶[转]
May 01, 2003 By Gianluca Insolvibile in Embedded Software Call some useful fuctions of the GNU C l ...
- 动态内存分配(Dynamic memory allocation)
下面的代码片段的输出是什么?为什么? 解析:这是一道动态内存分配(Dynamic memory allocation)题. 尽管不像非嵌入式计算那么常见,嵌入式系统还是有从堆(heap)中动态分 ...
- C++ TUTORIAL - MEMORY ALLOCATION - 2016
http://www.bogotobogo.com/cplusplus/memoryallocation.php Variables and Memory Variables represent st ...
随机推荐
- 天气预报service
https://weather.com/ https://api.weather.com/v2/turbo/vt1dailyForecast?apiKey=c1ea9f47f6a88b9acb43ab ...
- 基于 Docker 的 Zabbix 微服务系统
zabbix 官网提供一个镜像 [ zabbix-appliance ], 可以直接拉起一个 zabbix-server. 但是数据库无法分离出来. 本实践使用 zabbix 官方提供的 Docker ...
- Morley's Theorem (计算几何基础+向量点积、叉积、旋转、夹角等+两直线的交点)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- bzoj 2121 DP
首先如果我们能处理出来i,j段能不能消掉,这样就可以直接dp转移了,设w[i]为前i为最少剩下多少,那么w[i]=w[j-1] (flag[j][i]). 现在我们来求flag[i][j],首先我们可 ...
- web-project 故障查看功能 检测是否启动fmd服务
def check_fmd_service(): try: output = subprocess.check_output('svcs -H -o state fmd',shell=True) st ...
- Angular2.0 基础: User Input
1.Angular 2.0 中的变量 对输入值的获取,我们可以通过$event 来获取,也可以通过变量来获取. template: ` <input (keyup)="onKey($e ...
- CPU架构及并发编程基础(一)
一.intel cpu发展计划tick-tock Tick-Tock是Intel发展微处理器芯片设计制造业务的一种战略模式.Intel指出,每一次处理器微架构的更新和每一次芯片制程的更新遵循“Tick ...
- 集合框架源码学习之LinkedList
0-1. 简介 0-2. 内部结构分析 0-3. LinkedList源码分析 0-3-1. 构造方法 0-3-2. 添加add方法 0-3-3. 根据位置取数据的方法 0-3-4. 根据对象得到索引 ...
- JSOI2018简要题解
来自FallDream的博客,未经允许,请勿转载,谢谢. 有幸拜读到贵省的题目,题的质量还不错,而且相比zjoi可做多了,简单发一下题解吧. 还有就是,怎么markdown在博客园上的代码这么丑啊 「 ...
- 【EverydaySport】健身笔记——静态牵拉
静态牵拉一般在运动后进行,可以有效的提高肌肉的柔韧性和关节的灵活性,预防和缓解疼痛. 每个动作达到自己活动范围的最大,有牵拉感即说明有效,静态保持至少30秒,切勿震荡,进行2组. 1 大腿前群牵拉 2 ...