简体   繁体   中英

memory leaks during development

So, I've recently noticed that our development server has a steady ~300MB out of 4GB ram left after the finished development of a certain project. Assuming this was due to memory leaks during the development phase, will that memory eventually free itself up or will it require a server restart. Are there any tools that can be used to prevent this in the future (aside from the obvious, 'don't write code that produces memory leaks')? Sometimes they go unseen for a little while and over time I guess they add up as you continue testing your app.

What operating system are you running? Most operating systems these days will clean up leaked memory for a process when the process exits. It is possible that the memory you are seeing in use is actually being used for the filesystem cache. This is nothing to worry about -- the OS will reclaim this memory if necessary.

From: http://learnlinux.tsf.org.za/courses/build/internals/ch05.html

The amount of free memory indicated by the free command includes the current size of the buffer cache in its calculation. This is misleading, as the amount of free memory indicated will often be very low, as the buffer cache soon fills most of user memory. Don't' panic. Applications are probably not crowding your RAM; it is merely the buffer cache that is taking up all available space. The buffer cache counts as memory space available for application use (remembering that it will be shrunk as required), so subtract the size of the buffer cache to see the real amount of free memory available for application use

Run the program using the exceptional valgrind on Linux x86 boxes.

A commerical equivilant, Purify , is available on Windows.

These runtime analysis of your program will report memory leaks and other errors such as buffer overflows and unitialised variables.

Static code analysis - Lint and Coverity for example - can also uncover memory leaks and more serious errors.


Lets be specific about what memory leaks cause and how they harm your program:

If you 'leak' memory during operation of your program there is a risk that your application will eventually exhaust RAM and swap, or the address space of available to your program (which can be less than physical RAM) and cause the next allocation to fail. The vast majority of programs will fail to catch this error, as error checking is harder than it seems. The majority of programs will either fail by dereferencing a null pointer or will exit.

It's best to fight them during development, because then it's easier to identify the revision that introduces the leak. As you probably see now, doing it after the fact is very, very hard. Expect a lot of reports when running the tools I recommend below:

http://valgrind.org/

http://www.ibm.com/software/awdtools/purify/

http://directory.fsf.org/project/ElectricFence/

I'd suggest you to run this tools, suppress most warnings about leaks, and then fix them one by one, removing the suppresions.

And then, make sure you regularly run these tools and quickly fix any regressions!

Of course the obvious answer is "Don't write code that produces memory leaks" and it's a valid one, because they can be extremely hard to fix if you have reference counting issues, or complex code in which it's hard to track the lifetime of memory.

To address your current situation you might consider using a tool such as DevPartner for Windows, or Valgrind for Linux/Unix, both of which I've found to be very effective for tracking down memory leaks (as well as other issues such as performance bottlenecks).

Another thing you may wish to consider is to look at your use of pointers and slowly replace them with smart pointers if you can, which should help manage your pointer lifetimes.

And no, I doubt that memory is going to be recovered without restarting the process in which your code is running.

不要做任何事情,在它上面运行一个内存探查器,看看它在做什么。

If this is on Linux, check the output of 'free' and specifically check the amount of 'cached' ram. If your development work includes a lot of disk I/O, it'll use it for caching files, and you'll see very little 'available' but it's still there if it's needed. For all practical purposes, consider free+cached as available.

The 'free' output is distilled from /proc/meminfo, and you can get more detailed information on the running process in /proc/$pid/{maps,smaps}

In theory when your process exits, any memory it had is released. Is your process exiting?

When I was at college we used the Borland C++ Builder 6 IDE It included CodeGuard, which checks for memory leaks and other memory related issues. I am not sure if this option is still available on newer versions, but it would be weird for a new version to have less features.

On linux, as mentioned before, valgrind is a good memory leak debugger.

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