From last week I am trying to fix a strange memory consumption from my project. We have a logging system which has two thread, thread1 is used to collect the data and thread2 is used to parse data and write into the file. When the user want to write the data, he will inform thread1. thread1 will create the below Job
class and pass the reference of Data*
deque
class Job
{
public:
Job(std::deque<Data*> & Job)
{
m_dataHolder = new DataContainer();
for (auto & pData : Job)
{
m_dataHolder->Add(pData->Clone());
}
}
~Job()
{
delete m_dataHolder;
}
void clear()
{
delete m_dataHolder;
m_dataHolder = NULL;
}
void write()
{
if (NULL != m_dataHolder)
{
serialize_data();
file_write();
}
}
};
here thread1 is creating a copy of Data*. Now thread1 will pass this object to thread2 and wakeup the thread using pthread_kill(). Issue is started when we have 50k records in the queue and when we create the copy memory usage goes up to 90% and it will not reduce. thread2 sample code is given below
class FileOperationsWorker()
{
public:
void SignalEvent(uint32_t SignalNumber)
{
ExecuteeMutex.lock();
while (JobQueue.size() > 0)
{
Job* pNextJob = JobQueue.front();
JobQueue.pop_front();
ExecuteeMutex.unlock();
pNextJob->Write();
delete pNextJob;
ExecuteeMutex.lock();
}
JobQueue.clear();
ExecuteeMutex.unlock();
}
void AddJob(Job* pJob)
{
ExecuteeMutex.lock();
JobQueue.push_back(pJob);
ExecuteeMutex.unlock();
pthread_kill(ThreadId, Signal);
}
std::deque<Job*> JobQueue;
};
I modified thread2 AddJob() method as below
void AddJob(Job* pJob)
{
ExecuteeMutex.lock();
JobQueue.push_back(pJob);
pJob->clear();
ExecuteeMutex.unlock();
pthread_kill(ThreadId, Signal);
}
When I cleared the pJob->clear() memory consumption reduced and came back to normal usage. But after receiving the signal I tried the same but its not reducing the memory. Another observation is next time onward file write will not increase the memory(it will stay at 90%). My question is whether STL containers will cache memory? or linux process will cache memory? There is no other code executed after giving that signal. Once the signal is received in the thread2 and even after deleting the data memory consumption is not reduced.