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.
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.
- 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.