PDA

View Full Version : Memory Leaks



luca88
13th May 2014, 08:37
Hello.
I'm trying to solve some memory leaks in my application. I noticed that when I allocate a object with the new operator (for example a QGLWidget) obviously the memory used by application increased, but when I tried to delete the object only a portion has been released. To better understand I tried a simple program like the following code:

QList<QGLWidget *> aaa;
int i;

(1)
for(i=0;i<100;i++)
aaa.append(new QGLWidget());

(2)
for(i=0;i<100;i++)
delete aaa.at(i);

(3)
The application memory usage at each point is the following: (1) 4672 K (2) 413156 K (3) 111212 K

Why the deallocation isn't complete? In my original application I need to allocate and deallocate many times these objects but this situation brings a memory increase too high. I'm using Qt 4.8.4 in OpenSUSE 12.3. Can someone help me?? Thanks

ChrisW67
13th May 2014, 09:44
Memory allocated to a process is not generally freed to the operating system immediately an object is deleted. Chances are the process will request a new allocation shortly and this will be filled from free memory already allocated to the process. This is why operating system tools, like the free command on Linux, are not particularly useful for identifying small leaks. All memory allocated to the process will be freed when the process is terminated.

If you want to investigate memory leaks then use a tool built for the job, like Valgrind.

luca88
13th May 2014, 10:18
Thanks for your reply ChrisW67.
The code above it's only an example. In my application I have to allocate/deallocate a QGLWidget several times. I know that the OS doesn't freed immediatly the memory, but in my application I noticed that the memory isn't deallocated also after many minutes. I need to reistantiate a QGLWidget when the user clicks on a particular button, this can happen 2-3 times per minute, so the OS would have all the time to freed the memory, but this doesn't happen; for each click the memory increases and after 1 hour of application usage the memory reaches 1 GB. I think to manage correctly the method where I implented the QGLWidget reinitialization, in fact, I performed an object DELETE before and a NEW after. What do I have to do in order to solve this issue??? Thanks.

anda_skoa
13th May 2014, 11:01
As ChrisW67 said, use a tool designed for finding leaks to actually locate the leak instead of guessing where the leak is.

Cheers,
_

luca88
13th May 2014, 14:23
I tried to use Valgrind but unfortunatly my application is very big and so when I've launched it with Valgrind it blocked because too heavy.