Memory leaks

May 22, 2011 at 3:15pm
I've constructed a little dynamic object manager but I'm not sure if it's correctly allocating and deallocating. I've checked the code over and over and I know what I'm doing and I have no reason to think it isn't working right.

However, I made a little test program that loads a massive number of the same object and then unloads it. I then opened up Task Manager and got these results:

Test 1
Start: 540k
After loading: 10292k
After unloading: 1612k

Which is undesirable but I don't know if that's my object manager causing it or the way Windows allocates memory to my program. So I made another test which loads and unloads multiple times to see if any leaks build up:

Test 2
Start: 544k
After loading: 10292k
After unloading: 1612k
After reloading: 10308k
After unloading again: 1976k
After reloading again: 10356k
After unloading one more time: 1636k

Notice that although each time the memory seems to build up, the last time we unload the memory goes down from the last time it had unloaded. I'm getting the impression that there is far more complicated stuff going on here that's giving me the weird results, so can anyone tell me how exactly one goes about checking for memory leaks?
Last edited on May 22, 2011 at 6:29pm
May 22, 2011 at 6:29pm
Any ideas for checking for memory leaks?
May 22, 2011 at 6:40pm
You should run the code at least a few hundred times in a row and see if the memory usage increases.
It's entirely possible that it's just a peculiarity of memory management.

If your code is portable, you can run it through valgrind under Linux, which can find memory leaks reliably.
May 22, 2011 at 6:42pm
The code is entirely portable, but I don't have Linux unfortunately.

Re-running the tests yields little difference. I reckon it is just the peculiarity of memory management.
May 22, 2011 at 9:07pm
Screw it, I'm installing Linux.

I don't know how many times I've been given useful software to use that only works on Linux...

Guess I better start downloading Kubuntu then.
May 22, 2011 at 9:31pm
once you are set up on Kubuntu, do an sudo apt-get install valgrind

or download and install valgrind from http://valgrind.org
May 22, 2011 at 11:00pm
That was quick, alright then downloading valgrind.
May 22, 2011 at 11:07pm
before you do that, attempt:

sudo apt-get install valgrind

if it works, you don't even need to download it manually
May 23, 2011 at 6:40am

The method told by kfmfe04 is the best one, downloading and installing may break the version of dependencies.

Edit: for windows you can install a trial version of bounds checker.
Last edited on May 23, 2011 at 6:40am
May 23, 2011 at 9:57am
You can monitor memory usage on Windows by running up Performance Monitor and monitoring the process' private bytes.
May 23, 2011 at 4:19pm
That Performance Monitor only works in Windows 7, and it uses the same info that taskmgr uses.
May 23, 2011 at 7:44pm
That Performance Monitor only works in Windows 7
Nonsense, it's been in Windows NT from the start.

and it uses the same info that taskmgr uses.
Task Manager only uses a tiny subset of the metrics available on Windows NT. At any rate, it doesn't show what you need in this case, you need to use Performance Monitor.
May 23, 2011 at 9:13pm
How comes I only had it in 7 then?
May 23, 2011 at 10:19pm
It's been there all along. You can start it from the command line if you can't find it Computer Management:
 
perfmon
May 23, 2011 at 10:29pm
Crazy, but unfortunately I tried it and I'm still not getting helpful results. Thanks anyway.
May 23, 2011 at 11:28pm
What do you mean, you run perfmon, you select your process from Processes, select private bytes. It should turn up on the graph. Let it run as you do your tests. If it goes up and doesn't come down you have a memory leak.
May 24, 2011 at 5:31am
Why you people arguing on something which is not useful in finding memory leaks? Even if you find that there may be memory leak, you will still have to use source memory leak detector. Then why not start with that only?
May 24, 2011 at 5:40am
I personally use valgrind as part of my unit-testing:

to unit-test - make check
to test for memory leaks - make memcheck
to test for race conditions/threading problems - make drdcheck

I always run make check and memcheck, and I will run drdcheck periodically to ensure that new multithreading code works and that new code is not breaking old multithreading code

Even better is to use something like hudson continuous integration to automate testing on commit and daily tests
May 24, 2011 at 12:05pm
Why you people arguing on something which is not useful in finding memory leaks?
It was meant to be help, sometimes it's just not worth the bother.
May 24, 2011 at 7:27pm
I appreciate it kbw, if it's any consolation, however I've used the Valgrind to confirm I have no memory leak problems now.

Thanks kfmfe04, and everyone else. I seems to be able to load and unload in bulk to no gain in leaking memory.

Perfect!
Topic archived. No new replies allowed.