Good afternoon all,
What I’m trying to accomplish: I’d like to implement an extension to a C++ unit test fixture to detect if the test allocates memory and doesn’t free it. My idea was to record allocation levels or free memory levels before and after the test. If they don’t match then you’re leaking memory.
What I’ve tried so far: I’ve written a routine to read /proc/self/stat to get the vm size and resident set size. Resident set size seems like what I need but it’s obviously not right. It changes between successive calls to the function with no memory allocation. I believe it’s returning the cached memory used not what’s allocated. It also changes in 4k increments so it’s too coarse to be of any real use.
I can get the stack size by allocating a local and saving it’s address. Are there any problems with doing this?
Is there a way to get real free or allocated memory on linux?
Thanks
I’d have to agree with those suggesting Valgrind and similar, but if the run-time overhead is too great, one option may be to use mallinfo() call to retrieve statistics on currently allocated memory, and check whether
uordblksis nonzero.Note that this will have to be run before global destructors are called – so if you have any allocations that are cleaned up there, this will register a false positive. It also won’t tell you where the allocation is made – but it’s a good first pass to figure out which test cases need work.