CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2005
    Posts
    221

    Question DAO, Threads, and Memory Usage

    I'm encountering a memory problem in a threaded application that uses DAO.

    I do know that the DAO technology is deprecated and that it is not thread-safe. I am making sure that only one thread accesses the database at a time using the EnterCriticalSection / LeaveCriticalSection approach, by surrounding the call to the database-handling method, NOT by surrounding the invocations of the threads. That is, the threads may be processing asynchronously by the access to the database is assured to be "one-at-a time".

    At the beiginning of the processing of each thread I call AfxDaoInit(). The program runs just fine, doing all the threading and database processing perfectly.

    BUT ! I also keep an eye on the memory usage of the program. I have the
    Windows Task Manager window open and watch the Performance tab which shows memory usage. As my program handles all the therads, and invokes that AfxDaoInit time and time again, the memory usage simply explodes, eventually consuming all available memory. If I then stop the portion of the program that's doing the threads, i.e. I don't call any more threads and all previously active threads have finished their work, then slowly, VERY SLOWLY, while my program is not doing anything else, the memory gets reclaimed. I can watch the memory usage on the Windows Task Manager slowly decrease the PF usage and slowly increase the Available Physical Memory.

    So, first question: Is there any way to force the application to reclaim
    memory while it's running, ideally at the end of each thread's processing?


    I tried some things...

    I tried calling AfxDaoTerm at the end of each thread. That caused exceptions to be thrown, because the threads are asynchronous and there's no guarantee that there's matching invications of AfxDaoInit and AfxDaoTerm.

    I tried setting up an invocation counter, like:

    Code:
    static int nDaoInitializationCount = 0;
    
    
    BOOL CHostTransactionHandlerThread::InitInstance()
    {
    	if (0 == nDaoInitializationCount)
    	{
    		AfxDaoInit();
    		++nDaoInitializationCount;
    	}
    
    
    	do the database processing
    
    
    	--nDaoInitializationCount;
    	if (0 == nDaoInitializationCount)
    	{
    		AfxDaoTerm();
    	}
    
    	return(FALSE);
    }

    so that AfxDaoTerm would only get called when no thread needed it.
    That still resulted in exceptions, the problem being that each and every
    thread absolutely MUST cann AfxDaoInit.


    So, I'm at a loss as to how to proceed.

    It NEEDS AfxDaoInit at the BEGINNING of each thread.
    It HATES AfxDaoTerm at the END of each thread.
    It consumes enormous amounts of memory while running.
    But Windows seems to reclaim the memory if I just let the program run
    after the threading process has completed.

    I'm open to suggestions (other than "Don't use DAO", since I have to for this app.).


    - Roger

  2. #2
    Join Date
    Apr 2001
    Location
    Tampa Florida
    Posts
    233

    Re: DAO, Threads, and Memory Usage

    Hello
    I am hoping you get a solid answer for this also.
    DAO relies heavily on cache techniques and the memory may slowly
    drain back in more so from the release of the cache which would hold
    data for a much greater period due to wide association than that released
    by the thread when finished.
    Great question and I hope to hear something too.
    Regards
    "trampling out the vintage"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured