ITEM: M0775L

malloc, calloc, PSALLOC & memory usage


 Env: 3.2.5, xlc 1.3.0.12

 Desc: Know that APAR IX37139 and IX38211 clear up malloc problems.
 Would like to know if it clears up calloc problems. What we are
 experiencing is if the program tries to allocate more memory
 with calloc than available we get a killed (sig 9) on our program. 

*** Documentation of calloc: 
The calloc subroutine allocates space for an array with the number of 
elements specified by the NumberOfElements parameter. Each element is
of the size specified by the ElementSize parameter. The space is 
initialized to zeros.

The malloc, realloc, and calloc subroutines return a null pointer if
there is no available memory, or if the memory arena has been corrupted
by being stored outside the bounds of a block. When this happens, the 
block pointed to by the Pointer parameter may be destroyed.

Test Case:

\#include \
\#include \

main(c,v)
int c;
char *v[];
{        void * (a);
        int val = atoi(v[1]);
        a = calloc(val * 1000000, sizeof(int));
        printf("%08X\\n", a);
}To see the problem, call the program as follows:

        a.out 1
        a.out 10
        a.out 50
        a.out 100
        a.out 200
        a.out 1000

Eventually, the program will be killed when performing the calloc()
routine (the kernel sends a signal 9).

Action: 

Had verified that paging space is available and that your limits for
the user with uname -a were set at unlimited(-1).

This may have to do with the environment variable PSALLOC. 
Were able to produce the problem with a system that the PSALLOC=early
variable was not set. 

Setting the variable PSALLOC=early fixed the problem with the killed
process on your system. 

After reviewing key points of memory allocation and the PSALLOC
environment variable believe this is working as designed. From
InfoExplorer the below description supports memory allocation usage:

                Understanding Paging Space Allocation Policies
   AIX uses two modes for paging space allocation. The setting of the
PSALLOC environment variable determines the paging space allocation
mode. The default mechanism is the late paging space allocation
algorithm.  The user can switch to an early paging space allocation
mode by setting the value of the  PSALLOC environment variable to
early.
                Paging Space Considerations
   The amount of paging space required depends on the type of
activities performed on the system. If paging space runs low,
processes may be lost, and if paging space runs out, the system may
panic. When a paging-space low condition is detected, additional
paging space should be defined.
   The system monitors the number of free paging space blocks and
detects when a paging-space shortage exists. When the number of free
paging-space blocks falls below a threshold known as the paging-space
warning level, the system informs all processes (except kprocs) of
this condition by sending the SIGDANGER signal. If the shortage
continues and falls below a second threshold known as the paging-space
kill level, the system sends the SIGKILL signal to processes that are
the major users of paging space and that do not have a signal handler
for the  SIGDANGER signal (the default action for the SIGDANGER signal
is to ignore the signal). The system continues sending SIGKILL signals
until the number of free paging-space blocks is above the paging-space
kill level.  Processes that dynamically allocate memory can ensure
that sufficient paging space exists by monitoring the paging-space
levels with the psdanger subroutine or by using special allocation
routines (see the psmalloc.c file for sample code that uses memory
allocation routines which allocate paging space at memory allocation
time). Processes can keep from getting ended when the paging-space
kill level is reached by defining a signal handler for the SIGDANGER
signal and by releasing memory and paging-space resources allocated in
their data and stack areas and in shared memory segments, using the
disclaim subroutine.  
                Comparing Late and Early Paging Space Allocation
     AIX uses the PSALLOC environment variable to determine the mechanism
used for memory and paging space allocation. If the PSALLOC
environment variable is not set, is set to null, or is set to any
value other than early, the system uses the default late allocation
algorithm.  
     The default late allocation algorithm for memory and
paging space allocation assists in the efficient use of disk resources
and supports applications of customers who wish to take advantage of a
sparse allocation algorithm for resource management. The late
allocation algorithm does not reserve paging space when a memory
request is made; it approves the request and assigns paging space when
pages are touched. Some programs allocate large amounts of virtual
memory and then use only a fraction of the memory. Examples of such
programs are technical applications that use sparse vectors or
matrices as data structures. The late allocation algorithm is also
more efficient for a real-time, demand-paged kernel such as the one in
AIX.  
        It is possible to overcommit resources when using the late
allocation algorithm for paging space allocation. In this case, when
one process gets the resource before another, a failure results. AIX
attempts to avoid complete system failure by killing processes
affected by the resource overcommitment. The SIGDANGER signal is sent
to notify processes that the amount of free paging space is low. If
the paging space situation reaches an even more critical state,
selected processes that do not catch the SIGDANGER signal are sent a
SIGKILL signal.  
        The user can use the PSALLOC environment variable to
switch to an early allocation algorithm for memory and paging space
allocation. The early allocation mechanism allocates paging space for
the executing process at the time the memory is requested. If there is
insufficient paging space available at the time of the request, the
early allocation mechanism fails the memory request.  
        If the PSALLOC environment variable is set to early, then every 
program started in that environment from that point on, but not including 
currently running processes, will run in the early allocation environment. 
In the early allocation environment, interfaces such as the malloc
subroutine and the brk subroutine will fail if sufficient paging space
cannot be reserved when the request is made.  
        Processes executed in the early allocation environment mode will 
not be sent the SIGKILL signal should a low paging space condition occur.  
The following memory allocation interface subroutines are affected by a 
switch to an early allocation environment:  
ú       malloc 
ú       free
ú       calloc 
ú       realloc 
ú       brk 
ú       sbrk 
ú       shmget
ú       shmctl


Support Line: malloc, calloc, PSALLOC & memory usage ITEM: M0775L
Dated: October 1994 Category: N/A
This HTML file was generated 99/06/24~13:30:40
Comments or suggestions? Contact us