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 ...
随机推荐
- UITableViewController的使用
如果整个程序界面都只是使用UITableView来搭建,一般需要如下步骤: (1)向界面上拖一个UITableView (2)设置数据源 (3)设置代理 (4)遵守代理协议 上述过程相对繁琐,为了简 ...
- 删除windows上特定目录下以*.rar后缀名的python脚本
import os,fnmatch,datetime,time def all_files(root,pattern='*',single_level=False,yield_folders=Fals ...
- Robot Framework Chrome
1. 下载对应版本的chromedriver, 好像都是windows32位的,不过没关系,可以用即可. 2. 将chromedriver放入到chrome的安装路径下,然后将chromrdriver ...
- UOJ#110. 【APIO2015】Bali Sculptures
印尼巴厘岛的公路上有许多的雕塑,我们来关注它的一条主干道. 在这条主干道上一共有 NN 座雕塑,为方便起见,我们把这些雕塑从 11 到 NN 连续地进行标号,其中第 ii 座雕塑的年龄是 YiYi 年 ...
- JS对象操作
一.String常用操作 1.截取 substr(start,length) //返回从指定位置开始的指定长度的字符串. substring(start,end) //返回两个指定的位置之间的字符串. ...
- 深入理解 JavaScript(五)
根本没有“JSON 对象”这回事! 前言 写这篇文章的目的是经常看到开发人员说:把字符串转化为 JSON 对象,把 JSON 对象转化成字符串等类似的话题,所以把之前收藏的一篇老外的文章整理翻译了一下 ...
- word 宏,脚本编程
'脚本方式新建word 再新建文档,文档中输入字符串"你好" Dim wdapp As Word.Application Dim wddoc As Word.Document Se ...
- net_device->uc_promisc
如果设备不支持单播过滤,并且要监听多个单播地址时,就要使用net_device->uc_count和net_device->uc_promisc来设置混杂模式,具体见__dev_set_r ...
- 【Android XML】Android XML 转 Java Code 系列之 style(3)
最近一个月把代码重构了一遍, 感觉舒服多了, 但总体开发进度没有变化.. 今天聊聊把style属性转换成Java代码的办法 先说结论: 引用系统style是无法完美的实现的, 我们如果有写成Java代 ...
- 获取并编译最新的Notepad++源码
获取并编译最新的Notepad++源码 http://blog.csdn.net/u012814856/article/details/68947310 Notepad++源码编译及其分析 http: ...