Thursday, March 29, 2012

CRecordset consumes SQL Server Memory

I have an MFC app with a thread that uses a ODBC consumer class to access a
SQL server. When I write the code different ways I get different results an
d
don't know why.
Example 1:
threadFunc()
{
CtblMyTable myRecordset(NULL);
while( !kill )
{
myRecordset.Open();
myRecordset.Close();
}
}
Example 2:
threadFunc()
{
while( !kill )
{
CtblMyTable myRecordset(NULL);
myRecordset.Open();
myRecordset.Close();
}
}
Example 3:
threadFunc()
{
while( !kill )
{
CDatabase db;
CtblMyTable myRecordset(&db);
myRecordset.Open();
myRecordset.Close();
db.Close();
}
}
Example 1 uses up no memory in the SQL server. Example 2 consumes memory in
the SQL server at a fast rate and eventually my "stolen pages" start to
increment. Example 3 consumes memory at a much slower rate, like less than
a
tenth of the rate.
Can anyone explain why the location of the declaration or the declaration of
a CDatabase object affects memory usage in my SQL Server? Is there a way
that I can get the memory to clean up in SQL Server? I eventually crash
every few ws because I run out of memory in SQL Server and I think it has
something to do with the fact that most of my code is not like example 1.
ThanksHi
Passing NULL to your recordset constructor to have a CDatabase object
constructed and connected for you automatically each time it is called. This
is probably not getting cleaned up until your procedure exits and the
Recordset is distroyed. Having a single CDatabase object for your
application will be more efficient.
John
"Jason Wood" <jason.wood@.woodtc.com.nospam> wrote in message
news:7918D6CB-4E57-4722-BB9A-64BBD9F6C6DE@.microsoft.com...
>I have an MFC app with a thread that uses a ODBC consumer class to access a
> SQL server. When I write the code different ways I get different results
> and
> don't know why.
> Example 1:
> threadFunc()
> {
> CtblMyTable myRecordset(NULL);
> while( !kill )
> {
> myRecordset.Open();
> myRecordset.Close();
> }
> }
>
> Example 2:
> threadFunc()
> {
> while( !kill )
> {
> CtblMyTable myRecordset(NULL);
> myRecordset.Open();
> myRecordset.Close();
> }
> }
>
> Example 3:
> threadFunc()
> {
> while( !kill )
> {
> CDatabase db;
> CtblMyTable myRecordset(&db);
> myRecordset.Open();
> myRecordset.Close();
> db.Close();
> }
> }
>
> Example 1 uses up no memory in the SQL server. Example 2 consumes memory
> in
> the SQL server at a fast rate and eventually my "stolen pages" start to
> increment. Example 3 consumes memory at a much slower rate, like less
> than a
> tenth of the rate.
> Can anyone explain why the location of the declaration or the declaration
> of
> a CDatabase object affects memory usage in my SQL Server? Is there a way
> that I can get the memory to clean up in SQL Server? I eventually crash
> every few ws because I run out of memory in SQL Server and I think it
> has
> something to do with the fact that most of my code is not like example 1.
> Thanks

No comments:

Post a Comment