Choose the memory duration

Because a C UDR executes in the memory space of the database server, its dynamic memory allocations can increase the memory usage of the database server. For this reason, it is important that a UDR release its dynamically allocated memory as soon as it no longer needs to access this memory.

To help ensure that unneeded memory is freed, the database server associates a memory duration with memory allocation made from its shared memory. The portion of shared memory that the database server provides for dynamic allocation by C UDRs is organized into several memory pools. Each memory pool is associated with a memory duration, which specifies the lifetime of the memory allocated from the pool. Keeping related memory allocations in one pool helps to reduce memory fragmentation.

The following figure shows a schematic representation of the shared memory of the database server, including the memory-duration memory pools.
Figure 1. Memory-duration memory pools in database server shared memory
begin figure description - This figure is described in the surrounding text. - end figure description
Tip: For more information about the use and structure of database server memory pools, see your Informix® Administrator's Guide. For more information about how to monitor the amount of shared memory that exists in each of the memory pools, see Monitor shared memory.

When the database server calls a UDR, it creates a memory context. This memory context records all of the allocations that the UDR makes before the routine returns. The UDR might run for some time, calling other UDRs or DataBlade® API functions. The database server automatically reclaims shared memory based on its memory duration. When a particular memory duration expires, the database server marks the associated memory pool for deallocation.

The DataBlade® API provides the following regular and advanced groups of memory durations for dynamically allocated memory in C UDRs:
  • Use the following public memory durations in all UDRs.
    Available memory durations Memory-duration constant
    Current® memory duration PER_ROUTINE (by default)
    For the duration of one iteration of the UDR PER_ROUTINE, PER_FUNCTION
    For the duration of the current SQL command PER_COMMAND
    For the duration of the current SQL statement PER_STATEMENT (Deprecated)
    For the duration of the execution of the current SQL statement PER_STMT_EXEC
    For the duration of the current prepared SQL statement PER_STMT_PREP

    Most memory allocations can be allocated with a regular memory duration.

  • Use the following advanced memory durations only in specialized cases.
    Available memory durations Memory-duration constant
    For the duration of the current transaction PER_TRANSACTION
    For the duration of the current session PER_SESSION
    For the duration of the database server execution PER_SYSTEM

    These memory durations are quite long and therefore increase the chance of memory leaks.

Important: The advanced memory durations can adversely affect your UDR if you use them incorrectly. Use them only when no regular DataBlade® API memory duration can perform the task you need.