OCI Program in Non-Blocking mode Leaks Memory with Every Call to OCIStmtExecute. (Doc ID 1290023.1)

Last updated on JULY 05, 2017

Applies to:

Oracle Database - Enterprise Edition - Version 10.2.0.4 to 11.2.0.2 [Release 10.2 to 11.2]
Information in this document applies to any platform.
***Checked for relevance on 22-Feb-2013***

Symptoms

OCI program using non-blocking mode leaks memory with every call to OCIStmtExecute starting with 10.2.0.4

1) C Code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <oci.h>

static OCIEnv *p_env;
static OCIError *p_err;
static OCISvcCtx *p_svc;
static OCIServer *p_srv;
static OCIStmt *p_sql;
static OCISession *p_ses;

void chkerr(int rc) {
int errcode;
char errbuf[100];

if (rc != 0) {
OCIErrorGet((dvoid *)p_err, (ub4) 1, (text *) NULL, &errcode, errbuf, (ub4) sizeof(errbuf), OCI_HTYPE_ERROR);
printf("Error - %.*s\n", 512, errbuf);
exit(8);
}
}
int main()
{
int rc;
int i;
char sql[1023] = "select 1,2,3,4,5,'aaaaaaaaaaaaaaaaaa','bbbbbbbbbbbbbbbbb', sysdate from dual";

rc = OCIEnvCreate(&p_env, OCI_DEFAULT, 0, NULL, NULL, NULL, 0,0);
if (rc!=0)
{
printf("Error creating environment.\n");
exit(1);
}

/* Initialize handles */
rc = OCIHandleAlloc( (dvoid *) p_env, (dvoid **) &p_err, OCI_HTYPE_ERROR,
(size_t) 0, (dvoid **) 0);
rc = OCIHandleAlloc( (dvoid *) p_env, (dvoid **) &p_ses, OCI_HTYPE_SESSION,
(size_t) 0, (dvoid **) 0);

rc = OCIAttrSet((dvoid *) p_ses, (ub4) OCI_HTYPE_SESSION, (dvoid *) "scott", 5, (ub4) OCI_ATTR_USERNAME, p_err);
chkerr(rc);

rc = OCIAttrSet((dvoid *) p_ses, (ub4) OCI_HTYPE_SESSION, (dvoid *) "tiger", 5, (ub4) OCI_ATTR_PASSWORD, p_err);
chkerr(rc);

rc = OCIHandleAlloc( (dvoid *) p_env, (dvoid **) &p_srv, OCI_HTYPE_SERVER, (size_t) 0, (dvoid **) 0);
chkerr(rc);

rc = OCIHandleAlloc( (dvoid *) p_env, (dvoid **) &p_svc, OCI_HTYPE_SVCCTX, (size_t) 0, (dvoid **) 0);
chkerr(rc);

rc = OCIServerAttach(p_srv, p_err, (text *) "REF1122W_CELCSOL1.US.ORACLE.COM", (sb4) 31, (ub4) OCI_DEFAULT);
chkerr(rc);

rc = OCIAttrSet( p_svc, (ub4) OCI_HTYPE_SVCCTX, (dvoid *) p_srv, (ub4) 0, (ub4) OCI_ATTR_SERVER, p_err);
chkerr(rc);

rc = OCISessionBegin((dvoid *) p_svc, p_err, p_ses, (ub4) OCI_CRED_RDBMS, OCI_DEFAULT);
chkerr(rc);

rc = OCIAttrSet (p_svc, (ub4) OCI_HTYPE_SVCCTX, (dvoid *) p_ses, (ub4) 0, (ub4) OCI_ATTR_SESSION, p_err);
chkerr(rc);

// Non Blocking mode
rc = OCIAttrSet((dvoid *) p_srv, (ub4) OCI_HTYPE_SERVER, (dvoid *) 0, (ub4) 0, (ub4) OCI_ATTR_NONBLOCKING_MODE, p_err);
chkerr(rc);

/* Allocate and prepare SQL statement */
rc = OCIHandleAlloc( (dvoid *) p_env, (dvoid **) &p_sql, OCI_HTYPE_STMT, (size_t) 0, (dvoid **) 0);
chkerr(rc);
rc = OCIStmtPrepare(p_sql, p_err, sql, (ub4) strlen(sql), (ub4) OCI_NTV_SYNTAX, (ub4) OCI_DEFAULT);
chkerr(rc);

printf(" Entering loop value \n ");
for (i=0; i<250000; i++)
{
/* Execute the SQL statment */
while ((rc = OCIStmtExecute(p_svc, p_sql, p_err, (ub4) 0, (ub4) 0,
(CONST OCISnapshot *) NULL, (OCISnapshot *) NULL, OCI_DEFAULT)) == OCI_STILL_EXECUTING )
{
//usleep(1);
}
chkerr(rc);

rc = OCIStmtFetch (p_sql, p_err, (ub4) 0, (ub2) OCI_FETCH_NEXT, (ub4) OCI_DEFAULT);
chkerr(rc);
if (i%50000 == 0)
printf(" loop value %d \n ", i);
}

rc = OCIHandleFree((dvoid *) p_sql, OCI_HTYPE_STMT); /* Free handles */
chkerr(rc);
rc = OCIHandleFree((dvoid *) p_svc, OCI_HTYPE_SVCCTX);
chkerr(rc);
rc = OCIHandleFree((dvoid *) p_err, OCI_HTYPE_ERROR);
chkerr(rc);

return;
}


2) Build the executable

make -f demo_rdbms.mk build EXE=kmem264 OBJS=kmem.o


3) Reproduced the memory leak with 11.2.0.1/11.2.0.2, 11.1.0.7 and 10.2.0.5 databases

To monitor memory leak, on solaris used top command and on linux used 'ps axo user,pid,vsz,thcount,cmd'

With 11.2.0.2
[celcsol1]/testcases/ocmem> top |grep 27803
27803 refresh 1 0 0 157M 66M cpu/2 0:25 12.46% kmem264
...........
...
[celcsol1]/testcases/ocmem> top |grep 27803
27803 refresh 1 0 0 174M 83M cpu/18 8:04 12.27% kmem264

With 10.2.0.5
[celcsol1]/testcases/ocmem> top |grep 24937
24937 refresh 1 31 0 26M 21M sleep 0:07 4.01% kmem564
.........
.....
[celcsol1]/testcases/ocmem> top |grep 24937
24937 refresh 1 31 0 32M 27M cpu/17 1:01 3.76% kmem564

On Linux
[celclnx13]/grdbms/etc> ps axo user,pid,vsz,thcount,cmd | grep lkmem32
grdbms 1799 38904 1 lkmem32
......
[celclnx13]/grdbms/etc> ps axo user,pid,vsz,thcount,cmd | grep lkmem32
grdbms 1799 45640 1 lkmem32

Cause

Sign In with your My Oracle Support account

Don't have a My Oracle Support account? Click to get started

My Oracle Support provides customers with access to over a
Million Knowledge Articles and hundreds of Community platforms