I have written a small server application. It stores a lot of data in strings. When stresstesting it, RSS memory grows (spotted by $top).
I have ran the program through “Instrument” – Mac OS X memory leak applicaton and it find only some minor leaks – the memory leaked was a couple of hundred bytes and the program continuously grows. When searching deeper, it seems like two functions is responsible for a large majority of the memory footprint:
std::string serialize()
{
//Build basic message
std::string result="";
//std::cout << "result: " << result << "\n";
result+=m_topic+d;
//std::cout << "result: " << result << "\n";
result+=m_message+d;
//std::cout << "result: " << result << "\n";
char buf[12];
std::cout << m_severity << "\n";
snprintf(buf, sizeof(buf), "%d", m_severity);
//std::cout << "Buffer:" << buf << "\n";
std::string temp(buf);
result+=temp+d;
//std::cout << "result: " << temp << "\n";
int messagelength=strlen(result.c_str());
snprintf(buf, sizeof(buf), "%d", messagelength);
//std::cout << "Buffer:" << buf << "\n";
std::string temp2(buf);
temp2+=d;
temp2+=result;
//std::cout << "result: " << temp2 << "\n";
return temp2;
}
and
std::string message::prettyPrint()
{
struct tm *Sys_T= NULL;
time_t Tval = 0;
Tval = time(NULL);
Sys_T = localtime(&Tval);
std::string date;
char buf[10];
sprintf(buf,"%d:%d:%d (%d/%d 2010)",Sys_T->tm_hour, Sys_T->tm_min, Sys_T->tm_sec, Sys_T->tm_mday, Sys_T->tm_mon);
date+=std::string(buf);
char sevbuf[10];
sprintf(sevbuf,"%d",m_severity);
delete Sys_T;
std::string printed= "---------------------Message--------------------- \n";
printed+= +"\n "+ date + ": [[" + getTopic() + "]]\n\n" +
+ " Message:" + m_message + "\n"
+ " Severity " + std::string(sevbuf) +" \n";
//+ " Serialized " + serialize() + "\n";
return printed;
}
As you can see this is merely stack allocated objects.
At the same time, the “Instrument” memory observer reports that the number of “active” allocated memory does not grow.
I am not so familiar with programming or these terms – my question is:
- Can my application leak memory that is not reported by memory leak searching application?
- Is RSS not reporting the “active” memory set but also the historical ones?
Even though your objects are merely stack allocated, the class implementations may allocate memory in the heap. For example,
std::stringwill do this.Allocating and deallocating memory in the heap can lead to fragmentation, which would explain the increased memory usage. See http://en.wikipedia.org/wiki/Malloc#Heap-based
Edit: Looking closer at your code, there are other problems — as the others have pointed out.