PDA

View Full Version : Possible memory leak in CmtDbQry.dll


jakogut
February 18th, 2016, 07:06 PM
In the low level API, it seems that when I call the function CmtGetRecordDataByRecId() a few hundred thousand times, something leaks memory. It's roughly a few hundred KB/s in the daemon I wrote. I'm using Python, which has a reference counted garbage collector, and all of the memory profiling seems to indicate that there's no uncontrolled memory allocation going on in my code.

I'm not quite sure yet if it's a problem with my code, or the library. I don't have access to Valgrind on Windows, so I'm not sure how I would go about conclusively proving it one way or the other. Is this a known problem? Is there some way we could figure out if this function is leaking memory?

Thanks, guys.

Support Team
February 19th, 2016, 06:09 AM
Thank you for posting this and asking. We are not aware of this and haven't received any other report about it. We will attempt to review it and see whether we find anything.

Support Team
February 22nd, 2016, 06:03 AM
FYI, we reviewed the source code of this function code and so far we have not found any memory leak.

jakogut
February 22nd, 2016, 12:31 PM
Thanks for taking the time to check! I'm still not sure why it seems to be leaking memory, but I've found a workaround for it.

jakogut
February 22nd, 2016, 03:36 PM
FYI, I reproduced the problem in less than fifty lines of C, without any dynamic memory allocation. This rules out Python garbage collection as well as a memory leak in my code.

You can find the source here: https://gist.github.com/jakogut/f429540cf8487ff24115

As I said, I believe that I found a workaround for the issue in my project, but I think this shows conclusively that there is a problem with a leak in this function.

I'd be happy to help further with this issue if necessary.

Support Team
February 23rd, 2016, 06:43 AM
Thank you. We'll have a look.

jakogut
February 24th, 2016, 12:23 PM
Thanks for looking into it. Have we found any more information on the leak?

Support Team
February 24th, 2016, 12:26 PM
Nope. It was queued for review though it might take time. Good that you've worked around this.

jakogut
May 4th, 2016, 11:04 AM
It appears that my workaround wasn't as resilient as I had thought.

I was refreshing the handle to the low-level API libraries after a certain number of calls, hoping Python's garbage collector would delete and free the loaded libraries after the reference count decreased to zero.

However, the ctypes module does not garbage collect and unload libraries by default until the interpreter exits, because any open references to the library in question will crash the interpreter in short order.

This means that my workaround was not actually freeing the memory being leaked, which was causing the machine hosting the code to run out of memory every two or three days.

I found that I could manually unload the library with a call to:

_ctypes.FreeLibrary(dll._handle)

This does free the leaked memory. However, as expected, there are still references to the unloaded library in memory, and the interpreter crashes after a few minutes. Consequently, this workaround does not work.

Is there any update on fixing the leak in the library itself?

Support Team
May 4th, 2016, 01:50 PM
Thank you for the update. I checked and we do not have any news on this right now. It was reviewed at the time but nothing came up. However, following your post here we will reevaluate and review this again in our lab and will post an update once we have it. Thanks!

jakogut
May 6th, 2016, 10:23 AM
I did find a better workaround for this issue. I isolated my wrapper class for CommitCRM's low level interface into its own process, and access it using Pyro4 (an RPC/data serialization library).

This way I can terminate and recreate the process to free up the used memory, it doesn't crash the interpreter, and even if it did, it wouldn't be a problem.

Support Team
May 6th, 2016, 11:02 AM
Thank you for the update and great that you find a solution for this. For what it work, this is something that we're still looking at from our side. Thanks!

Support Team
May 9th, 2016, 06:07 AM
We know that it already well works for you with the new configuration that frees the memory but nonetheless, we may have found what has been causing this under some call configurations and have just emailed you an updated dll to test with.