memory leak debug

2020-06-17 06:19发布

What are some techniques in detecting/debugging memory leak if you don't have trace tools?

6条回答
▲ chillily
2楼-- · 2020-06-17 06:37

Similar questions on SO:

In addition to the manual inspection techniques mentioned by others, you should consider a code analysis tool such as valgrind.

Introduction from their site:

Valgrind is an award-winning instrumentation framework for building dynamic analysis tools. There are Valgrind tools that can automatically detect many memory management and threading bugs, and profile your programs in detail. You can also use Valgrind to build new tools.

The Valgrind distribution currently includes six production-quality tools: a memory error detector, two thread error detectors, a cache and branch-prediction profiler, a call-graph generating cache profiler, and a heap profiler. It also includes two experimental tools: a heap/stack/global array overrun detector, and a SimPoint basic block vector generator. It runs on the following platforms: X86/Linux, AMD64/Linux, PPC32/Linux, PPC64/Linux, and X86/Darwin (Mac OS X).

查看更多
\"骚年 ilove
3楼-- · 2020-06-17 06:38

One possibility could be to compile the code and execute it on a system where you can take advantage of built in tools (e.g. libumem on Solaris, or the libc capability on Linux)

查看更多
三岁会撩人
4楼-- · 2020-06-17 06:41

Divide and conquer is the best approach. If you have written you code in a systematic way, it should be pretty easy to call subsets of you code. Your best bet is to execute each section of code over and over and see if your memory usage steadily climbs, if not move on to the next section of code.

Also, the wikipedia article on memory leaks has several great links in the references section on detecting memory leaks for different systems (window, macos, linux, etc)

查看更多
可以哭但决不认输i
5楼-- · 2020-06-17 06:42

I have used memtrace http://www.fpx.de/fp/Software/MemTrace/ http://sourceforge.net/projects/memtrace/

You may need to call the statistics function to printout if there are any leaks. Best thing is to call this statistics function before and after a module or piece of code gets executed. * Warning * Memtrace is kind enough to allow memory overwrite/double free. It detects these anomalies and gracefully avoids any crash.

查看更多
Summer. ? 凉城
6楼-- · 2020-06-17 06:50
  1. Check out your loops
  2. Look at where you are allocating variables - do you ever de-allocate them?
  3. Try and reproduce the leak with a small subset of suspected code.
  4. MAKE trace tools - you can always log to a file.
查看更多
forever°为你锁心
7楼-- · 2020-06-17 06:56

Intercept all functions that allocate and deallocate memory (depending on the platform, the list may look like: malloc, calloc, realloc, strdup, getcwd, free), and in addition to performing what these functions originally do, save information about the calls somewhere, in a dynamically growing global array probably, protected by synchronization primitives for multithreaded programs.

This information may include function name, amount of memory requested, address of the successfully allocated block, stack trace that lets you figure out what the caller was, and so on. In free(), remove corresponding element from the array (if there are none, a wrong pointer is passed to free which is also a error that's good to be detected early). When the program ends, dump the remaining elements of the array - they will be the blocks that leaked. Don't forget about global objects that allocate and deallocate resources before and after main(), respectively. To properly count those resources, you will need to dump the remaining resources after the last global object gets destroyed, so a small hack of your compiler runtime may be necessary

查看更多
登录 后发表回答