I wrote a number crunching algorithm. The idea is that:

1. A small main programs needs very few memory (starts at 2 MB)
2. Then, in a loop, it calls a function that needs quite some memory (around 100 MB) which should be released when the function end. In order to understand what's going on, the function is now always called with the same parameters.

It seems that the program slowly eats memory so I suspect a memory leak. I have tried Address Sanitizer from Clang and Pointer Checker from Intel but they don't find anything.

Now, I am looking at the memory consumption in my Activity Monitor (I am running OSX, but I get the same memory usage from the Unix command "top") and just before the big function is called, the program takes 2 MB. When running the function, the program takes 120 MB. What is strange is that when the program ends up the big function and comes back inside the loop, it now takes 37 MB! Then, when it goes back into the big function, it takes 130 MB. Again, coming back in the loop, it takes 36 MB, then in the big function it takes 140 MB...

So it is slowly drifting away, but not with a regular pattern. How should I trust the memory usage in "top"?

Can memory fragmentation increase the memory usage without memory leak?

---

I let the program run overnight, and here is the data I get:

1. In the first loop, the program takes 150 MB
2. 2 hours later, after 68 loops, the program takes 220 MB
3. After one night and 394 loops, the program takes 480 MB

So it seems that the function that allocates and deallocates memory (about 120 MB) seems to "leak" 1 MB each time it is called.