项目作者: AviAvni

项目描述 :
Win32 memory leak detector with ETW
高级语言: C#
项目地址: git://github.com/AviAvni/NativeLeakDetector.git
创建时间: 2017-12-07T15:30:56Z
项目社区:https://github.com/AviAvni/NativeLeakDetector

开源协议:

下载


Win32 Leak Detector

This is a Win32 memory leak detector that instruments the Windows heap allocation APIs and collects real-time allocation information. It aggregates allocation stacks in real-time, and can display any memory allocated by an application that was not yet freed, helping identify and resolve memory leaks. It works only in live mode, and collects the HeapAlloc and HeapFree ETW events for a specified process. Unlike some other tools, this tool does not record every allocation and free event to a file on disk and analyzes them later — for production processes with a heavy allocation load, this makes the difference between a working tool and a gigantic disk hog.

Importantly, if the target process exits before the tool had a chance to print stacks, symbol resolution will fail, so it is more suitable for longer-running processes.

NOTE: This project is not done. There are still some unimplemented features, and the code hasn’t been extensively tested. Caveat emptor, and pull requests welcome!

Running

Open a command prompt window as administrator, and try the example Demo program.

Collect allocation events and print the top leaked stacks when Ctrl+C is hit:

  1. LeakDetector -p 7408

Print the top stacks, sorted by top leak

  1. LeakDetector -T -p 7408

How often to print the stack summary in seconds

  1. LeakDetector -i 5 -p 7408

How many times to print a summary before quitting

  1. LeakDetector -c 5 -p 7408

Clear the screen between printouts

  1. LeakDetector -C -i 5 -p 7408

Example Output

  1. 18:26:32
  2. AllocateCount: 30
  3. FreeCount: 0
  4. AlocateSize: 5040
  5. FreeSize: 0
  6. ntdll.dll!NtTraceEvent+0xC
  7. ntdll.dll!RtlpLogHeapAllocateEvent+0x5F
  8. ntdll.dll!RtlpAllocateHeapInternal+0x411
  9. ntdll.dll!RtlAllocateHeap+0x3E
  10. ucrtbased.dll!heap_alloc_dbg_internal+0x198
  11. ucrtbased.dll!heap_alloc_dbg+0x36
  12. ucrtbased.dll!_malloc_dbg+0x1A
  13. ucrtbased.dll!malloc+0x14
  14. Demo.exe!+0x11F0D
  15. Demo.exe!+0x11AB2
  16. Demo.exe!+0x119D0
  17. KERNEL32.DLL!BaseThreadInitThunk+0x24
  18. ntdll.dll!__RtlUserThreadStart+0x2F
  19. ntdll.dll!_RtlUserThreadStart+0x1B
  20. ------------
  21. AllocateCount: 20
  22. FreeCount: 0
  23. AlocateSize: 4000
  24. FreeSize: 0
  25. ntdll.dll!NtTraceEvent+0xC
  26. ntdll.dll!RtlpLogHeapAllocateEvent+0x5F
  27. ntdll.dll!RtlpAllocateHeapInternal+0x411
  28. ntdll.dll!RtlAllocateHeap+0x3E
  29. ntdll.dll!RtlpReAllocateHeap+0x1C2
  30. ntdll.dll!RtlpReAllocateHeapInternal+0x660
  31. ntdll.dll!RtlReAllocateHeap+0x43
  32. Demo.exe!+0x11C1D
  33. Demo.exe!+0x119D0
  34. KERNEL32.DLL!BaseThreadInitThunk+0x24
  35. ntdll.dll!__RtlUserThreadStart+0x2F
  36. ntdll.dll!_RtlUserThreadStart+0x1B
  37. ------------
  38. AllocateCount: 30
  39. FreeCount: 24
  40. AlocateSize: 3000
  41. FreeSize: 2400
  42. ntdll.dll!NtTraceEvent+0xC
  43. ntdll.dll!RtlpLogHeapAllocateEvent+0x5F
  44. ntdll.dll!RtlpAllocateHeapInternal+0x411
  45. ntdll.dll!RtlAllocateHeap+0x3E
  46. Demo.exe!+0x11BF0
  47. Demo.exe!+0x119D0
  48. KERNEL32.DLL!BaseThreadInitThunk+0x24
  49. ntdll.dll!__RtlUserThreadStart+0x2F
  50. ntdll.dll!_RtlUserThreadStart+0x1B
  51. ------------
  52. AllocateCount: 4
  53. FreeCount: 0
  54. AlocateSize: 800
  55. FreeSize: 0
  56. ntdll.dll!NtTraceEvent+0xC
  57. ntdll.dll!RtlpLogHeapAllocateEvent+0x5F
  58. ntdll.dll!RtlpAllocateHeapInternal+0x411
  59. ntdll.dll!RtlAllocateHeap+0x3E
  60. ntdll.dll!RtlpReAllocateHeap+0xA7C
  61. ntdll.dll!RtlpReAllocateHeapInternal+0x660
  62. ntdll.dll!RtlReAllocateHeap+0x43
  63. Demo.exe!+0x11C1D
  64. Demo.exe!+0x119D0
  65. KERNEL32.DLL!BaseThreadInitThunk+0x24
  66. ntdll.dll!__RtlUserThreadStart+0x2F
  67. ntdll.dll!_RtlUserThreadStart+0x1B

Requirements/Limitations

Kernel symbols are currently not resolved, and filtered out by default.

Overhead

This tool does not inject anything into the target process, and relies only on ETW events. Furthermore, it does not use disk buffers, and processes events in real-time. Still, very high allocation rates combined with an otherwise loaded system can introduce additional overhead due to the event processing and aggregation. Further benchmarking is needed to establish more accurate estimates.

Building

To build the tool, you will need Visual Studio 2015/2017, and the Windows SDK installed (for the symsrv.dll and dbghelp.dll files).