views:

94

answers:

4

I have a graphics program where I am creating and destroying the same objects over and over again. All in all, there are 140 objects. They get deleted and newed such that the number never increases 140. This is a requirement as it is a stress test, that is I cannot have a memory pool or dummy objects. Now I am fairly certain there aren't any memory leaks. I am also using a memory leak detector which is not reporting any leaks.

The problem is that the memory footprint of the program keeps increasing (albeit quite slowly, slower than the rate at which the objects are being destroyed/created). So my question then is whether an increasing memory footprint is a solid sign for memory leaks or can it sometimes be deceiving?

EDIT: I am using new/delete to create/destroy the objects

+2  A: 

It depends if you're under a CLR (or a virtual machine with garbage collector) or your still in the old mode (like C++, MFC ect...)

When you have a GC around - you can't really tell, only if you test it long enough. GC can decide not to clean your objects for now... (there is a way to force it)

In the native applications, yes, a footprint increase might mean a leak.

there are some tools (very good tools) for c++ that find these leaks (google devpartner or boundschecker)

I guess there are some tools for c# and Java as well.

Dani
I'd add also valgrind to the memory check tools.
Matteo Italia
The only free leak detector that I could find that works well is Visual Leak Detector. Unfortunately, with some combinations of various SDKs such as Ogre3d, it crashes w/o informing me of any leaks (the crash occurs in the simplest of all programs - for example initializing Ogre/Havok and exiting). There are other free, simple leak detectors but they also crash (sometimes, not all the time) especially when libraries such as Havok destroy their objects in a different manner. They also miss the leaks cause by third party libraries. I wish I could afford the better leak detectors.
Samaursa
+2  A: 

If your application's process footprint increases beyond a reasonable limit, which depends on your application and what it does, and continues to increase until eventually you (will) run out of virtual memory, you definitely have a memory leak.

Michael Goldshteyn
+3  A: 

It does seem possible that this behavior could come from a situation in which there is no leak.

Is there any chance that your heap is getting fragmented?

Say you make lots of allocations of size n. You free them all, which makes your C library insert those buffers into a free list. Some other code path then makes allocations smaller than n, so those blocks in the free list get chunked up into smaller units. Then the next iteration of the loop does another batch of allocations of size n, and the free list no longer contains contiguous memory at that size, and malloc has to ask the kernel for more memory. Eventually those "smaller-than-n" allocations get freed as would your "n-sized" ones, but if you run enough iterations where the fragmentation exists, I could see the process gradually increasing its memory footprint.

One way to avoid this might be to allocate all your objects once, and not keep allocating/freeing them. Since you're using C++ this might necessitate placement new or something similar. Since you are using Windows, I might also mention that Win32 supports having multiple heaps in a process, so if your objects come from a different heap than other allocations you may avoid this.

asveikau
That is a very good point you raised and the thought has crossed my mind. I am sure it is being fragmented to some extent. Could the trickle increase in memory be simply because of fragmentation?
Samaursa
+1  A: 

Try memory allocation tests included in CRT: http://msdn.microsoft.com/en-us/library/e5ewb1h3%28VS.80%29.aspx

They help A LOT.

But I've noticed that apps do tend to vary their memory consumption a little if you look at some factors. Windows 7 might also create extra padding in memory allocation to fix bugs: http://msdn.microsoft.com/en-us/library/dd744764%28VS.85%29.aspx

Madman
I gotta say, that is some very interesting information, thank you. I had no idea windows had such a thing as a fault tolerant heap. As for the memory leaks detection, I am dealing with quite a few libraries that have their own memory managers and they throw the leak detection off. It detects hundreds, if not thousands of leaks that I know for sure are not leaks (Havok SDK for example)- Visual Leak Detector agrees in this case but it has its own problems of crashing without any reason (I have another post regarding that)
Samaursa