简体   繁体   中英

How to predict memory leak in linux c/c++ programme?

I'm tring to predict memory leak in an linux c++ programme.

The programme is an network server which fork a lot of sub-process to handle request.

I can get the RSS of the process after it handled one request, and I want to make the process kill itself if it find out that there could be memory leak.

I can make the process kill itself if it's RSS is bigger than an configured value ( like 1GB ), but this seems too simple and this value is also not easy to configure.

I can also make the process kill itself if the RSS is bigger and bigger for N times (like 100), but this seems not proper too, if the process cache some datas.

Is there any memory leak prediction algorithms?

I have googled it, but can't find one.

Thanks.


Sorry for not makeing the question clear.

Actually I'm not trying to find some way to find how the memory leak, I know there is tools like valgrind.

The situation is like this:

The programme I worted is an RPC framework, there is a father process, which will fork lots of sub-processes.

These sub process will run some business logic code which is not written by me and I have no control of these codes.

When these logic codes leak memory, the sub-process will be killed by the os OOM Killer.

But at that point, due to lack of memory, the os will halt for a while (minutes or even more ) or even breakdown and we will have to reboot the system.

I want to avoid this situation by predict the memory leak of the logic codes and make the sub-process kill itself before the OOM Kill do it's job.

And if the sub-process kill itself, the father process will fork another sub-process to run the logic code.

I can get the memory occupied by the sub-process after it handle one request.

So the simplest way is to check if the sub-process occupy too much memory than an pre-configured value (like 1GB), if it is, then make it kill itself.

But this algorithm seems too simple and the value is not easy to configure.

So what I'm finding is an algorithm to predict that if there is memory leak in an process.

Thanks

You can't predict memory leaks (it probably can be proved equivalent to the halting problem ).

You could measure or instrument the binary, notably with valgrind (which will only tell you if some particular execution has leaked).

You could also consider using Boehm's conservative garbage collector . It would usually free memory automatically (but there is no guarantee since it is a conservative garbage collector !).

Without further constraints, no there isn't. The program could do anything and as a result have what ever kind of pattern in its memory use. Tools like valgrind can detect memory leaks, for example by tracking each memory allocation and noticing when there is a piece of allocated memory which is not reachable. But they are essentially running the program in a VM or under debugger, which a huge performance penalty, so not a solution for production use memory leak detection.

Solution is really to not leak memory, to find memory leaks while still developing the software. There you can use tools like valgrind , combined with enough test cases, to catch all leaks. But best is to do high quality software. Memory leaks are result of either bad methodology, or sloppy implementation.


As a practical solution to problem itself: Do not try to detect memory leaks. Just restart the process for example once per 24h, at the time where it causes least disruption. Of course this is a bit "ugly", it encourages just ignoring some bugs when they should be fixed for example. This is a case of "ugly reality vs. sound engineering principles".

I'm tring to predict memory leak in an linux c++ programme. [...] Is there any memory leak prediction algorithms?

No, there is no algorithm. This depends on the developer's good or bad habits. That means, if you wrote ten applications and they all had memory leaks, you can predict that your 11th one will have memory leaks (ie there is 1 chance in 11 that your app will not have memory leaks).

This looks to me like the xy problem. If you have child processes with memory leaks, the proper solution would be to remove the memory leaks, from the child processes. Otherwise, you can impose a limit in your system (ie no child process will consume more than 350Mb of memory"). This limit though is in no way connected to memory leaks.

To detect memory leaks in your development process, consider using a specialized tool (like valgrind or BoundsChecker).

One way to deal with memory leaks is to implement reference counting. The idea is to have for each object or struct, a count of how much pointers are pointing to it, when this count drop to 0 free the allocated memory. This forces you to rethink the code, but it is effective when several objects are pointing to another object and cannot predict when it will become useless.

If you are programming in C++, you could try boost libraries, they have implementation of smart pointers for reference counting.

http://www.boost.org/doc/libs/1_55_0/libs/smart_ptr/smart_ptr.htm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM