Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursion leading to out of memory

I have a RAM of 2 GB. We have a application which performs Export/Import operations. We have a recursive function which has one local variable of type Set which keeps on getting populated every iteration. This Set keeps growing and at one point we run out of memory.

Is there any alternative data structure which can optimally use the memory ?

Here's the rough code

GetObjectsForExportImpl(long lExportOptions, __int64 numIdProject, XExportSets
     &exportSets, long lClientId, CComPtr<IEPIPDServer> ptrIPDServer,FILE *fp)
{
    XExportSets exportLocal;   //Thats a structure containing the Set
    QueryObjectsForExport(lExportOptions, numIdProject, exportLocal,
         lClientId, ptrIPDServer);
    SetIDs::iterator it = exportLocal.setShared.begin();

    for (; it != exportLocal.setShared.end(); ++it)
    {
         //recursive call
         pExportObject->GetObjectsForExportImpl(lExportOptions,
             numIdProject, exportSets, lClientId, ptrIPDServer,fp);
    }
}
like image 671
sameer karjatkar Avatar asked Dec 30 '22 03:12

sameer karjatkar


1 Answers

An alternative structure wouldn't help much. Say you switched to a class that uses half the memory. Still you're only delaying the doom.

A structure size of 2GB usually indicates you need to switch to a disk based structure, a database or a memory mapped hashtable.

like image 156
Sedat Kapanoglu Avatar answered Jan 09 '23 11:01

Sedat Kapanoglu