Programmer Guide > Tips for Efficient Programming > Running Out of Memory?
  

Running Out of Memory?
Whenever you define a variable or perform an operation, PV-WAVE asks the operating system for some memory in which to store the data or operation. (Internally, PV-WAVE calls the C function malloc to allocate the additional memory.) With each additional definition or operation, the amount of memory allocated to the PV-WAVE process grows. If you typically process large arrays of data and use the vendor-supplied default system parameters, sooner or later the following error will occur:
% Unable to allocate memory
This error message means that PV-WAVE was unable to obtain from the operating system enough memory to hold all of your data. In general, this situation arises because of the way in which all C applications interact with the operating system. That is, allocated memory that is freed (via a call to the C routine free) results in a fragmented or discontiguous pool of memory within the application.
You have two basic options to resolve this error:
*First, you can try deleting all unneeded variables, functions, procedures, and common blocks. This option may be effective in many cases. However, because of the memory fragmentation previously described, it is not always possible to free sufficient space for a large variable or routine by deleting other smaller variables or routines. PV-WAVE requires a chunk of contiguous memory large enough to hold any given array or routine. To delete common blocks, procedures, and functions, use the DELCOM, DELPROC, and DELFUNC procedures, respectively. Use the DELVAR procedure to delete variables. Alternatively, if you do not want to use DELVAR, you can simply assign the value of large array and structure variables to scalar values. Note that the DEL* routines should only be used at the WAVE> prompt at the $MAIN$ level.
For more information on the PV-WAVE kernel deletion utilities, see the DEL* routines in the PV-WAVE Reference Guide.
*If the first option does not work, you will have to exit PV-WAVE. Before exiting, use the SAVE procedure to save only the variables and routines that you need. When you restore the session with the RESTORE procedure, the saved variables and routines will be stored in memory in a less fragmented manner, which may create sufficient space for you to continue your work. If PV-WAVE still cannot allocate enough memory for your data, you can try exiting without first saving the session.
 
note
The INFO, /Memory procedure tells you how much memory you have allocated. For example, a 512-by-512 complex floating array requires 8*5122 or about 2 megabytes of memory because each complex element requires 8 bytes.
Again, with the deletion and reassignment of large variables (as well as structure definitions, compiled procedures, and functions), the memory available to PV-WAVE processes will become fragmented. Eventually, you will not be able to obtain sufficient memory for a given large variable. At this point, you can try deleting unneeded variables, procedures, functions, and structures. If that does not solve the problem, you must exit from PV-WAVE to clear out the memory.
Controlling UNIX Virtual Memory System Parameters
The size of the swapping area(s) determines how much virtual memory your process is allowed. To increase the amount of available virtual memory, you must either increase the size of the swap device (sometimes called the swap partition), or use the swapon(8) command to add additional swap areas. Increasing the size of a swap partition is a time consuming task which should be planned carefully. It requires saving the contents of the disk, reformatting the disk with the new file partition sizes, and restoring the original contents. Consult the documentation that came with your system for details. Some systems (SunOS) allow you to swap to a normal file by using the mkfile(8) command in conjunction with swapon. This is a considerably easier solution.
Minimize the Memory Used
If memory is a problem, try to tailor your programming to minimize the number of images held in variables.
Keep in mind that PV-WAVE creates temporary arrays to evaluate expressions involving arrays. For example, when evaluating the statement:
A = (B + C) * (E + F)
PV-WAVE first evaluates the expression B + C, and creates a temporary array if either B or C are arrays. In the same manner, another temporary array is created if either E or F are arrays. Finally, the result is computed, the previous contents of A are deleted and the temporary area holding the result is saved as variable A. Note that during the evaluation of this statement enough memory to hold two array’s worth of data is required in addition to normal variable storage.
It is a good idea to delete the allocation of a variable that contains an image and that appears on the left side of an assignment statement. For example, in the program:
; Loop to process an image.
FOR I = ... DO BEGIN
; Processing steps.
...
; Delete old allocation for A.
A = 0
; Compute image expression and store.
A = Image Expression
...
ENDFOR
the purpose of the statement A = 0 is to free the old memory allocation for the variable A before computing the image expression in the next statement. Because the old value of A is going to be wiped out in the next statement, it makes sense to free A’s memory allocation before executing the next statement. For more information on the effects of freeing memory by deleting or reassigning large array variables, see "Running Out of Memory?".

Version 2017.1
Copyright © 2019, Rogue Wave Software, Inc. All Rights Reserved.